Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
):
Comment thread
dr-carlos marked this conversation as resolved.
Outdated
_imp.create_builtin(spec)

class Spec:
name = ""
spec = Spec()
Comment thread
dr-carlos marked this conversation as resolved.
Outdated

# gh-142029
with self.assertRaisesRegex(
TypeError,
'name must not be empty'
):
Comment thread
dr-carlos marked this conversation as resolved.
Outdated
_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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Raise :exc:`TypeError` instead of crashing when empty string is used as a
name in ``_imp.create_builtin()``.
7 changes: 7 additions & 0 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -4420,6 +4420,13 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
return NULL;
}

if (PyUnicode_GetLength(name) == 0) {
PyErr_Format(PyExc_TypeError,
Comment thread
dr-carlos marked this conversation as resolved.
Outdated
"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