summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2016-10-16 22:05:04 (GMT)
committerBerker Peksag <berker.peksag@gmail.com>2016-10-16 22:05:04 (GMT)
commit05b84439a3541ea3a2fdda9f8abfd46b64c431e8 (patch)
treee3c1054e86b2247190703d9273341048203ad16d
parent197332ae5a9d4885806b5cceafef650c4e8f8b77 (diff)
downloadcpython-05b84439a3541ea3a2fdda9f8abfd46b64c431e8.zip
cpython-05b84439a3541ea3a2fdda9f8abfd46b64c431e8.tar.gz
cpython-05b84439a3541ea3a2fdda9f8abfd46b64c431e8.tar.bz2
Issue #21720: Improve exception message when the type of fromlist is unicode
-rw-r--r--Lib/test/test_import.py8
-rw-r--r--Misc/NEWS5
-rw-r--r--Python/import.c5
3 files changed, 16 insertions, 2 deletions
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
index 2abf5b2..4325005 100644
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -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
diff --git a/Misc/NEWS b/Misc/NEWS
index d0cc7a6..ba5ad65 100644
--- 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.
diff --git a/Python/import.c b/Python/import.c
index af47b0b..1d74faf 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -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;
}