diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py index c17da10..e4d6523 100644 --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -397,6 +397,13 @@ class ImportTests(unittest.TestCase): finally: sys.path.pop(0) + def test_fromlist_error_messages(self): + # Test for issue #21720: fromlist unicode error messages + try: + __import__('encodings', fromlist=[u'aliases']) + except TypeError as exc: + self.assertIn("must be str, not unicode", str(exc)) + 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..0053720 100644 --- a/Python/import.c +++ b/Python/import.c @@ -2566,6 +2566,12 @@ ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen, } return 0; } + if (PyUnicode_Check(item)) { + PyErr_SetString(PyExc_TypeError, + "Item in ``from list'' must be str, not unicode"); + Py_DECREF(item); + return 0; + } if (!PyString_Check(item)) { PyErr_SetString(PyExc_TypeError, "Item in ``from list'' not a string");