[issue2517] Error when printing an exception containing a Unicode string

Amaury Forgeot d'Arc report at bugs.python.org
Tue Apr 1 01:10:49 CEST 2008


Amaury Forgeot d'Arc <amauryfa at gmail.com> added the comment:

Even in 2.5, __str__ is allowed to return a Unicode object;
we could change BaseException_str this way:

Index: exceptions.c
===================================================================
--- exceptions.c	(revision 61957)
+++ exceptions.c	(working copy)
@@ -108,6 +104,11 @@
         break;
     case 1:
         out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
+        if (out == NULL &&
PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
+        {
+            PyErr_Clear();
+            out = PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0));
+        }
         break;
     default:
         out = PyObject_Str(self->args);

Then str(e) still raises UnicodeEncodeError,
but unicode(e) returns the original message.

But I would like the opinion of an experimented core developer...

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2517>
__________________________________


More information about the Python-bugs-list mailing list