Issue #15778: Coerce ImportError.args to a string when it isn't

already one.

Patch by Dave Malcolm.
This commit is contained in:
Brett Cannon 2012-08-24 13:05:09 -04:00
parent 491b1dc79e
commit 07c6e71689
3 changed files with 9 additions and 1 deletions

View File

@ -937,6 +937,11 @@ class ImportErrorTests(unittest.TestCase):
self.assertEqual(exc.name, 'somename')
self.assertEqual(exc.path, 'somepath')
def test_non_str_argument(self):
# Issue #15778
arg = b'abc'
exc = ImportError(arg)
self.assertEqual(str(arg), str(exc))
def test_main():

View File

@ -10,6 +10,9 @@ What's New in Python 3.3.0 Release Candidate 1?
Core and Builtins
-----------------
- Issue #15778: ensure that str(ImportError(msg)) returns a str
even when msg isn't a str.
- Issue #2051: Source file permission bits are once again correctly
copied to the cached bytecode file. (The migration to importlib
reintroduced this problem because these was no regression test. A test

View File

@ -679,7 +679,7 @@ ImportError_traverse(PyImportErrorObject *self, visitproc visit, void *arg)
static PyObject *
ImportError_str(PyImportErrorObject *self)
{
if (self->msg) {
if (self->msg && PyUnicode_CheckExact(self->msg)) {
Py_INCREF(self->msg);
return self->msg;
}