summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-08-24 13:05:09 -0400
committerBrett Cannon <brett@python.org>2012-08-24 13:05:09 -0400
commit07c6e7168919c275e47fa35c741413270d3d80fd (patch)
tree5dd658c5cf3b17902ddaf1c9dcc0c7d34dfeb6a6
parent491b1dc79efd4d3fc39b0ded2cd3fd746154b882 (diff)
downloadcpython-git-07c6e7168919c275e47fa35c741413270d3d80fd.tar.gz
Issue #15778: Coerce ImportError.args to a string when it isn't
already one. Patch by Dave Malcolm.
-rw-r--r--Lib/test/test_exceptions.py5
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/exceptions.c2
3 files changed, 9 insertions, 1 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 0b1fd1b8b9..55e9db3780 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -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():
diff --git a/Misc/NEWS b/Misc/NEWS
index 57f022c048..e772a70393 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index b7e11f85bf..74bb26285e 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -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;
}