[Python-checkins] r46510 - python/trunk/Objects/exceptions.c

georg.brandl python-checkins at python.org
Sun May 28 22:57:10 CEST 2006


Author: georg.brandl
Date: Sun May 28 22:57:09 2006
New Revision: 46510

Modified:
   python/trunk/Objects/exceptions.c
Log:
Fix refleaks in UnicodeError get and set methods.



Modified: python/trunk/Objects/exceptions.c
==============================================================================
--- python/trunk/Objects/exceptions.c	(original)
+++ python/trunk/Objects/exceptions.c	Sun May 28 22:57:09 2006
@@ -1,3 +1,9 @@
+/*
+ * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
+ *
+ * Thanks go to Tim Peters and Michael Hudson for debugging.
+ */
+
 #define PY_SSIZE_T_CLEAN
 #include <Python.h>
 #include "structmember.h"
@@ -1037,56 +1043,57 @@
 {
     PyObject *str;
     PyObject *result;
+    int have_filename = 0;
+    int have_lineno = 0;
+    char *buffer = NULL;
 
     if (self->msg)
         str = PyObject_Str(self->msg);
     else
         str = PyObject_Str(Py_None);
-    result = str;
+    if (!str) return NULL;
+    /* Don't fiddle with non-string return (shouldn't happen anyway) */
+    if (!PyString_Check(str)) return str;
 
     /* XXX -- do all the additional formatting with filename and
        lineno here */
 
-    if (str != NULL && PyString_Check(str)) {
-        int have_filename = 0;
-        int have_lineno = 0;
-        char *buffer = NULL;
-
-        have_filename = (self->filename != NULL) &&
-            PyString_Check(self->filename);
-        have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
-
-        if (have_filename || have_lineno) {
-            Py_ssize_t bufsize = PyString_GET_SIZE(str) + 64;
-            if (have_filename)
-                bufsize += PyString_GET_SIZE(self->filename);
-
-            buffer = (char *)PyMem_MALLOC(bufsize);
-            if (buffer != NULL) {
-                if (have_filename && have_lineno)
-                    PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
-                        PyString_AS_STRING(str),
-                        my_basename(PyString_AS_STRING(self->filename)),
-                        PyInt_AsLong(self->lineno));
-                else if (have_filename)
-                    PyOS_snprintf(buffer, bufsize, "%s (%s)",
-                        PyString_AS_STRING(str),
-                        my_basename(PyString_AS_STRING(self->filename)));
-                else if (have_lineno)
-                    PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
-                        PyString_AS_STRING(str),
-                        PyInt_AsLong(self->lineno));
-
-                result = PyString_FromString(buffer);
-                PyMem_FREE(buffer);
-
-                if (result == NULL)
-                    result = str;
-                else
-                    Py_DECREF(str);
-            }
-        }
-    }
+    have_filename = (self->filename != NULL) &&
+        PyString_Check(self->filename);
+    have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);
+
+    if (!have_filename && !have_lineno)
+        return str;
+
+    Py_ssize_t bufsize = PyString_GET_SIZE(str) + 64;
+    if (have_filename)
+        bufsize += PyString_GET_SIZE(self->filename);
+
+    buffer = PyMem_MALLOC(bufsize);
+    if (buffer == NULL)
+        return str;
+
+    if (have_filename && have_lineno)
+        PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
+            PyString_AS_STRING(str),
+            my_basename(PyString_AS_STRING(self->filename)),
+            PyInt_AsLong(self->lineno));
+    else if (have_filename)
+        PyOS_snprintf(buffer, bufsize, "%s (%s)",
+            PyString_AS_STRING(str),
+            my_basename(PyString_AS_STRING(self->filename)));
+    else /* only have_lineno */
+        PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
+            PyString_AS_STRING(str),
+            PyInt_AsLong(self->lineno));
+
+    result = PyString_FromString(buffer);
+    PyMem_FREE(buffer);
+
+    if (result == NULL)
+        result = str;
+    else
+        Py_DECREF(str);
     return result;
 }
 
@@ -1208,7 +1215,7 @@
     PyObject *obj = PyInt_FromSsize_t(value);
     if (!obj)
         return -1;
-    Py_XDECREF(*attr);
+    Py_CLEAR(*attr);
     *attr = obj;
     return 0;
 }
@@ -1236,7 +1243,7 @@
     PyObject *obj = PyString_FromString(value);
     if (!obj)
         return -1;
-    Py_XDECREF(*attr);
+    Py_CLEAR(*attr);
     *attr = obj;
     return 0;
 }
@@ -1302,6 +1309,7 @@
             *start = 0; /*XXX check for values <0*/
         if (*start>=size)
             *start = size-1;
+        Py_DECREF(obj);
         return 0;
     }
     return -1;
@@ -1321,6 +1329,7 @@
             *start = 0;
         if (*start>=size)
             *start = size-1;
+        Py_DECREF(obj);
         return 0;
     }
     return -1;
@@ -1368,6 +1377,7 @@
             *end = 1;
         if (*end>size)
             *end = size;
+        Py_DECREF(obj);
         return 0;
     }
     return -1;
@@ -1387,6 +1397,7 @@
             *end = 1;
         if (*end>size)
             *end = size;
+        Py_DECREF(obj);
         return 0;
     }
     return -1;
@@ -1630,8 +1641,8 @@
 static PyObject *
 UnicodeDecodeError_str(PyObject *self)
 {
-    Py_ssize_t start;
-    Py_ssize_t end;
+    Py_ssize_t start = 0;
+    Py_ssize_t end = 0;
 
     if (PyUnicodeDecodeError_GetStart(self, &start))
     return NULL;


More information about the Python-checkins mailing list