Skip to content

bpo-31315: Fix an assertion failure in imp.create_dynamic(), when spec.name is not a string #3257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions Lib/test/test_imp.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ def test_load_source(self):
with self.assertRaisesRegex(ValueError, 'embedded null'):
imp.load_source(__name__, __file__ + "\0")

@support.cpython_only
def test_issue31315(self):
# There shouldn't be an assertion failure in imp.create_dynamic(),
# when spec.name is not a string.
create_dynamic = support.get_attribute(imp, 'create_dynamic')
class BadSpec:
name = None
origin = 'foo'
with self.assertRaises(TypeError):
create_dynamic(BadSpec())


class ReloadTests(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an assertion failure in imp.create_dynamic(), when spec.name is not a
string. Patch by Oren Milman.
5 changes: 5 additions & 0 deletions Python/importdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
if (name_unicode == NULL) {
return NULL;
}
if (!PyUnicode_Check(name_unicode)) {
PyErr_SetString(PyExc_TypeError,
"spec.name must be a string");
goto error;
}

name = get_encoded_name(name_unicode, &hook_prefix);
if (name == NULL) {
Expand Down