This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Ben Lewis2
Recipients Ben Lewis2, brett.cannon, eric.smith
Date 2019-06-27.04:45:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1561610743.52.0.746245193062.issue37409@roundup.psfhosted.org>
In-reply-to
Content
>>> 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 :-)
History
Date User Action Args
2019-06-27 04:45:43Ben Lewis2setrecipients: + Ben Lewis2, brett.cannon, eric.smith
2019-06-27 04:45:43Ben Lewis2setmessageid: <1561610743.52.0.746245193062.issue37409@roundup.psfhosted.org>
2019-06-27 04:45:43Ben Lewis2linkissue37409 messages
2019-06-27 04:45:43Ben Lewis2create