]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-142029: Raise `ValueError` instead of crashing on empty name given to `create_buil...
authordr-carlos <77367421+dr-carlos@users.noreply.github.com>
Wed, 10 Dec 2025 06:31:57 +0000 (17:01 +1030)
committerGitHub <noreply@github.com>
Wed, 10 Dec 2025 06:31:57 +0000 (12:01 +0530)
Co-authored-by: Victor Stinner <vstinner@python.org>
Lib/test/test_import/__init__.py
Misc/NEWS.d/next/Core_and_Builtins/2025-11-28-16-45-07.gh-issue-142029.JuXiKu.rst [new file with mode: 0644]
Python/import.c

index 271361ae8164494e669e4cfd7ed1e215453bb31a..fa4f5e013d4b1721c280072f06f599c50af48d00 100644 (file)
@@ -1253,6 +1253,20 @@ os.does_not_exist
                 origin = "a\x00b"
             _imp.create_dynamic(Spec2())
 
+    def test_create_builtin(self):
+        class Spec:
+            name = None
+        spec = Spec()
+
+        with self.assertRaisesRegex(TypeError, 'name must be string, not NoneType'):
+            _imp.create_builtin(spec)
+
+        spec.name = ""
+
+        # gh-142029
+        with self.assertRaisesRegex(ValueError, 'name must not be empty'):
+            _imp.create_builtin(spec)
+
     def test_filter_syntax_warnings_by_module(self):
         module_re = r'test\.test_import\.data\.syntax_warnings\z'
         unload('test.test_import.data.syntax_warnings')
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-11-28-16-45-07.gh-issue-142029.JuXiKu.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-11-28-16-45-07.gh-issue-142029.JuXiKu.rst
new file mode 100644 (file)
index 0000000..b4cd284
--- /dev/null
@@ -0,0 +1,2 @@
+Raise :exc:`ValueError` instead of crashing when empty string is used as a name
+in ``_imp.create_builtin()``.
index e91c95b40d94bfd814eef04d4cfd4bf1fdb17b05..4dd247fac276549a4c923689c8f8c91b5468b30b 100644 (file)
@@ -4420,6 +4420,12 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
         return NULL;
     }
 
+    if (PyUnicode_GetLength(name) == 0) {
+        PyErr_Format(PyExc_ValueError, "name must not be empty");
+        Py_DECREF(name);
+        return NULL;
+    }
+
     PyObject *mod = create_builtin(tstate, name, spec, NULL);
     Py_DECREF(name);
     return mod;