##// END OF EJS Templates
demandimport: do not raise ImportError for unknown item in fromlist...
Yuya Nishihara -
r30647:1914db1b stable
parent child Browse files
Show More
@@ -199,8 +199,11 b' def _demandimport(name, globals=None, lo'
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
@@ -70,7 +70,16 b' try:'
70 print('no demandmod should be created for attribute of non-package '
70 print('no demandmod should be created for attribute of non-package '
71 'module:\ncontextlib.unknownattr =', f(unknownattr))
71 'module:\ncontextlib.unknownattr =', f(unknownattr))
72 except ImportError as inst:
72 except ImportError as inst:
73 print('contextlib.unknownattr = ImportError: %s' % inst)
73 print('contextlib.unknownattr = ImportError: %s'
74 % rsub(r"'", '', str(inst)))
75
76 # Unlike the import statement, __import__() function should not raise
77 # ImportError even if fromlist has an unknown item
78 # (see Python/import.c:import_module_level() and ensure_fromlist())
79 contextlibimp = __import__('contextlib', globals(), locals(), ['unknownattr'])
80 print("__import__('contextlib', ..., ['unknownattr']) =", f(contextlibimp))
81 print("hasattr(contextlibimp, 'unknownattr') =",
82 util.safehasattr(contextlibimp, 'unknownattr'))
74
83
75 demandimport.disable()
84 demandimport.disable()
76 os.environ['HGDEMANDIMPORT'] = 'disable'
85 os.environ['HGDEMANDIMPORT'] = 'disable'
@@ -18,4 +18,6 b" re.stderr = <open file '<whatever>', mod"
18 re = <proxied module 'sys'>
18 re = <proxied module 'sys'>
19 contextlib = <unloaded module 'contextlib'>
19 contextlib = <unloaded module 'contextlib'>
20 contextlib.unknownattr = ImportError: cannot import name unknownattr
20 contextlib.unknownattr = ImportError: cannot import name unknownattr
21 __import__('contextlib', ..., ['unknownattr']) = <module 'contextlib' from '?'>
22 hasattr(contextlibimp, 'unknownattr') = False
21 node = <module 'mercurial.node' from '?'>
23 node = <module 'mercurial.node' from '?'>
General Comments 0
You need to be logged in to leave comments. Login now