[issue7309] crasher in str(Exception())

Eric Smith report at bugs.python.org
Sat Nov 14 12:04:49 CET 2009


Eric Smith <eric at trueblade.com> added the comment:

For some reason I'm not able to attach the patch file. I'll look at
that, but in the meantime here's the preliminary patch against trunk:
Index: Objects/exceptions.c
===================================================================
--- Objects/exceptions.c        (revision 76258)
+++ Objects/exceptions.c        (working copy)
@@ -1779,7 +1779,13 @@
 UnicodeTranslateError_str(PyObject *self)
 {
     PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
+    PyObject *result = NULL;
+    PyObject *reason_str = NULL;
 
+    reason_str = PyObject_Str(uself->reason);
+    if (reason_str == NULL)
+        goto done;
+
     if (uself->end==uself->start+1) {
         int badchar =
(int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
         char badchar_str[20];
@@ -1789,19 +1795,22 @@
             PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x",
badchar);
         else
             PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x",
badchar);
-        return PyString_FromFormat(
+        result = PyString_FromFormat(
             "can't translate character u'\\%s' in position %zd: %.400s",
             badchar_str,
             uself->start,
-            PyString_AS_STRING(uself->reason)
+            PyString_AS_STRING(reason_str)
         );
-    }
-    return PyString_FromFormat(
-        "can't translate characters in position %zd-%zd: %.400s",
-        uself->start,
-        uself->end-1,
-        PyString_AS_STRING(uself->reason)
-    );
+    } else
+        result = PyString_FromFormat(
+            "can't translate characters in position %zd-%zd: %.400s",
+            uself->start,
+            uself->end-1,
+            PyString_AS_STRING(reason_str)
+        );
+done:
+    Py_XDECREF(reason_str);
+    return result;
 }
 
 static PyTypeObject _PyExc_UnicodeTranslateError = {

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7309>
_______________________________________


More information about the Python-bugs-list mailing list