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

numpy-svn@scip... numpy-svn@scip...
Thu Sep 20 23:19:59 CDT 2007


Author: oliphant
Date: 2007-09-20 23:19:00 -0500 (Thu, 20 Sep 2007)
New Revision: 4067

Modified:
   trunk/numpy/core/src/multiarraymodule.c
Log:
Fix places where RENEW is called so that if reallocation fails, cleanup is done gracefully.

Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c	2007-09-21 01:56:03 UTC (rev 4066)
+++ trunk/numpy/core/src/multiarraymodule.c	2007-09-21 04:19:00 UTC (rev 4067)
@@ -3679,7 +3679,7 @@
      */
     ap = (PyArrayObject *)\
         PyArray_ContiguousFromAny((PyObject *)op, 
-				  op->descr->type_num, 1, 0);
+                                  op->descr->type_num, 1, 0);
 
     Py_DECREF(op);
     if (ap == NULL) return NULL;
@@ -5998,8 +5998,8 @@
 {
     PyArrayObject *r;
     intp i;
-    char *dptr, *clean_sep;
-
+    char *dptr, *clean_sep, *tmp;
+    int err = 0;
     intp thisbuf = 0;
     intp size;
     intp bytes, totalbytes;
@@ -6025,19 +6025,29 @@
         dptr += dtype->elsize;
         if (num < 0 && thisbuf == size) {
             totalbytes += bytes;
-            r->data = PyDataMem_RENEW(r->data, totalbytes);
-            dptr = r->data + (totalbytes - bytes);
+            tmp = PyDataMem_RENEW(r->data, totalbytes);
+            if (tmp == NULL) {
+                err = 1;
+                break;
+            }
+	    r->data = tmp;
+            dptr = tmp + (totalbytes - bytes);
             thisbuf = 0;
         }
         if (skip_sep(&stream, clean_sep, stream_data) < 0)
             break;
     }
     if (num < 0) {
-        r->data = PyDataMem_RENEW(r->data, (*nread)*dtype->elsize);
-        PyArray_DIM(r,0) = *nread;
+        tmp = PyDataMem_RENEW(r->data, (*nread)*dtype->elsize);
+	if (tmp == NULL) err=1;
+	else {
+	    PyArray_DIM(r,0) = *nread;
+	    r->data = tmp;
+	}
     }
     NPY_END_ALLOW_THREADS;
     free(clean_sep);
+    if (err == 1) PyErr_NoMemory();
     if (PyErr_Occurred()) {
         Py_DECREF(r);
         return NULL;
@@ -6229,6 +6239,7 @@
 {
     PyArrayObject *ret;
     size_t nread = 0;
+    char *tmp;
 
     if (PyDataType_REFCHK(dtype)) {
         PyErr_SetString(PyExc_ValueError,
@@ -6262,8 +6273,13 @@
     if (((intp) nread) < num) {
         fprintf(stderr, "%ld items requested but only %ld read\n",
                 (long) num, (long) nread);
-        ret->data = PyDataMem_RENEW(ret->data,
-                                    nread * ret->descr->elsize);
+        tmp = PyDataMem_RENEW(ret->data,
+			      nread * ret->descr->elsize);
+	if (tmp == NULL) {
+	    Py_DECREF(ret);
+	    return PyErr_NoMemory();	    
+	}
+	ret->data = tmp;
         PyArray_DIM(ret,0) = nread;
     }
     return (PyObject *)ret;
@@ -6384,11 +6400,12 @@
       (assuming realloc is reasonably good about reusing space...)
     */
     if (i==0) i = 1;
-    ret->data = PyDataMem_RENEW(ret->data, i * elsize);
-    if (ret->data == NULL) {
+    new_data = PyDataMem_RENEW(ret->data, i * elsize);
+    if (new_data == NULL) {
         PyErr_SetString(PyExc_MemoryError, "cannot allocate array memory");
         goto done;
     }
+    ret->data = new_data;
 
  done:
     Py_XDECREF(iter);



More information about the Numpy-svn mailing list