summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Belopolsky <a@enlnt.com>2013-11-06 14:17:27 -0500
committerSebastian Berg <sebastian@sipsolutions.net>2013-11-08 16:12:53 +0100
commitf30cecb89264660b5818fedaf7ce1fbb3e35050b (patch)
treeabee0fa81be31230d2469a57375bd65420043cf0
parentd071d3f9d80886868c5ce790289f2af03bed43b1 (diff)
downloadnumpy-f30cecb89264660b5818fedaf7ce1fbb3e35050b.tar.gz
BUG: Support ndim=0 when constructing arrays from buffer
Previously the buffer was interpreted as 1-dimensional when no shape was provided, even when ndim was 0. Closes gh-4015
-rw-r--r--numpy/core/src/multiarray/ctors.c18
-rw-r--r--numpy/core/tests/test_multiarray.py8
2 files changed, 21 insertions, 5 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 1b7e1c428..a897169a3 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -1293,8 +1293,8 @@ _array_from_buffer_3118(PyObject *obj, PyObject **out)
descr->elsize = view->itemsize;
}
+ nd = view->ndim;
if (view->shape != NULL) {
- nd = view->ndim;
if (nd >= NPY_MAXDIMS || nd < 0) {
goto fail;
}
@@ -1318,9 +1318,17 @@ _array_from_buffer_3118(PyObject *obj, PyObject **out)
}
}
else {
- nd = 1;
- shape[0] = view->len / view->itemsize;
- strides[0] = view->itemsize;
+ if (nd == 1) {
+ shape[0] = view->len / view->itemsize;
+ strides[0] = view->itemsize;
+ }
+ else if (nd > 1) {
+ PyErr_WarnEx(PyExc_RuntimeWarning,
+ "ndim computed from the PEP 3118 buffer format "
+ "is greater than 1, but shape is NULL.",
+ 0);
+ goto fail;
+ }
}
flags = NPY_ARRAY_BEHAVED & (view->readonly ? ~NPY_ARRAY_WRITEABLE : ~0);
@@ -2082,7 +2090,7 @@ PyArray_FromInterface(PyObject *origin)
/* Get the strides */
iface = PyArray_GetAttrString_SuppressException(origin,
- "__array_interface__");
+ "__array_interface__");
if (iface == NULL) {
return Py_NotImplemented;
}
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index 9d191da35..f42c426bc 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -3407,9 +3407,13 @@ class TestNewBufferProtocol(object):
y2 = np.array(x)
assert_(not y.flags.owndata)
assert_(y2.flags.owndata)
+
assert_equal(y.dtype, obj.dtype)
+ assert_equal(y.shape, obj.shape)
assert_array_equal(obj, y)
+
assert_equal(y2.dtype, obj.dtype)
+ assert_equal(y2.shape, obj.shape)
assert_array_equal(obj, y2)
def test_roundtrip(self):
@@ -3520,6 +3524,10 @@ class TestNewBufferProtocol(object):
x = np.zeros(4, dtype=dt)
self._check_roundtrip(x)
+ def test_roundtrip_scalar(self):
+ # Issue #4015.
+ self._check_roundtrip(0)
+
def test_export_simple_1d(self):
x = np.array([1, 2, 3, 4, 5], dtype='i')
y = memoryview(x)