From ddcf7e4d6290fa607c15754fb2ff900941f2d461 Mon Sep 17 00:00:00 2001 From: dr-carlos Date: Fri, 28 Nov 2025 16:39:13 +1030 Subject: [PATCH 1/9] Check that `name` is non-empty in `_imp.create_builtin()` --- Python/import.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Python/import.c b/Python/import.c index e91c95b40d94bf..e123e9665fe1a4 100644 --- a/Python/import.c +++ b/Python/import.c @@ -4420,6 +4420,13 @@ _imp_create_builtin(PyObject *module, PyObject *spec) return NULL; } + if (PyUnicode_GetLength(name) == 0) { + PyErr_Format(PyExc_TypeError, + "name must not be empty"); + Py_DECREF(name); + return NULL; + } + PyObject *mod = create_builtin(tstate, name, spec, NULL); Py_DECREF(name); return mod; From 901b039c924f33f656e67825dca74d1691e0cc7e Mon Sep 17 00:00:00 2001 From: dr-carlos Date: Fri, 28 Nov 2025 16:53:51 +1030 Subject: [PATCH 2/9] Edit NEWS entry --- .../2025-11-28-16-45-07.gh-issue-142029.JuXiKu.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-11-28-16-45-07.gh-issue-142029.JuXiKu.rst 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 index 00000000000000..b247d3a166f776 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-11-28-16-45-07.gh-issue-142029.JuXiKu.rst @@ -0,0 +1,2 @@ +Raise :exc:`TypeError` instead of crashing when empty string is used as a +name in :func:`importlib._imp.create_builtin`. From 784c232286fcd9fac7a498115413ac9b8608457e Mon Sep 17 00:00:00 2001 From: dr-carlos Date: Fri, 28 Nov 2025 17:00:00 +1030 Subject: [PATCH 3/9] Don't use `:func:` in NEWS for undocumented function --- .../2025-11-28-16-45-07.gh-issue-142029.JuXiKu.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index b247d3a166f776..962589dd3543a4 100644 --- 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 @@ -1,2 +1,2 @@ Raise :exc:`TypeError` instead of crashing when empty string is used as a -name in :func:`importlib._imp.create_builtin`. +name in ``importlib._imp.create_builtin``. From 5102f3d5b2e8e5091a39801b300c6950fb957b35 Mon Sep 17 00:00:00 2001 From: dr-carlos Date: Sat, 29 Nov 2025 07:44:23 +1030 Subject: [PATCH 4/9] Update function name in NEWS entry --- .../2025-11-28-16-45-07.gh-issue-142029.JuXiKu.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 962589dd3543a4..f64fa9a18fd561 100644 --- 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 @@ -1,2 +1,2 @@ Raise :exc:`TypeError` instead of crashing when empty string is used as a -name in ``importlib._imp.create_builtin``. +name in ``_imp.create_builtin()``. From 035017723744d2a0ec96855d752e3a5942cc0a47 Mon Sep 17 00:00:00 2001 From: dr-carlos Date: Sat, 29 Nov 2025 07:50:59 +1030 Subject: [PATCH 5/9] Add simple tests for `create_builtin()` --- Lib/test/test_import/__init__.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index 271361ae816449..a0b859ac28414f 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -1253,6 +1253,28 @@ class Spec2: 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) + + class Spec: + name = "" + spec = Spec() + + # gh-142029 + with self.assertRaisesRegex( + TypeError, + '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') From 5d9aa6ac62f0a69c15a3d49d11745be03761a0aa Mon Sep 17 00:00:00 2001 From: dr-carlos Date: Wed, 3 Dec 2025 08:39:29 +1030 Subject: [PATCH 6/9] Update formatting in `test_create_builtin()` --- Lib/test/test_import/__init__.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index a0b859ac28414f..b1d66d6846c066 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -1258,21 +1258,14 @@ class Spec: name = None spec = Spec() - with self.assertRaisesRegex( - TypeError, - 'name must be string, not NoneType' - ): + with self.assertRaisesRegex(TypeError, 'name must be string, not NoneType'): _imp.create_builtin(spec) - class Spec: - name = "" + spec.name = "" spec = Spec() # gh-142029 - with self.assertRaisesRegex( - TypeError, - 'name must not be empty' - ): + with self.assertRaisesRegex(TypeError,'name must not be empty'): _imp.create_builtin(spec) def test_filter_syntax_warnings_by_module(self): From 81185df683fb2d7edd991b3ea38fc3eb69a533fd Mon Sep 17 00:00:00 2001 From: dr-carlos Date: Wed, 3 Dec 2025 08:44:17 +1030 Subject: [PATCH 7/9] Raise `ValueError` instead of `TypeError` for empty names --- Lib/test/test_import/__init__.py | 3 +-- Python/import.c | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index b1d66d6846c066..fa4f5e013d4b17 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -1262,10 +1262,9 @@ class Spec: _imp.create_builtin(spec) spec.name = "" - spec = Spec() # gh-142029 - with self.assertRaisesRegex(TypeError,'name must not be empty'): + with self.assertRaisesRegex(ValueError, 'name must not be empty'): _imp.create_builtin(spec) def test_filter_syntax_warnings_by_module(self): diff --git a/Python/import.c b/Python/import.c index e123e9665fe1a4..c749ca4bf6d99a 100644 --- a/Python/import.c +++ b/Python/import.c @@ -4421,7 +4421,7 @@ _imp_create_builtin(PyObject *module, PyObject *spec) } if (PyUnicode_GetLength(name) == 0) { - PyErr_Format(PyExc_TypeError, + PyErr_Format(PyExc_ValueError, "name must not be empty"); Py_DECREF(name); return NULL; From 2165ccee898e3eb59172c4b96e4cd65addf155c3 Mon Sep 17 00:00:00 2001 From: dr-carlos Date: Wed, 3 Dec 2025 09:11:34 +1030 Subject: [PATCH 8/9] Update NEWS entry --- .../2025-11-28-16-45-07.gh-issue-142029.JuXiKu.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index f64fa9a18fd561..b4cd284804de98 100644 --- 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 @@ -1,2 +1,2 @@ -Raise :exc:`TypeError` instead of crashing when empty string is used as a -name in ``_imp.create_builtin()``. +Raise :exc:`ValueError` instead of crashing when empty string is used as a name +in ``_imp.create_builtin()``. From 705b4738888ca6e88d0faa3ec664156c14c50ce6 Mon Sep 17 00:00:00 2001 From: dr-carlos <77367421+dr-carlos@users.noreply.github.com> Date: Wed, 3 Dec 2025 09:23:27 +1030 Subject: [PATCH 9/9] Update formatting in `import.c` Co-authored-by: Victor Stinner --- Python/import.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Python/import.c b/Python/import.c index c749ca4bf6d99a..4dd247fac27654 100644 --- a/Python/import.c +++ b/Python/import.c @@ -4421,8 +4421,7 @@ _imp_create_builtin(PyObject *module, PyObject *spec) } if (PyUnicode_GetLength(name) == 0) { - PyErr_Format(PyExc_ValueError, - "name must not be empty"); + PyErr_Format(PyExc_ValueError, "name must not be empty"); Py_DECREF(name); return NULL; }