Mercurial > hg
comparison mercurial/demandimport.py @ 30647:1914db1b7d9e stable
demandimport: do not raise ImportError for unknown item in fromlist
This is the behavior of the default __import__() function, which doesn't
validate the existence of the fromlist items. Later on, the missing attribute
is detected while processing the import statement.
https://hg.python.org/cpython/file/v2.7.13/Python/import.c#l2575
The comtypes library relies on this (maybe) undocumented behavior, and we
got a bug report to TortoiseHg, sigh.
https://bitbucket.org/tortoisehg/thg/issues/4647/
The test added at 26a4e46af2bc verifies the behavior of the import statement,
so this patch only adds the test of __import__() function and works around
CPython/PyPy difference.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 19 Dec 2016 22:46:00 +0900 |
parents | 75c71c533977 |
children | f80d9ddc40f3 |
comparison
equal
deleted
inserted
replaced
30558:7817df5585db | 30647:1914db1b7d9e |
---|---|
197 """ | 197 """ |
198 symbol = getattr(mod, attr, nothing) | 198 symbol = getattr(mod, attr, nothing) |
199 nonpkg = getattr(mod, '__path__', nothing) is nothing | 199 nonpkg = getattr(mod, '__path__', nothing) is nothing |
200 if symbol is nothing: | 200 if symbol is nothing: |
201 if nonpkg: | 201 if nonpkg: |
202 # do not try relative import, which would raise ValueError | 202 # do not try relative import, which would raise ValueError, |
203 raise ImportError('cannot import name %s' % attr) | 203 # and leave unknown attribute as the default __import__() |
204 # would do. the missing attribute will be detected later | |
205 # while processing the import statement. | |
206 return | |
204 mn = '%s.%s' % (mod.__name__, attr) | 207 mn = '%s.%s' % (mod.__name__, attr) |
205 if mn in ignore: | 208 if mn in ignore: |
206 importfunc = _origimport | 209 importfunc = _origimport |
207 else: | 210 else: |
208 importfunc = _demandmod | 211 importfunc = _demandmod |