]> granicus.if.org Git - python/commitdiff
Issue #21720: Improve exception message when the type of fromlist is unicode
authorBerker Peksag <berker.peksag@gmail.com>
Sun, 16 Oct 2016 22:05:04 +0000 (01:05 +0300)
committerBerker Peksag <berker.peksag@gmail.com>
Sun, 16 Oct 2016 22:05:04 +0000 (01:05 +0300)
Lib/test/test_import.py
Misc/NEWS
Python/import.c

index 2abf5b29400363708e227f8444db23a3700a3f35..432500505e07b13772577d9011f6c7409d9478e5 100644 (file)
@@ -415,6 +415,14 @@ class ImportTests(unittest.TestCase):
         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
index d0cc7a6c2a112f604d265af05962b2b480f76ceb..ba5ad65970f91436174b27d6bb83550cd6eb84bb 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,11 @@ What's New in Python 2.7.13?
 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.
 
index af47b0bdb4db393b35b8f00b1c939ee8e2a9f524..1d74faf5f6df125c3db8d8e9d147db78c3b83a03 100644 (file)
@@ -2591,8 +2591,9 @@ ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
             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;
         }