[Numpy-svn] r4911 - trunk/numpy/core/src

numpy-svn at scipy.org numpy-svn at scipy.org
Fri Mar 21 21:43:16 EDT 2008


Author: oliphant
Date: 2008-03-21 20:43:02 -0500 (Fri, 21 Mar 2008)
New Revision: 4911

Modified:
   trunk/numpy/core/src/arraymethods.c
   trunk/numpy/core/src/multiarraymodule.c
   trunk/numpy/core/src/scalartypes.inc.src
   trunk/numpy/core/src/ufuncobject.c
Log:
Fix reference count problems due to indiscriminate use of DescrConverter in PyArg_ParseTuple.  Tests need to be run to make sure this works.

Modified: trunk/numpy/core/src/arraymethods.c
===================================================================
--- trunk/numpy/core/src/arraymethods.c	2008-03-22 01:00:25 UTC (rev 4910)
+++ trunk/numpy/core/src/arraymethods.c	2008-03-22 01:43:02 UTC (rev 4911)
@@ -255,13 +255,16 @@
 array_getfield(PyArrayObject *self, PyObject *args, PyObject *kwds)
 {
 
-    PyArray_Descr *dtype;
+    PyArray_Descr *dtype=NULL;
     int offset = 0;
     static char *kwlist[] = {"dtype", "offset", 0};
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|i", kwlist,
                                      PyArray_DescrConverter,
-                                     &dtype, &offset)) return NULL;
+                                     &dtype, &offset)) {
+        Py_XDECREF(dtype);
+        return NULL;
+    }
 
     return PyArray_GetField(self, dtype, offset);
 }
@@ -302,14 +305,17 @@
 static PyObject *
 array_setfield(PyArrayObject *self, PyObject *args, PyObject *kwds)
 {
-    PyArray_Descr *dtype;
+    PyArray_Descr *dtype=NULL;
     int offset = 0;
     PyObject *value;
     static char *kwlist[] = {"value", "dtype", "offset", 0};
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&|i", kwlist,
                                      &value, PyArray_DescrConverter,
-                                     &dtype, &offset)) return NULL;
+                                     &dtype, &offset)) {
+        Py_XDECREF(dtype);
+        return NULL;
+    }
 
     if (PyArray_SetField(self, dtype, offset, value) < 0)
         return NULL;
@@ -649,7 +655,10 @@
     PyObject *obj;
 
     if (!PyArg_ParseTuple(args, "O&", PyArray_DescrConverter,
-                          &descr)) return NULL;
+                          &descr)) {
+        Py_XDECREF(descr);
+        return NULL;
+    }
 
     if (descr == self->descr) {
         obj = _ARET(PyArray_NewCopy(self,NPY_ANYORDER));
@@ -709,7 +718,10 @@
     PyObject *ret;
 
     if (!PyArg_ParseTuple(args, "|O&", PyArray_DescrConverter,
-                          &newtype)) return NULL;
+                          &newtype)) {
+        Py_XDECREF(newtype);
+        return NULL;
+    }
 
     /* convert to PyArray_Type */
     if (!PyArray_CheckExact(self)) {
@@ -1437,9 +1449,13 @@
                                      &axis, PyArray_DescrConverter2,
                                      &dtype,
                                      PyArray_OutputConverter,
-                                     &out)) return NULL;
+                                     &out)) {
+        Py_XDECREF(dtype);
+        return NULL;
+    }
 
     num = _get_type_num_double(self->descr, dtype);
+    Py_XDECREF(dtype);
     return PyArray_Mean(self, axis, num, out);
 }
 
@@ -1449,6 +1465,7 @@
     int axis=MAX_DIMS;
     PyArray_Descr *dtype=NULL;
     PyArrayObject *out=NULL;
+    int rtype;
     static char *kwlist[] = {"axis", "dtype", "out", NULL};
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
@@ -1456,9 +1473,14 @@
                                      &axis, PyArray_DescrConverter2,
                                      &dtype,
                                      PyArray_OutputConverter,
-                                     &out)) return NULL;
+                                     &out)) {
+        Py_XDECREF(dtype);
+        return NULL;
+    }
 
-    return PyArray_Sum(self, axis, _CHKTYPENUM(dtype), out);
+    rtype = _CHKTYPENUM(dtype);
+    Py_XDECREF(dtype);
+    return PyArray_Sum(self, axis, rtype, out);
 }
 
 
@@ -1468,6 +1490,7 @@
     int axis=MAX_DIMS;
     PyArray_Descr *dtype=NULL;
     PyArrayObject *out=NULL;
+    int rtype;
     static char *kwlist[] = {"axis", "dtype", "out", NULL};
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
@@ -1475,9 +1498,14 @@
                                      &axis, PyArray_DescrConverter2,
                                      &dtype,
                                      PyArray_OutputConverter,
-                                     &out)) return NULL;
+                                     &out)) {
+        Py_XDECREF(dtype);
+        return NULL;
+    }
 
-    return PyArray_CumSum(self, axis, _CHKTYPENUM(dtype), out);
+    rtype = _CHKTYPENUM(dtype);
+    Py_XDECREF(dtype);
+    return PyArray_CumSum(self, axis, rtype, out);
 }
 
 static PyObject *
@@ -1486,6 +1514,7 @@
     int axis=MAX_DIMS;
     PyArray_Descr *dtype=NULL;
     PyArrayObject *out=NULL;
+    int rtype;
     static char *kwlist[] = {"axis", "dtype", "out", NULL};
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
@@ -1493,9 +1522,14 @@
                                      &axis, PyArray_DescrConverter2,
                                      &dtype,
                                      PyArray_OutputConverter,
-                                     &out)) return NULL;
+                                     &out)) {
+        Py_XDECREF(dtype);
+        return NULL;
+    }
 
-    return PyArray_Prod(self, axis, _CHKTYPENUM(dtype), out);
+    rtype = _CHKTYPENUM(dtype);
+    Py_XDECREF(dtype);
+    return PyArray_Prod(self, axis, rtype, out);
 }
 
 static PyObject *
@@ -1504,6 +1538,7 @@
     int axis=MAX_DIMS;
     PyArray_Descr *dtype=NULL;
     PyArrayObject *out=NULL;
+    int rtype;
     static char *kwlist[] = {"axis", "dtype", "out", NULL};
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
@@ -1511,9 +1546,14 @@
                                      &axis, PyArray_DescrConverter2,
                                      &dtype,
                                      PyArray_OutputConverter,
-                                     &out)) return NULL;
+                                     &out)) {
+        Py_XDECREF(dtype);
+        return NULL;
+    }
 
-    return PyArray_CumProd(self, axis, _CHKTYPENUM(dtype), out);
+    rtype = _CHKTYPENUM(dtype);
+    Py_XDECREF(dtype);
+    return PyArray_CumProd(self, axis, rtype, out);
 }
 
 
@@ -1571,9 +1611,13 @@
                                      &axis, PyArray_DescrConverter2,
                                      &dtype,
                                      PyArray_OutputConverter,
-                                     &out, &ddof)) return NULL;
+                                     &out, &ddof)) {
+        Py_XDECREF(dtype);
+        return NULL;
+    }
 
     num = _get_type_num_double(self->descr, dtype);
+    Py_XDECREF(dtype);
     return __New_PyArray_Std(self, axis, num, out, 0, ddof);
 }
 
@@ -1593,9 +1637,13 @@
                                      &axis, PyArray_DescrConverter2,
                                      &dtype,
                                      PyArray_OutputConverter,
-                                     &out, &ddof)) return NULL;
+                                     &out, &ddof)) {
+        Py_XDECREF(dtype);
+        return NULL;
+    } 
 
     num = _get_type_num_double(self->descr, dtype);
+    Py_XDECREF(dtype);
     return __New_PyArray_Std(self, axis, num, out, 1, ddof);
 }
 
@@ -1633,16 +1681,22 @@
     int axis1=0, axis2=1, offset=0;
     PyArray_Descr *dtype=NULL;
     PyArrayObject *out=NULL;
+    int rtype;
     static char *kwlist[] = {"offset", "axis1", "axis2", "dtype", "out", NULL};
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iiiO&O&", kwlist,
                                      &offset, &axis1, &axis2,
                                      PyArray_DescrConverter2, &dtype,
-                                     PyArray_OutputConverter, &out))
+                                     PyArray_OutputConverter, &out)) {
+        Py_XDECREF(dtype);
         return NULL;
+    }
 
+    rtype = _CHKTYPENUM(dtype);
+    Py_XDECREF(dtype);
+
     return _ARET(PyArray_Trace(self, offset, axis1, axis2,
-                               _CHKTYPENUM(dtype), out));
+                               rtype, out));
 }
 
 #undef _CHKTYPENUM

Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c	2008-03-22 01:00:25 UTC (rev 4910)
+++ trunk/numpy/core/src/multiarraymodule.c	2008-03-22 01:43:02 UTC (rev 4911)
@@ -5633,8 +5633,11 @@
                                     PyArray_BoolConverter, &copy,
                                     PyArray_OrderConverter, &order,
                                     PyArray_BoolConverter, &subok,
-                                    &ndmin))
-        return NULL;
+                                    &ndmin)) {
+            Py_XDECREF(type);
+            return NULL;
+    }
+            
 
     /* fast exit if simple call */
     if ((subok && PyArray_Check(op)) ||
@@ -5688,9 +5691,11 @@
 
     flags |= NPY_FORCECAST;
 
+    Py_XINCREF(type);
     ret = PyArray_CheckFromAny(op, type, 0, 0, flags, NULL);
 
  finish:
+    Py_XDECREF(type);
     if (!ret || (nd=PyArray_NDIM(ret)) >= ndmin) return ret;
     /* create a new array from the same data with ones in the shape */
     /* steals a reference to ret */
@@ -5748,8 +5753,9 @@
     return ret;
 
  fail:
+    Py_XDECREF(typecode);
     PyDimMem_FREE(shape.ptr);
-    return ret;
+    return NULL;
 }
 
 /* This function is needed for supporting Pickles of 
@@ -5876,6 +5882,7 @@
     return ret;
 
  fail:
+    Py_XDECREF(typecode);
     PyDimMem_FREE(shape.ptr);
     return ret;
 }
@@ -6217,6 +6224,7 @@
                                      &data, &s,
                                      PyArray_DescrConverter, &descr,
                                      &nin, &sep)) {
+        Py_XDECREF(descr);
         return NULL;
     }
 
@@ -6350,11 +6358,10 @@
                                      &file,
                                      PyArray_DescrConverter, &type,
                                      &nin, &sep)) {
+        Py_XDECREF(type);
         return NULL;
     }
 
-    if (type == NULL) type = PyArray_DescrFromType(PyArray_DEFAULT);
-
     if (PyString_Check(file) || PyUnicode_Check(file)) {
         file = PyObject_CallFunction((PyObject *)&PyFile_Type,
                                      "Os", file, "rb");
@@ -6363,6 +6370,7 @@
     else {
         Py_INCREF(file);
     }
+
     fp = PyFile_AsFile(file);
     if (fp == NULL) {
         PyErr_SetString(PyExc_IOError,
@@ -6370,6 +6378,9 @@
         Py_DECREF(file);
         return NULL;
     }
+
+    if (type == NULL) type = PyArray_DescrFromType(PyArray_DEFAULT);
+
     ret = PyArray_FromFile(fp, type, (intp) nin, sep);
     Py_DECREF(file);
     return ret;
@@ -6480,6 +6491,7 @@
                                      &iter,
                                      PyArray_DescrConverter, &descr,
                                      &nin)) {
+        Py_XDECREF(descr);
         return NULL;
     }
 
@@ -6598,6 +6610,7 @@
                                      &obj,
                                      PyArray_DescrConverter, &type,
                                      &nin, &offset)) {
+        Py_XDECREF(type);
         return NULL;
     }
     if (type==NULL)
@@ -6882,8 +6895,10 @@
     if(!PyArg_ParseTupleAndKeywords(args, kws, "O|OOO&", kwd, &o_start,
                                     &o_stop, &o_step,
                                     PyArray_DescrConverter2,
-                                    &typecode))
+                                    &typecode)) {
+        Py_XDECREF(typecode);
         return NULL;
+    }
 
     return PyArray_ArangeObj(o_start, o_stop, o_step, typecode);
 }
@@ -7059,23 +7074,28 @@
     PyArray_Descr *d1=NULL;
     PyArray_Descr *d2=NULL;
     Bool ret;
-    PyObject *retobj;
+    PyObject *retobj=NULL;
     static char *kwlist[] = {"from", "to", NULL};
 
     if(!PyArg_ParseTupleAndKeywords(args, kwds, "O&O&", kwlist,
                                     PyArray_DescrConverter, &d1,
-                                    PyArray_DescrConverter, &d2))
-        return NULL;
+                                    PyArray_DescrConverter, &d2)) {
+        goto finish;
+    }
     if (d1 == NULL || d2 == NULL) {
         PyErr_SetString(PyExc_TypeError,
                         "did not understand one of the types; " \
                         "'None' not accepted");
-        return NULL;
+        goto finish;
     }
 
     ret = PyArray_CanCastTo(d1, d2);
     retobj = (ret ? Py_True : Py_False);
     Py_INCREF(retobj);
+
+ finish:    
+    Py_XDECREF(d1);
+    Py_XDECREF(d2);
     return retobj;
 }
 

Modified: trunk/numpy/core/src/scalartypes.inc.src
===================================================================
--- trunk/numpy/core/src/scalartypes.inc.src	2008-03-22 01:00:25 UTC (rev 4910)
+++ trunk/numpy/core/src/scalartypes.inc.src	2008-03-22 01:43:02 UTC (rev 4911)
@@ -1075,7 +1075,10 @@
         PyObject *ret;
 
         if (!PyArg_ParseTuple(args, "|O&", &PyArray_DescrConverter,
-                              &outcode)) return NULL;
+                              &outcode)) {
+                Py_XDECREF(outcode);
+                return NULL;
+        }
         ret = PyArray_FromScalar(scalar, outcode);
         return ret;
 }
@@ -1214,7 +1217,7 @@
 static PyObject *
 voidtype_setfield(PyVoidScalarObject *self, PyObject *args, PyObject *kwds)
 {
-        PyArray_Descr *typecode;
+        PyArray_Descr *typecode=NULL;
         int offset = 0;
         PyObject *value, *src;
         int mysize;
@@ -1229,7 +1232,10 @@
         if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&|i", kwlist,
                                          &value,
                                          PyArray_DescrConverter,
-                                         &typecode, &offset)) return NULL;
+                                         &typecode, &offset)) {
+                Py_XDECREF(typecode);
+                return NULL;
+        }
 
         mysize = self->ob_size;
 
@@ -1250,6 +1256,7 @@
                 temp = (PyObject **)dptr;
                 Py_XDECREF(*temp);
                 memcpy(temp, &value, sizeof(PyObject *));
+                Py_DECREF(typecode);
         }
         else {
                 /* Copy data from value to correct place in dptr */

Modified: trunk/numpy/core/src/ufuncobject.c
===================================================================
--- trunk/numpy/core/src/ufuncobject.c	2008-03-22 01:00:25 UTC (rev 4910)
+++ trunk/numpy/core/src/ufuncobject.c	2008-03-22 01:43:02 UTC (rev 4911)
@@ -2832,10 +2832,13 @@
                                         PyArray_DescrConverter2,
                                         &otype,
                                         PyArray_OutputConverter,
-                                        &out)) return NULL;
+                                        &out)) {
+            Py_XDECREF(otype);
+            return NULL;
+        }
         indices = (PyArrayObject *)PyArray_FromAny(obj_ind, indtype,
                                                    1, 1, CARRAY, NULL);
-        if (indices == NULL) return NULL;
+        if (indices == NULL) {Py_XDECREF(otype); return NULL;}
     }
     else {
         if(!PyArg_ParseTupleAndKeywords(args, kwds, "O|iO&O&", kwlist1,
@@ -2843,7 +2846,10 @@
                                         PyArray_DescrConverter2,
                                         &otype,
                                         PyArray_OutputConverter,
-                                        &out)) return NULL;
+                                        &out)) {
+            Py_XDECREF(otype);
+            return NULL;
+        }
     }
 
     /* Ensure input is an array */
@@ -2861,6 +2867,7 @@
     if (mp->nd == 0) {
         PyErr_Format(PyExc_TypeError, "cannot %s on a scalar",
                      _reduce_type[operation]);
+        Py_XDECREF(otype);
         Py_DECREF(mp);
         return NULL;
     }
@@ -2871,6 +2878,7 @@
         PyErr_Format(PyExc_TypeError,
                      "cannot perform %s with flexible type",
                      _reduce_type[operation]);
+        Py_XDECREF(otype);
         Py_DECREF(mp);
         return NULL;
     }
@@ -2878,6 +2886,7 @@
     if (axis < 0) axis += mp->nd;
     if (axis < 0 || axis >= mp->nd) {
         PyErr_SetString(PyExc_ValueError, "axis not in array");
+        Py_XDECREF(otype);
         Py_DECREF(mp);
         return NULL;
     }




More information about the Numpy-svn mailing list