[Python-checkins] [3.6] bpo-31492: Fix assertion failures in case of a module with a bad __name__ attribute. (GH-3620). (#3773)

Serhiy Storchaka webhook-mailer at python.org
Wed Sep 27 00:33:03 EDT 2017


https://github.com/python/cpython/commit/f0db2dfda777ad3380e7816cabe4c4240f31687f
commit: f0db2dfda777ad3380e7816cabe4c4240f31687f
branch: 3.6
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2017-09-27T07:33:00+03:00
summary:

[3.6] bpo-31492: Fix assertion failures in case of a module with a bad __name__ attribute. (GH-3620). (#3773)

(cherry picked from commit 6db7033192cd537ca987a65971acb01206c3ba82)

files:
A Misc/NEWS.d/next/Core and Builtins/2017-09-16-22-49-16.bpo-31492.RtyteL.rst
M Lib/test/test_import/__init__.py
M Objects/moduleobject.c
M Python/ceval.c

diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
index e8111214b92..b73a96f7570 100644
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -353,6 +353,18 @@ def __getattr__(self, _):
         with self.assertRaises(ImportError):
             from test_from_import_AttributeError import does_not_exist
 
+    @cpython_only
+    def test_issue31492(self):
+        # There shouldn't be an assertion failure in case of failing to import
+        # from a module with a bad __name__ attribute, or in case of failing
+        # to access an attribute of such a module.
+        with swap_attr(os, '__name__', None):
+            with self.assertRaises(ImportError):
+                from os import does_not_exist
+
+            with self.assertRaises(AttributeError):
+                os.does_not_exist
+
     def test_concurrency(self):
         sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'data'))
         try:
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-09-16-22-49-16.bpo-31492.RtyteL.rst b/Misc/NEWS.d/next/Core and Builtins/2017-09-16-22-49-16.bpo-31492.RtyteL.rst
new file mode 100644
index 00000000000..a8704738d1a
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2017-09-16-22-49-16.bpo-31492.RtyteL.rst	
@@ -0,0 +1,3 @@
+Fix assertion failures in case of failing to import from a module with a bad
+``__name__`` attribute, and in case of failing to access an attribute of such
+a module. Patch by Oren Milman.
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 79be51a806b..f357af2b211 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -667,14 +667,11 @@ module_getattro(PyModuleObject *m, PyObject *name)
     if (m->md_dict) {
         _Py_IDENTIFIER(__name__);
         mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
-        if (mod_name) {
+        if (mod_name && PyUnicode_Check(mod_name)) {
             PyErr_Format(PyExc_AttributeError,
                         "module '%U' has no attribute '%U'", mod_name, name);
             return NULL;
         }
-        else if (PyErr_Occurred()) {
-            PyErr_Clear();
-        }
     }
     PyErr_Format(PyExc_AttributeError,
                 "module has no attribute '%U'", name);
diff --git a/Python/ceval.c b/Python/ceval.c
index b6ad444e70b..9ad582b15c5 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -5254,6 +5254,10 @@ import_from(PyObject *v, PyObject *name)
     if (pkgname == NULL) {
         goto error;
     }
+    if (!PyUnicode_Check(pkgname)) {
+        Py_CLEAR(pkgname);
+        goto error;
+    }
     fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
     Py_DECREF(pkgname);
     if (fullmodname == NULL) {



More information about the Python-checkins mailing list