[Numpy-discussion] PATCH: arange leaks references to provided dtype

Lisandro Dalcin dalcinl@gmail....
Wed Jun 30 15:18:07 CDT 2010


Sorry, no time to make a proper bug report in the tracker.

This bug have been annoying me from about 3 years, today I finally
discovered the actual issue!!.

The code below shows that arange leaks references to dtype (actually,
PyArray_Descr instances).

import sys
import numpy as np

t = np.dtype('d')
while 1:
    a = np.arange(1,dtype=t)
    print (sys.getrefcount(t))


I see this in numpy 1.3.0 on py2.6 (Fedora 12). This is also present
in numpy svn/trunk on py2.7 and 3.1


The trivial patch below seems to fix this refcount bug:

Index: numpy/core/src/multiarray/multiarraymodule.c
===================================================================
--- numpy/core/src/multiarray/multiarraymodule.c	(revision 8467)
+++ numpy/core/src/multiarray/multiarraymodule.c	(working copy)
@@ -1863,7 +1863,7 @@

 static PyObject *
 array_arange(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kws) {
-    PyObject *o_start = NULL, *o_stop = NULL, *o_step = NULL;
+    PyObject *o_start = NULL, *o_stop = NULL, *o_step = NULL, *range;
     static char *kwd[]= {"start", "stop", "step", "dtype", NULL};
     PyArray_Descr *typecode = NULL;

@@ -1873,7 +1873,9 @@
         Py_XDECREF(typecode);
         return NULL;
     }
-    return PyArray_ArangeObj(o_start, o_stop, o_step, typecode);
+    range = PyArray_ArangeObj(o_start, o_stop, o_step, typecode);
+    Py_XDECREF(typecode);
+    return range;
 }

 /*NUMPY_API




-- 
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169


More information about the NumPy-Discussion mailing list