[Numpy-svn] r8133 - trunk/numpy/core/src/multiarray

numpy-svn@scip... numpy-svn@scip...
Sat Feb 20 12:04:11 CST 2010


Author: ptvirtan
Date: 2010-02-20 12:04:11 -0600 (Sat, 20 Feb 2010)
New Revision: 8133

Modified:
   trunk/numpy/core/src/multiarray/buffer.c
Log:
ENH: core: cleanups in PEP 3118 provider

Modified: trunk/numpy/core/src/multiarray/buffer.c
===================================================================
--- trunk/numpy/core/src/multiarray/buffer.c	2010-02-20 18:03:53 UTC (rev 8132)
+++ trunk/numpy/core/src/multiarray/buffer.c	2010-02-20 18:04:11 UTC (rev 8133)
@@ -327,13 +327,20 @@
 
     /* Fill in shape and strides */
     info->ndim = PyArray_NDIM(arr);
-    info->shape = (Py_ssize_t*)malloc(sizeof(Py_ssize_t)
-                                      * PyArray_NDIM(arr) * 2 + 1);
-    info->strides = info->shape + PyArray_NDIM(arr);
-    for (k = 0; k < PyArray_NDIM(arr); ++k) {
-        info->shape[k] = PyArray_DIMS(arr)[k];
-        info->strides[k] = PyArray_STRIDES(arr)[k];
+
+    if (info->ndim == 0) {
+        info->shape = NULL;
+        info->strides = NULL;
     }
+    else {
+        info->shape = (Py_ssize_t*)malloc(sizeof(Py_ssize_t)
+                                          * PyArray_NDIM(arr) * 2 + 1);
+        info->strides = info->shape + PyArray_NDIM(arr);
+        for (k = 0; k < PyArray_NDIM(arr); ++k) {
+            info->shape[k] = PyArray_DIMS(arr)[k];
+            info->strides[k] = PyArray_STRIDES(arr)[k];
+        }
+    }
 
     return info;
 }
@@ -465,15 +472,13 @@
     self = (PyArrayObject*)obj;
 
     if (view == NULL) {
-        return -1;
+        goto fail;
     }
 
-    view->format = NULL;
-    view->shape = NULL;
-
+    /* Check whether we can provide the wanted properties */
     if ((flags & PyBUF_C_CONTIGUOUS) == PyBUF_C_CONTIGUOUS &&
         !PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)) {
-        PyErr_SetString(PyExc_ValueError, "ndarray is not C contiguous");
+        PyErr_SetString(PyExc_ValueError, "ndarray is not C-contiguous");
         goto fail;
     }
     if ((flags & PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS &&
@@ -486,44 +491,41 @@
         PyErr_SetString(PyExc_ValueError, "ndarray is not contiguous");
         goto fail;
     }
+    if ((flags & PyBUF_STRIDES) != PyBUF_STRIDES &&
+        !PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)) {
+        /* Non-strided buffers must be C-contiguous */
+        PyErr_SetString(PyExc_ValueError, "ndarray is not C-contiguous");
+        goto fail;
+    }
+    if ((flags & PyBUF_WRITEABLE) == PyBUF_WRITEABLE &&
+        !PyArray_ISWRITEABLE(self)) {
+        PyErr_SetString(PyExc_ValueError, "ndarray is not writeable");
+        goto fail;
+    }
 
+    /* Fill in information */
+    info = _buffer_get_info(obj);
+    if (info == NULL) {
+        goto fail;
+    }
+
     view->buf = PyArray_DATA(self);
     view->suboffsets = NULL;
     view->itemsize = PyArray_ITEMSIZE(self);
     view->readonly = !PyArray_ISWRITEABLE(self);
     view->internal = NULL;
     view->len = PyArray_NBYTES(self);
-
-    info = _buffer_get_info(obj);
-    if (info == NULL) {
-        goto fail;
-    }
-
     if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
         view->format = info->format;
-    }
-    else {
+    } else {
         view->format = NULL;
     }
+    view->ndim = info->ndim;
+    view->shape = info->shape;
+    view->strides = info->strides;
+    view->obj = (PyObject*)self;
 
-    if ((flags & PyBUF_STRIDED) == PyBUF_STRIDED) {
-        view->ndim = info->ndim;
-        view->shape = info->shape;
-        view->strides = info->strides;
-    }
-    else if (PyArray_ISONESEGMENT(self)) {
-        view->ndim = 0;
-        view->shape = NULL;
-        view->strides = NULL;
-    }
-    else {
-        PyErr_SetString(PyExc_ValueError, "ndarray is not single-segment");
-        goto fail;
-    }
-
-    view->obj = (PyObject*)self;
     Py_INCREF(self);
-
     return 0;
 
 fail:



More information about the Numpy-svn mailing list