Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07c6e71

Browse files
committed
Issue #15778: Coerce ImportError.args to a string when it isn't
already one. Patch by Dave Malcolm.
1 parent 491b1dc commit 07c6e71

3 files changed

Lines changed: 9 additions & 1 deletion

File tree

Lib/test/test_exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,11 @@ def test_attributes(self):
937937
self.assertEqual(exc.name, 'somename')
938938
self.assertEqual(exc.path, 'somepath')
939939

940+
def test_non_str_argument(self):
941+
# Issue #15778
942+
arg = b'abc'
943+
exc = ImportError(arg)
944+
self.assertEqual(str(arg), str(exc))
940945

941946

942947
def test_main():

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Release Candidate 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #15778: ensure that str(ImportError(msg)) returns a str
14+
even when msg isn't a str.
15+
1316
- Issue #2051: Source file permission bits are once again correctly
1417
copied to the cached bytecode file. (The migration to importlib
1518
reintroduced this problem because these was no regression test. A test

Objects/exceptions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ ImportError_traverse(PyImportErrorObject *self, visitproc visit, void *arg)
679679
static PyObject *
680680
ImportError_str(PyImportErrorObject *self)
681681
{
682-
if (self->msg) {
682+
if (self->msg && PyUnicode_CheckExact(self->msg)) {
683683
Py_INCREF(self->msg);
684684
return self->msg;
685685
}

0 commit comments

Comments
 (0)