Message346710
>>> foo = 'oops'
>>> from . import foo as fubar # should raise ImportError
>>> fubar
'oops'
After further investigation, the problem is that builtins.__import__ (the c port version) does not replicate the behaviour of importlib.__import__ (the python reference version):
>>> import builtins, importlib
>>> __package__ is None
True
>>> importlib.__import__('', globals(), locals(), ('foo',), 1)
ImportError
>>> builtins.__import__('', globals(), locals(), ('foo',), 1)
<module '__main__' (built-in)>
A further discrepancy is that for deeper relative imports, builtins.__import__ raises a ValueError instead of ImportError (contrary to expectation/spec):
>>> from ...... import foo
ValueError
A simple work around uses the python implementation to restore expected behaviour:
>>> builtins.__import__ = importlib.__import__
>>> from ...... import foo
ImportError
>>> from curses import ascii
>>> from . import ascii
ImportError
PS: Brett Cannon, to replicate please copy and paste lines in correct order :-) |
|
| Date |
User |
Action |
Args |
| 2019-06-27 04:45:43 | Ben Lewis2 | set | recipients:
+ Ben Lewis2, brett.cannon, eric.smith |
| 2019-06-27 04:45:43 | Ben Lewis2 | set | messageid: <[email protected]> |
| 2019-06-27 04:45:43 | Ben Lewis2 | link | issue37409 messages |
| 2019-06-27 04:45:43 | Ben Lewis2 | create | |
|