diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -937,6 +937,10 @@ self.assertEqual(exc.name, 'somename') self.assertEqual(exc.path, 'somepath') + # Issue #15778: ensure that str(ImportError(msg)) returns a str + # even when msg isn't a str: + exc = ImportError(b'test') + self.assertEqual(str(exc), "b'test'") def test_main(): diff --git a/Misc/NEWS b/Misc/NEWS --- a/Misc/NEWS +++ b/Misc/NEWS @@ -26,6 +26,9 @@ - Issue #14846: importlib.FileFinder now handles the case where the directory being searched is removed after a previous import attempt +- Issue #15778: ensure that str(ImportError(msg)) returns a str + even when msg isn't a str + Library ------- diff --git a/Objects/exceptions.c b/Objects/exceptions.c --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -679,7 +679,7 @@ static PyObject * ImportError_str(PyImportErrorObject *self) { - if (self->msg) { + if (self->msg && PyUnicode_CheckExact(self->msg)) { Py_INCREF(self->msg); return self->msg; }