classification
Title: None value in sys.modules no longer blocks import
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: barry, brett.cannon, christian.heimes, eric.snow, ncoghlan, serhiy.storchaka
Priority: normal Keywords: 3.6regression, patch

Created on 2017-09-29 20:21 by christian.heimes, last changed 2017-10-08 09:55 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 3834 merged serhiy.storchaka, 2017-09-30 06:47
PR 3923 merged serhiy.storchaka, 2017-10-08 08:15
Messages (5)
msg303356 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2017-09-29 20:21
Since Python 3.6, the blocking of imports is broken for 'from package import module' imports.

$ mkdir a
$ touch a/__init__.py a/b.py

>>> import sys
>>> sys.modules['a.b'] = None
>>> from a import b
>>> b is None
True
>>> import a.b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: import of 'a.b' halted; None in sys.modules

Tests with Python 2.7 to master:

$ python2.7 -c "from a import b; print(b)"
<module 'a.b' from 'a/b.py'>
$ python2.7 -c "import sys; sys.modules['a.b'] = None; from a import b"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name b
$ python2.7 -c "import sys; sys.modules['a.b'] = None; from a import b"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name b
$ python3.4 -c "import sys; sys.modules['a.b'] = None; from a import b"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: import of 'a.b' halted; None in sys.modules
$ python3.5 -c "import sys; sys.modules['a.b'] = None; from a import b"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: import of 'a.b' halted; None in sys.modules
$ python3.6 -c "import sys; sys.modules['a.b'] = None; from a import b"
$ ./python -c "import sys; sys.modules['a.b'] = None; from a import b"
msg303374 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-29 23:42
This is a side effect of issue15767. The exception is raised in _find_and_load, but it is always silenced in _handle_fromlist.
msg303396 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-30 06:56
PR 3834 provide possible solution of this issue.

Initially import errors were silenced for solving issue15715. An import error raised when import was blocked by setting None in sys.modules was not silenced because it has different error message. I don't know whether this was intentional. In issue15767 (5fdb8c897023) the error message test was replaced by the type test, and that exception become silenced (I suppose it was not intentional).
msg303900 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-08 07:44
New changeset f07e2b64df6304a36fb5e29397d3c77a7ba17704 by Serhiy Storchaka in branch 'master':
bpo-31642: Restore blocking "from" import by setting None in sys.modules. (#3834)
https://github.com/python/cpython/commit/f07e2b64df6304a36fb5e29397d3c77a7ba17704
msg303904 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-08 09:52
New changeset 6f059ab80a351a7dd85cc57c5ca240f817a79c5d by Serhiy Storchaka in branch '3.6':
[3.6] bpo-31642: Restore blocking "from" import by setting None in sys.modules. (GH-3834). (#3923)
https://github.com/python/cpython/commit/6f059ab80a351a7dd85cc57c5ca240f817a79c5d
History
Date User Action Args
2017-10-08 09:55:13serhiy.storchakasetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: - Python 3.8
2017-10-08 09:52:00serhiy.storchakasetmessages: + msg303904
2017-10-08 08:15:31serhiy.storchakasetpull_requests: + pull_request3896
2017-10-08 07:44:12serhiy.storchakasetmessages: + msg303900
2017-09-30 06:56:34serhiy.storchakasetmessages: + msg303396
2017-09-30 06:47:08serhiy.storchakasetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request3815
2017-09-29 23:42:25serhiy.storchakasetmessages: + msg303374
2017-09-29 20:32:45barrysetnosy: + barry
2017-09-29 20:23:21serhiy.storchakasetnosy: + serhiy.storchaka
2017-09-29 20:22:23christian.heimessettype: behavior
stage: needs patch
2017-09-29 20:22:13christian.heimessetnosy: + brett.cannon, ncoghlan, eric.snow
2017-09-29 20:21:12christian.heimescreate