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

numpy-svn at scipy.org numpy-svn at scipy.org
Fri Mar 21 19:55:09 EDT 2008


Author: oliphant
Date: 2008-03-21 18:54:58 -0500 (Fri, 21 Mar 2008)
New Revision: 4907

Modified:
   trunk/numpy/core/src/arrayobject.c
   trunk/numpy/core/src/arraytypes.inc.src
Log:
Fix ticket #711 by more carefully guarding object array reference counting.

Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c	2008-03-21 19:34:19 UTC (rev 4906)
+++ trunk/numpy/core/src/arrayobject.c	2008-03-21 23:54:58 UTC (rev 4907)
@@ -784,6 +784,7 @@
             dstride = nbytes;
 
         /* Refcount note: src and dest may have different sizes */
+        PyArray_INCREF(src);
         PyArray_XDECREF(dest);
 
         NPY_BEGIN_THREADS
@@ -794,7 +795,8 @@
 
         NPY_END_THREADS
 
-	PyArray_INCREF(dest);
+        PyArray_INCREF(dest);
+        PyArray_XDECREF(src);
     }
     else {
         PyArrayIterObject *dit;
@@ -803,8 +805,8 @@
             PyArray_IterAllButAxis((PyObject *)dest, &axis);
         if (dit == NULL) goto finish;
         /* Refcount note: src and dest may have different sizes */
+        PyArray_INCREF(src);
         PyArray_XDECREF(dest);
-
         NPY_BEGIN_THREADS
             while(dit->index < dit->size) {
                 myfunc(dit->dataptr, PyArray_STRIDE(dest, axis),
@@ -817,9 +819,9 @@
                 PyArray_ITER_NEXT(dit);
             }
         NPY_END_THREADS
-
-	PyArray_INCREF(dest);
-	Py_DECREF(dit);
+        PyArray_INCREF(dest);
+        PyArray_XDECREF(src);
+        Py_DECREF(dit);
     }
     retval = 0;
  finish:
@@ -843,12 +845,13 @@
 
 
     if (PyArray_NDIM(src) == 0) {
-	PyArray_XDECREF((PyArrayObject *)dst);
-	NPY_BEGIN_THREADS
-	memcpy(PyArray_BYTES(dst), PyArray_BYTES(src),
-	       PyArray_ITEMSIZE(src));
+        /* Refcount note: src and dst have the same size */
+        PyArray_INCREF((PyArrayObject *)src);
+        PyArray_XDECREF((PyArrayObject *)dst);
+        NPY_BEGIN_THREADS
+        memcpy(PyArray_BYTES(dst), PyArray_BYTES(src),
+               PyArray_ITEMSIZE(src));
         NPY_END_THREADS
-	PyArray_INCREF((PyArrayObject *)dst);
         return 0;
     }
 
@@ -873,19 +876,19 @@
     elsize = PyArray_ITEMSIZE(dst);
     nbytes = elsize * PyArray_DIM(src, axis);
     
+    /* Refcount note: src and dst have the same size */
+    PyArray_INCREF((PyArrayObject *)src);
     PyArray_XDECREF((PyArrayObject *)dst);
     NPY_BEGIN_THREADS
-        while(it->index < it->size) {
-            myfunc(dptr, elsize, it->dataptr,
-                   PyArray_STRIDE(src,axis),
-                   PyArray_DIM(src,axis), elsize);
-            dptr += nbytes;
-            PyArray_ITER_NEXT(it);
-        }
+    while(it->index < it->size) {
+        myfunc(dptr, elsize, it->dataptr,
+               PyArray_STRIDE(src,axis),
+               PyArray_DIM(src,axis), elsize);
+        dptr += nbytes;
+        PyArray_ITER_NEXT(it);
+    }
     NPY_END_THREADS
 
-    PyArray_INCREF((PyArrayObject *)dst);
-
     Py_DECREF(it);
     return 0;
 }
@@ -915,27 +918,27 @@
     }
     elsize = PyArray_ITEMSIZE(dest);
 
+    /* Refcount note: src and dst have the same size */
+    PyArray_INCREF(src);
     PyArray_XDECREF(dest);
 
     NPY_BEGIN_THREADS
-        while(dit->index < dit->size) {
-            /* strided copy of elsize bytes */
-            myfunc(dit->dataptr, dest->strides[maxaxis],
-                   sit->dataptr, src->strides[maxaxis],
-                   maxdim, elsize);
-            if (swap) {
-                _strided_byte_swap(dit->dataptr,
-                                   dest->strides[maxaxis],
-                                   dest->dimensions[maxaxis],
-                                   elsize);
-            }
-            PyArray_ITER_NEXT(dit);
-            PyArray_ITER_NEXT(sit);
+    while(dit->index < dit->size) {
+        /* strided copy of elsize bytes */
+        myfunc(dit->dataptr, dest->strides[maxaxis],
+               sit->dataptr, src->strides[maxaxis],
+               maxdim, elsize);
+        if (swap) {
+            _strided_byte_swap(dit->dataptr,
+                               dest->strides[maxaxis],
+                               dest->dimensions[maxaxis],
+                               elsize);
         }
+        PyArray_ITER_NEXT(dit);
+        PyArray_ITER_NEXT(sit);
+    }
     NPY_END_THREADS
 
-    PyArray_INCREF(dest);
-
     Py_DECREF(sit);
     Py_DECREF(dit);
     return 0;
@@ -951,7 +954,7 @@
     int maxaxis; intp maxdim;
     NPY_BEGIN_THREADS_DEF
 
-        elsize = PyArray_ITEMSIZE(dest);
+    elsize = PyArray_ITEMSIZE(dest);
     multi = (PyArrayMultiIterObject *)PyArray_MultiIterNew(2, dest, src);
     if (multi == NULL) return -1;
 
@@ -965,10 +968,11 @@
 
     maxaxis = PyArray_RemoveSmallest(multi);
     if (maxaxis < 0) { /* copy 1 0-d array to another */
+        /* Refcount note: src and dst have the same size */
+        PyArray_INCREF(src);
         PyArray_XDECREF(dest);
         memcpy(dest->data, src->data, elsize);
         if (swap) byte_swap_vector(dest->data, 1, elsize);
-        PyArray_INCREF(dest);
         return 0;
     }
     maxdim = multi->dimensions[maxaxis];
@@ -976,25 +980,28 @@
     /* Increment the source and decrement the destination
        reference counts
     */
+    /* Refcount note: src and dest may have different sizes */
+    PyArray_INCREF(src);
     PyArray_XDECREF(dest);
 
     NPY_BEGIN_THREADS
-        while(multi->index < multi->size) {
-            myfunc(multi->iters[0]->dataptr,
-                   multi->iters[0]->strides[maxaxis],
-                   multi->iters[1]->dataptr,
-                   multi->iters[1]->strides[maxaxis],
-                   maxdim, elsize);
-            if (swap) {
-                _strided_byte_swap(multi->iters[0]->dataptr,
-                                   multi->iters[0]->strides[maxaxis],
-                                   maxdim, elsize);
-            }
-            PyArray_MultiIter_NEXT(multi);
+    while(multi->index < multi->size) {
+        myfunc(multi->iters[0]->dataptr,
+               multi->iters[0]->strides[maxaxis],
+               multi->iters[1]->dataptr,
+               multi->iters[1]->strides[maxaxis],
+               maxdim, elsize);
+        if (swap) {
+            _strided_byte_swap(multi->iters[0]->dataptr,
+                               multi->iters[0]->strides[maxaxis],
+                               maxdim, elsize);
         }
+        PyArray_MultiIter_NEXT(multi);
+    }
     NPY_END_THREADS
 
     PyArray_INCREF(dest);
+    PyArray_XDECREF(src);
     
     Py_DECREF(multi);
     return 0;
@@ -1036,15 +1043,16 @@
                       (PyArray_ISFARRAY_RO(src) && PyArray_ISFARRAY(dest)));
 
     if (simple) {
+        /* Refcount note: src and dest have the same size */
+        PyArray_INCREF(src);
         PyArray_XDECREF(dest);
         NPY_BEGIN_THREADS
-            if (usecopy)
-                memcpy(dest->data, src->data, PyArray_NBYTES(dest));
-            else
-                memmove(dest->data, src->data, PyArray_NBYTES(dest));
-        NPY_END_THREADS       
-	PyArray_INCREF(dest);
-	return 0;
+        if (usecopy)
+            memcpy(dest->data, src->data, PyArray_NBYTES(dest));
+        else
+            memmove(dest->data, src->data, PyArray_NBYTES(dest));
+        NPY_END_THREADS
+        return 0;
     }
 
     swap = PyArray_ISNOTSWAPPED(dest) != PyArray_ISNOTSWAPPED(src);
@@ -1109,12 +1117,12 @@
 
     if (simple) {
         /* Refcount note: src and dest have the same size */
+        PyArray_INCREF(src);
         PyArray_XDECREF(dest);
         NPY_BEGIN_THREADS
-            memcpy(dest->data, src->data, PyArray_NBYTES(dest));
+        memcpy(dest->data, src->data, PyArray_NBYTES(dest));
         NPY_END_THREADS
-        PyArray_INCREF(dest);
-	return 0;
+        return 0;
     }
 
     if (PyArray_SAMESHAPE(dest, src)) {
@@ -1135,6 +1143,8 @@
     isrc = (PyArrayIterObject *)PyArray_IterNew((PyObject *)src);
     if (isrc == NULL) {Py_DECREF(idest); return -1;}
     elsize = dest->descr->elsize;
+    /* Refcount note: src and dest have the same size */
+    PyArray_INCREF(src);
     PyArray_XDECREF(dest);
     NPY_BEGIN_THREADS
         while(idest->index < idest->size) {
@@ -1143,7 +1153,6 @@
             PyArray_ITER_NEXT(isrc);
         }
     NPY_END_THREADS
-    PyArray_INCREF(dest);
     Py_DECREF(idest);
     Py_DECREF(isrc);
     return 0;

Modified: trunk/numpy/core/src/arraytypes.inc.src
===================================================================
--- trunk/numpy/core/src/arraytypes.inc.src	2008-03-21 19:34:19 UTC (rev 4906)
+++ trunk/numpy/core/src/arraytypes.inc.src	2008-03-21 23:54:58 UTC (rev 4907)
@@ -2086,7 +2086,7 @@
 @name at _fastclip(@type@ *in, intp ni, @type@ *min, @type@ *max, @type@ *out)
 {
         register npy_intp i;
-        @type@ max_val = 0, min_val = 0;
+        @type@ max_val=0, min_val=0;
 
 	if (max != NULL)
 		max_val = *max;




More information about the Numpy-svn mailing list