New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
None value in sys.modules no longer blocks import #75823
Comments
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" |
This is a side effect of bpo-15767. The exception is raised in _find_and_load, but it is always silenced in _handle_fromlist. |
PR 3834 provide possible solution of this issue. Initially import errors were silenced for solving bpo-15715. 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 bpo-15767 (5fdb8c897023) the error message test was replaced by the type test, and that exception become silenced (I suppose it was not intentional). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: