]> granicus.if.org Git - python/commitdiff
Issue #15778: Coerce ImportError.args to a string when it isn't
authorBrett Cannon <brett@python.org>
Fri, 24 Aug 2012 17:05:09 +0000 (13:05 -0400)
committerBrett Cannon <brett@python.org>
Fri, 24 Aug 2012 17:05:09 +0000 (13:05 -0400)
already one.

Patch by Dave Malcolm.

Lib/test/test_exceptions.py
Misc/NEWS
Objects/exceptions.c

index 0b1fd1b8b9bc214df2fe75ceb3dc9c4129a44640..55e9db37803cbb38ad20fea02f513fff71e4e63e 100644 (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():
index 57f022c048f0c3ed832ec6d6eec83e36363aceaa..e772a7039355f9412e9b65283aee6f6db3d888d8 100644 (file)
--- 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
index b7e11f85bfebe2297631c58302757988031685e3..74bb26285ef2b86c503fd54b78d669bc07f6a6b1 100644 (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;
     }