diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py index c17da10..5582ba8 100644 --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -397,6 +397,16 @@ class ImportTests(unittest.TestCase): finally: sys.path.pop(0) + def test_fromlist_error_messages(self): + # Test for issue #21720: fromlist unicode error messages + 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("not a string", str(cm.exception)) + class PycRewritingTests(unittest.TestCase): # Test that the `co_filename` attribute on code objects always points diff --git a/Python/import.c b/Python/import.c index 92363b3..263ecf0 100644 --- a/Python/import.c +++ b/Python/import.c @@ -2567,8 +2567,16 @@ 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"); + if (PyUnicode_Check(item)) { + PyErr_SetString( + PyExc_TypeError, + "Item in ``from list'' must be str, not unicode"); + } + else { + PyErr_SetString( + PyExc_TypeError, + "Item in ``from list'' not a string"); + } Py_DECREF(item); return 0; }