[Python-checkins] [3.6] bpo-31315: Fix an assertion failure in imp.create_dynamic(), when spec.name is not a string. (GH-3257) (#3653)

Serhiy Storchaka webhook-mailer at python.org
Tue Sep 19 08:51:24 EDT 2017


https://github.com/python/cpython/commit/99a51d4e5b154a7b8d971090fecc1e34769a3ca1
commit: 99a51d4e5b154a7b8d971090fecc1e34769a3ca1
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Serhiy Storchaka <storchaka at gmail.com>
date: 2017-09-19T15:51:19+03:00
summary:

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

(cherry picked from commit 9974e1bcf3d0cec9b38b39b39b7ec8a1ebd9ef54)

files:
A Misc/NEWS.d/next/Core and Builtins/2017-09-01-00-40-58.bpo-31315.ZX20bl.rst
M Lib/test/test_imp.py
M Python/importdl.c

diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py
index 6f35f49487c..d513eedc7bb 100644
--- a/Lib/test/test_imp.py
+++ b/Lib/test/test_imp.py
@@ -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):
 
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
index 00000000000..d13badbb358
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2017-09-01-00-40-58.bpo-31315.ZX20bl.rst	
@@ -0,0 +1,2 @@
+Fix an assertion failure in imp.create_dynamic(), when spec.name is not a
+string. Patch by Oren Milman.
diff --git a/Python/importdl.c b/Python/importdl.c
index f56fa94cc42..326b26fd06e 100644
--- a/Python/importdl.c
+++ b/Python/importdl.c
@@ -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) {



More information about the Python-checkins mailing list