Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/demandimport.py @ 30024:26a4e46af2bc
demandimport: error out early on missing attribute of non package (issue5373)
If the parent module isn't a package, all valid attributes must be obtained
from it. We can raise ImportError early if any attributes not found.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 26 Sep 2016 23:28:57 +0900 |
parents | 16a09ae318b4 |
children | 75c71c533977 |
comparison
equal
deleted
inserted
replaced
30023:16a09ae318b4 | 30024:26a4e46af2bc |
---|---|
189 globalname = globals.get('__name__') | 189 globalname = globals.get('__name__') |
190 | 190 |
191 def processfromitem(mod, attr): | 191 def processfromitem(mod, attr): |
192 """Process an imported symbol in the import statement. | 192 """Process an imported symbol in the import statement. |
193 | 193 |
194 If the symbol doesn't exist in the parent module, it must be a | 194 If the symbol doesn't exist in the parent module, and if the |
195 module. We set missing modules up as _demandmod instances. | 195 parent module is a package, it must be a module. We set missing |
196 modules up as _demandmod instances. | |
196 """ | 197 """ |
197 symbol = getattr(mod, attr, nothing) | 198 symbol = getattr(mod, attr, nothing) |
199 nonpkg = getattr(mod, '__path__', nothing) is nothing | |
198 if symbol is nothing: | 200 if symbol is nothing: |
201 if nonpkg: | |
202 # do not try relative import, which would raise ValueError | |
203 raise ImportError('cannot import name %s' % attr) | |
199 mn = '%s.%s' % (mod.__name__, attr) | 204 mn = '%s.%s' % (mod.__name__, attr) |
200 if mn in ignore: | 205 if mn in ignore: |
201 importfunc = _origimport | 206 importfunc = _origimport |
202 else: | 207 else: |
203 importfunc = _demandmod | 208 importfunc = _demandmod |