diff options
-rw-r--r-- | numpy/core/records.py | 18 | ||||
-rw-r--r-- | numpy/core/src/multiarraymodule.c | 22 |
2 files changed, 36 insertions, 4 deletions
diff --git a/numpy/core/records.py b/numpy/core/records.py index 9f626fc4b..edafdd6c2 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -270,6 +270,8 @@ def fromarrays(arrayList, dtype=None, shape=None, formats=None, 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 @@ def array(obj, dtype=None, shape=None, offset=0, strides=None, formats=None, return fromstring(obj, dtype, shape=shape, offset=offset, **kwds) elif isinstance(obj, (list, tuple)): - if isinstance(obj[0], sb.ndarray): - return fromarrays(obj, dtype=dtype, shape=shape, **kwds) - else: + if isinstance(obj[0], tuple): return fromrecords(obj, dtype=dtype, shape=shape, **kwds) + else: + return fromarrays(obj, dtype=dtype, shape=shape, **kwds) elif isinstance(obj, recarray): new = obj.copy() @@ -517,4 +519,12 @@ def array(obj, dtype=None, shape=None, offset=0, strides=None, formats=None, 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) + + diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c index c528ffac1..6fd3b6f70 100644 --- a/numpy/core/src/multiarraymodule.c +++ b/numpy/core/src/multiarraymodule.c @@ -6523,6 +6523,26 @@ buffer_buffer(PyObject *dummy, PyObject *args, PyObject *kwds) } 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 @@ static struct PyMethodDef array_module_methods[] = { 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, |