]> granicus.if.org Git - python/commitdiff
bpo-31315: Fix an assertion failure in imp.create_dynamic(), when spec.name is not...
authorOren Milman <orenmn@gmail.com>
Tue, 19 Sep 2017 11:39:47 +0000 (14:39 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 19 Sep 2017 11:39:47 +0000 (14:39 +0300)
Lib/test/test_imp.py
Misc/NEWS.d/next/Core and Builtins/2017-09-01-00-40-58.bpo-31315.ZX20bl.rst [new file with mode: 0644]
Python/importdl.c

index 3bfcb095894048fee8851d24e8075fd3ae1c1f97..51bec50cc2f264d56cc170391b2be1532128fe1c 100644 (file)
@@ -313,6 +313,17 @@ class ImportTests(unittest.TestCase):
         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):
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-09-01-00-40-58.bpo-31315.ZX20bl.rst b/Misc/NEWS.d/next/Core and Builtins/2017-09-01-00-40-58.bpo-31315.ZX20bl.rst
new file mode 100644 (file)
index 0000000..d13badb
--- /dev/null
@@ -0,0 +1,2 @@
+Fix an assertion failure in imp.create_dynamic(), when spec.name is not a
+string. Patch by Oren Milman.
index 32fb7e1be212ca0b7e343f33127b9100a3d3eb7d..50231ff28848a18798ca6907408aba3ab450e20d 100644 (file)
@@ -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) {