[Python-checkins] cpython: Issue #15778: Coerce ImportError.args to a string when it isn't

brett.cannon python-checkins at python.org
Fri Aug 24 19:08:35 CEST 2012


http://hg.python.org/cpython/rev/91909962d7f5
changeset:   78737:91909962d7f5
parent:      78735:c93fbc2caba5
user:        Brett Cannon <brett at python.org>
date:        Fri Aug 24 13:05:09 2012 -0400
summary:
  Issue #15778: Coerce ImportError.args to a string when it isn't
already one.

Patch by Dave Malcolm.

files:
  Lib/test/test_exceptions.py |  5 +++++
  Misc/NEWS                   |  3 +++
  Objects/exceptions.c        |  2 +-
  3 files changed, 9 insertions(+), 1 deletions(-)


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,11 @@
         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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 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
--- 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;
     }

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list