##// END OF EJS Templates
demandimport: error out early on missing attribute of non package (issue5373)...
Yuya Nishihara -
r30022:26a4e46a default
parent child Browse files
Show More
@@ -191,11 +191,16 b' def _demandimport(name, globals=None, lo'
191 191 def processfromitem(mod, attr):
192 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
195 module. We set missing modules up as _demandmod instances.
194 If the symbol doesn't exist in the parent module, and if the
195 parent module is a package, it must be a module. We set missing
196 modules up as _demandmod instances.
196 197 """
197 198 symbol = getattr(mod, attr, nothing)
199 nonpkg = getattr(mod, '__path__', nothing) is nothing
198 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 204 mn = '%s.%s' % (mod.__name__, attr)
200 205 if mn in ignore:
201 206 importfunc = _origimport
@@ -63,6 +63,15 b' print("re =", f(re))'
63 63 print("re.stderr =", f(re.stderr))
64 64 print("re =", f(re))
65 65
66 import contextlib
67 print("contextlib =", f(contextlib))
68 try:
69 from contextlib import unknownattr
70 print('no demandmod should be created for attribute of non-package '
71 'module:\ncontextlib.unknownattr =', f(unknownattr))
72 except ImportError as inst:
73 print('contextlib.unknownattr = ImportError: %s' % inst)
74
66 75 demandimport.disable()
67 76 os.environ['HGDEMANDIMPORT'] = 'disable'
68 77 # this enable call should not actually enable demandimport!
@@ -16,4 +16,6 b" fred = <proxied module 're'>"
16 16 re = <unloaded module 'sys'>
17 17 re.stderr = <open file '<whatever>', mode 'w' at 0x?>
18 18 re = <proxied module 'sys'>
19 contextlib = <unloaded module 'contextlib'>
20 contextlib.unknownattr = ImportError: cannot import name unknownattr
19 21 node = <module 'mercurial.node' from '?'>
General Comments 0
You need to be logged in to leave comments. Login now