Skip to content

gh-150633: properly handle imports with null bytes in names#150634

Open
KowalskiThomas wants to merge 5 commits into
python:mainfrom
KowalskiThomas:kowalski/fix-properly-handle-imports-with-null-bytes-in-names
Open

gh-150633: properly handle imports with null bytes in names#150634
KowalskiThomas wants to merge 5 commits into
python:mainfrom
KowalskiThomas:kowalski/fix-properly-handle-imports-with-null-bytes-in-names

Conversation

@KowalskiThomas
Copy link
Copy Markdown
Contributor

@KowalskiThomas KowalskiThomas commented May 30, 2026

@KowalskiThomas KowalskiThomas changed the title fix: properly handle imports with null bytes in names gh-150633: properly handle imports with null bytes in names May 30, 2026
@KowalskiThomas KowalskiThomas marked this pull request as ready for review June 1, 2026 14:15
@@ -0,0 +1,3 @@
Fix :func:`__import__` accepting module names with embedded null bytes, which
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for frozen modules only, right? I cannot reproduce this for other modules.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Lib/test/test_import/__init__.py Outdated
# lead to duplicates in sys.modules
before = set(sys.modules.keys())
with self.assertRaises(ModuleNotFoundError):
__import__('codecs\x00junk')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get ModuleNotFoundError on non-installed Python. The bug is only reproducible om the system Python. Can we make the test more reproducible?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Module names containing null bytes bypass the sys.modules cache

2 participants