[Python-checkins] cpython (2.7): Issue #21720: Improve exception message when the type of fromlist is unicode

berker.peksag python-checkins at python.org
Sun Oct 16 18:04:24 EDT 2016


https://hg.python.org/cpython/rev/7dd0910e8fbf
changeset:   104514:7dd0910e8fbf
branch:      2.7
parent:      104503:94f02193f00f
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Mon Oct 17 01:05:04 2016 +0300
summary:
  Issue #21720: Improve exception message when the type of fromlist is unicode

files:
  Lib/test/test_import.py |  8 ++++++++
  Misc/NEWS               |  5 +++++
  Python/import.c         |  5 +++--
  3 files changed, 16 insertions(+), 2 deletions(-)


diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -415,6 +415,14 @@
         finally:
             rmtree(dir_name)
 
+    def test_fromlist_type(self):
+        with self.assertRaises(TypeError) as cm:
+            __import__('encodings', fromlist=[u'aliases'])
+        self.assertIn('must be str, not unicode', str(cm.exception))
+        with self.assertRaises(TypeError) as cm:
+            __import__('encodings', fromlist=[1])
+        self.assertIn('must be str, not int', str(cm.exception))
+
 
 class PycRewritingTests(unittest.TestCase):
     # Test that the `co_filename` attribute on code objects always points
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,11 @@
 Core and Builtins
 -----------------
 
+- Issue #21720: Improve exception message when the type of fromlist is unicode.
+  fromlist parameter of __import__() only accepts str in Python 2 and this
+  will help to identify the problem especially when the unicode_literals
+  future import is used.
+
 - Issue #26906: Resolving special methods of uninitialized type now causes
   implicit initialization of the type instead of a fail.
 
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -2591,8 +2591,9 @@
             return 0;
         }
         if (!PyString_Check(item)) {
-            PyErr_SetString(PyExc_TypeError,
-                            "Item in ``from list'' not a string");
+            PyErr_Format(PyExc_TypeError,
+                         "Item in ``from list'' must be str, not %.200s",
+                         Py_TYPE(item)->tp_name);
             Py_DECREF(item);
             return 0;
         }

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list