[Numpy-svn] r3233 - in trunk/numpy/core: include/numpy src

numpy-svn at scipy.org numpy-svn at scipy.org
Thu Sep 28 13:46:26 EDT 2006


Author: oliphant
Date: 2006-09-28 12:46:22 -0500 (Thu, 28 Sep 2006)
New Revision: 3233

Modified:
   trunk/numpy/core/include/numpy/ndarrayobject.h
   trunk/numpy/core/src/arrayobject.c
Log:
Create PyArray_HasArrayInterface macro to simplify getting an array from the array interface.

Modified: trunk/numpy/core/include/numpy/ndarrayobject.h
===================================================================
--- trunk/numpy/core/include/numpy/ndarrayobject.h	2006-09-28 16:22:43 UTC (rev 3232)
+++ trunk/numpy/core/include/numpy/ndarrayobject.h	2006-09-28 17:46:22 UTC (rev 3233)
@@ -1416,6 +1416,13 @@
 #define PyArray_Check(op) ((op)->ob_type == &PyArray_Type ||            \
                            PyObject_TypeCheck((op), &PyArray_Type))
 #define PyArray_CheckExact(op) ((op)->ob_type == &PyArray_Type)
+#define PyArray_HasArrayInterfaceType(op, type, context, out)              \
+        ((((out)=PyArray_FromStructInterface(op)) != Py_NotImplemented) || \
+         (((out)=PyArray_FromInterface(op)) != Py_NotImplemented) ||    \
+         (((out)=PyArray_FromArrayAttr(op, type, context)) !=           \
+          Py_NotImplemented))
+#define PyArray_HasArrayInterface(op, out)              \
+        PyArray_HasArrayInterfaceType(op, NULL, NULL, out)
 
 #define PyArray_IsZeroDim(op) (PyArray_Check(op) && (PyArray_NDIM(op) == 0))
 #define PyArray_IsScalar(obj, cls)                              \

Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c	2006-09-28 16:22:43 UTC (rev 3232)
+++ trunk/numpy/core/src/arrayobject.c	2006-09-28 17:46:22 UTC (rev 3233)
@@ -1158,7 +1158,7 @@
 PyArray_CopyObject(PyArrayObject *dest, PyObject *src_object)
 {
         PyArrayObject *src;
-        PyArray_Descr* dtype;
+        PyObject *r;
         int ret;
 
         /* Special code to mimic Numeric behavior for
@@ -1184,17 +1184,24 @@
         }
 
         if (PyArray_Check(src_object)) {
-                dtype = NULL;
+                src = (PyArrayObject *)src_object;
+                Py_INCREF(src);
         }
+        else if (!PyArray_IsScalar(src_object, Generic) && 
+                 PyArray_HasArrayInterface(src_object, r)) {
+                src = (PyArrayObject *)r;
+        }
         else {
+                PyArray_Descr* dtype;
                 dtype = dest->descr;
                 Py_INCREF(dtype);
+                src = (PyArrayObject *)PyArray_FromAny(src_object, dtype, 0,
+                                                       dest->nd,
+                                                       FORTRAN_IF(dest), 
+                                                       NULL);
         }
-        src = (PyArrayObject *)PyArray_FromAny(src_object, dtype, 0,
-                                               dest->nd,
-                                               FORTRAN_IF(dest), NULL);
         if (src == NULL) return -1;
-
+        
         ret = PyArray_MoveInto(dest, src);
         Py_DECREF(src);
         return ret;
@@ -8253,10 +8260,7 @@
             if (flags & UPDATEIFCOPY) goto err;
             r = Array_FromPyScalar(op, newtype);
         }
-        else if (((r = PyArray_FromStructInterface(op))!=Py_NotImplemented)|| \
-                 ((r = PyArray_FromInterface(op)) != Py_NotImplemented) || \
-                 ((r = PyArray_FromArrayAttr(op, newtype, context))     \
-                  != Py_NotImplemented)) {
+        else if (PyArray_HasArrayInterfaceType(op, newtype, context, r)) {
                 PyObject *new;
                 if (r == NULL) {Py_XDECREF(newtype); return NULL;}
                 if (newtype != NULL || flags != 0) {




More information about the Numpy-svn mailing list