[Numpy-discussion] Ticket review: #848, leak in PyArray_DescrFromType

Michael Abbott michael@araneidae.co...
Tue Jul 15 02:48:38 CDT 2008


Only half of my patch for this bug has gone into trunk, and without the 
rest of my patch there remains a leak.

Furthermore, it remains necessary to perform an extra INCREF on typecode 
before calling PyArray_FromAny ... as otherwise there is the real 
possibility that typecode will have evaporated by the time it's passed to 
scalar_value later down in the routine.

I attach the patch I believe remains necessary against this routine:


--- numpy/core/src/scalartypes.inc.src  (revision 5411)
+++ numpy/core/src/scalartypes.inc.src  (working copy)
@@ -1925,19 +1925,30 @@
         goto finish;
     }

+    Py_XINCREF(typecode);
     arr = PyArray_FromAny(obj, typecode, 0, 0, FORCECAST, NULL);
-    if ((arr==NULL) || (PyArray_NDIM(arr) > 0)) return arr;
+    if ((arr==NULL) || (PyArray_NDIM(arr) > 0)) {
+        Py_XDECREF(typecode);
+        return arr;
+    }
     robj = PyArray_Return((PyArrayObject *)arr);

 finish:
-    if ((robj==NULL) || (robj->ob_type == type)) return robj;
+    if ((robj==NULL) || (robj->ob_type == type)) {
+        Py_XDECREF(typecode);
+        return robj;
+    }
     /* Need to allocate new type and copy data-area over */
     if (type->tp_itemsize) {
         itemsize = PyString_GET_SIZE(robj);
     }
     else itemsize = 0;
     obj = type->tp_alloc(type, itemsize);
-    if (obj == NULL) {Py_DECREF(robj); return NULL;}
+    if (obj == NULL) {
+        Py_XDECREF(typecode);
+        Py_DECREF(robj);
+        return NULL;
+    }
     if (typecode==NULL)
         typecode = PyArray_DescrFromType(PyArray_@TYPE@);
     dest = scalar_value(obj, typecode);


I can see that there might be an argument that PyArray_FromAny has the 
semantics that it retains a reference to typecode unless it returns NULL 
... but I don't want to go there.  That would not be a good thing to rely 
on -- and even with those semantics the existing code still needs fixing.


More information about the Numpy-discussion mailing list