https://github.com/python/cpython/commit/0977fc23fdc589fc089efee5733612e37480ea9e
commit: 0977fc23fdc589fc089efee5733612e37480ea9e
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-06-08T18:56:49Z
summary:

[3.14] gh-150633: Properly handle null characters in the name when importing 
frozen modules (GH-150634) (GH-151101)

(cherry picked from commit 54de5475cd753e2519692c3e54af0f150e0a8b62)

Co-authored-by: Thomas Kowalski <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-05-30-20-19-35.gh-issue-150633.XkNul0.rst
M Lib/test/test_import/__init__.py
M Python/import.c

diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
index 2e1c6d72f549f94..a6030d7b563834b 100644
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -360,6 +360,15 @@ def test_import_raises_ModuleNotFoundError(self):
         with self.assertRaises(ModuleNotFoundError):
             import something_that_should_not_exist_anywhere
 
+    def test_import_null_byte_in_name_raises_ModuleNotFoundError(self):
+        # gh-150633: module names containing null bytes should not
+        # lead to duplicates in sys.modules
+        before = set(sys.modules.keys())
+        with self.assertRaises(ModuleNotFoundError):
+            __import__('zipimport\x00junk')
+
+        self.assertEqual(set(sys.modules.keys()), before)
+
     def test_from_import_missing_module_raises_ModuleNotFoundError(self):
         with self.assertRaises(ModuleNotFoundError):
             from something_that_should_not_exist_anywhere import blah
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-05-30-20-19-35.gh-issue-150633.XkNul0.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-05-30-20-19-35.gh-issue-150633.XkNul0.rst
new file mode 100644
index 000000000000000..c397ad61f086c1b
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-05-30-20-19-35.gh-issue-150633.XkNul0.rst
@@ -0,0 +1,3 @@
+Fix the frozen importer accepting module names with embedded null bytes, which
+caused it to bypass the :data:`sys.modules` cache and create duplicate module
+objects.
diff --git a/Python/import.c b/Python/import.c
index 670e4fef91fb447..d0ba19d22e72255 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -3025,7 +3025,7 @@ find_frozen(PyObject *nameobj, struct frozen_info *info)
     if (nameobj == NULL || nameobj == Py_None) {
         return FROZEN_BAD_NAME;
     }
-    const char *name = PyUnicode_AsUTF8(nameobj);
+    const char *name = _PyUnicode_AsUTF8NoNUL(nameobj);
     if (name == NULL) {
         // Note that this function previously used
         // _PyUnicode_EqualToASCIIString().  We clear the error here

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to