gh-150633: properly handle imports with null bytes in names#150634
Open
KowalskiThomas wants to merge 5 commits into
Open
gh-150633: properly handle imports with null bytes in names#150634KowalskiThomas wants to merge 5 commits into
KowalskiThomas wants to merge 5 commits into
Conversation
| @@ -0,0 +1,3 @@ | |||
| Fix :func:`__import__` accepting module names with embedded null bytes, which | |||
Member
There was a problem hiding this comment.
This is for frozen modules only, right? I cannot reproduce this for other modules.
Contributor
Author
There was a problem hiding this comment.
Yes, indeed. Initially I had added a check rejecting null bytes in every __import__ call (which I think matches what is discussed in the issue) but I eventually went for a more targeted fix. I'll change the news entry for now so it matches what the code does.
| # lead to duplicates in sys.modules | ||
| before = set(sys.modules.keys()) | ||
| with self.assertRaises(ModuleNotFoundError): | ||
| __import__('codecs\x00junk') |
Member
There was a problem hiding this comment.
I get ModuleNotFoundError on non-installed Python. The bug is only reproducible om the system Python. Can we make the test more reproducible?
Contributor
Author
There was a problem hiding this comment.
I changed it to zipimport instead of codecs, I have it failing on my non-installed build as well now.
% ./python.exe -m unittest Lib.test.test_import.ImportTests.test_import_null_byte_in_name_raises_ModuleNotFoundError -v
test_import_null_byte_in_name_raises_ModuleNotFoundError (Lib.test.test_import.ImportTests.test_import_null_byte_in_name_raises_ModuleNotFoundError) ... FAIL
======================================================================
FAIL: test_import_null_byte_in_name_raises_ModuleNotFoundError (Lib.test.test_import.ImportTests.test_import_null_byte_in_name_raises_ModuleNotFoundError)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/thomas.kowalski/Documents/cpython/Lib/test/test_import/__init__.py", line 371, in test_import_null_byte_in_name_raises_ModuleNotFoundError
with self.assertRaises(ModuleNotFoundError):
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
AssertionError: ModuleNotFoundError not raised
----------------------------------------------------------------------
Ran 1 test in 0.005s
FAILED (failures=1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is this PR?
Fixes #150633.
sys.modulescache #150633