FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-142029: Raise `ValueError` instead of crashing on empty name given to `create_builtin()` by dr-carlos · Pull Request #142033 · python/cpython · GitHub

/ cpython Public
14 changes: 14 additions & 0 deletions Lib/test/test_import/__init__.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,20 @@ 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)

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')
Expand Down
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Raise :exc:`ValueError` instead of crashing when empty string is used as a name
in ``_imp.create_builtin()``.
6 changes: 6 additions & 0 deletions Python/import.c
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

Back | FazBrowse Home | New Git URL