[Numpy-svn] r3243 - in trunk/numpy/core: . src

numpy-svn at scipy.org numpy-svn at scipy.org
Mon Oct 2 18:17:59 EDT 2006


Author: oliphant
Date: 2006-10-02 17:17:56 -0500 (Mon, 02 Oct 2006)
New Revision: 3243

Modified:
   trunk/numpy/core/records.py
   trunk/numpy/core/src/multiarraymodule.c
Log:
Fix ticket #308 by sending more than just ndarray to fromarray.  Also, allow conversion of array-interfacing object to recarray.  Also add the asbuffer function to multiarray (but don't expose it) which allows creating buffer objects from memory locations.  User must be sure the object holding the memory is not deallocated before the new buffer.

Modified: trunk/numpy/core/records.py
===================================================================
--- trunk/numpy/core/records.py	2006-10-02 21:21:22 UTC (rev 3242)
+++ trunk/numpy/core/records.py	2006-10-02 22:17:56 UTC (rev 3243)
@@ -270,6 +270,8 @@
     if isinstance(shape, int):
         shape = (shape,)
 
+    arrayList = [sb.asarray(x) for x in arrayList]
+
     if formats is None and dtype is None:
         # go through each object in the list to see if it is an ndarray
         # and determine the formats.
@@ -494,10 +496,10 @@
         return fromstring(obj, dtype, shape=shape, offset=offset, **kwds)
 
     elif isinstance(obj, (list, tuple)):
-        if isinstance(obj[0], sb.ndarray):
+        if isinstance(obj[0], tuple):
+            return fromrecords(obj, dtype=dtype, shape=shape, **kwds)
+        else:
             return fromarrays(obj, dtype=dtype, shape=shape, **kwds)
-        else:
-            return fromrecords(obj, dtype=dtype, shape=shape, **kwds)
 
     elif isinstance(obj, recarray):
         new = obj.copy()
@@ -517,4 +519,12 @@
         return res
 
     else:
-        raise ValueError("Unknown input type")
+        interface = getattr(obj, "__array_interface__", None)
+        if interface is None or not isinstance(interface, dict):
+            raise ValueError("Unknown input type")
+        dtype = interface.get("descr", None) or interface.get("typestr")
+        shape = interface.get("shape")
+        strides = interface.get("strides", None)
+        return recarray(shape, dtype, buf=obj, offset=offset, strides=strides)
+        
+                

Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c	2006-10-02 21:21:22 UTC (rev 3242)
+++ trunk/numpy/core/src/multiarraymodule.c	2006-10-02 22:17:56 UTC (rev 3243)
@@ -6523,6 +6523,26 @@
 }
 
 static PyObject *
+as_buffer(PyObject *dummy, PyObject *args, PyObject *kwds)
+{
+        PyObject *mem;
+        Py_ssize_t size;
+        Bool ro=FALSE;
+        void *memptr;
+        static char *kwlist[] = {"mem", "size", "readonly", NULL};
+        if (!PyArg_ParseTupleAndKeywords(args, kwds, "O" \
+                                         NPY_SSIZE_T_PYFMT "|O&", 
+                                         &mem, &size, PyArray_BoolConverter,
+                                         &ro)) return NULL;
+        memptr = PyLong_AsVoidPtr(mem);
+        if (memptr == NULL) return NULL;
+        if (ro) {
+                return PyBuffer_FromMemory(memptr, size);
+        }
+        return PyBuffer_FromReadWriteMemory(memptr, size);
+}
+
+static PyObject *
 format_longfloat(PyObject *dummy, PyObject *args, PyObject *kwds)
 {
     PyObject *obj;
@@ -6731,6 +6751,8 @@
 	 METH_VARARGS, NULL},
 	{"getbuffer", (PyCFunction)buffer_buffer,
 	 METH_VARARGS | METH_KEYWORDS, NULL},
+        {"asbuffer", (PyCFunction)as_buffer,
+         METH_VARARGS, NULL},
         {"format_longfloat", (PyCFunction)format_longfloat,
          METH_VARARGS | METH_KEYWORDS, NULL},
         {"compare_chararrays", (PyCFunction)compare_chararrays,




More information about the Numpy-svn mailing list