summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Bray <erik.m.bray@gmail.com>2017-10-19 10:42:54 +0200
committerEric Wieser <wieser.eric@gmail.com>2017-10-19 01:42:54 -0700
commite9c85baf072f81b2a2e47e07b4d4e121ddbfb669 (patch)
tree25ae644b0104986cae228054ca2bc3fafac40d47
parente657629bbc2bfb880a1b2fa24a39c5921c1f965e (diff)
downloadnumpy-e9c85baf072f81b2a2e47e07b4d4e121ddbfb669.tar.gz
BUG: Allow __array_interface__['shape'] == () (#7994)
Previously this would fail when __array_interface__['data'] is a buffer object. gh-2897 already removed this check for other types of data.
-rw-r--r--numpy/core/src/multiarray/ctors.c7
-rw-r--r--numpy/core/tests/test_multiarray.py26
2 files changed, 27 insertions, 6 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 7958c619f..701f1da73 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -2301,12 +2301,7 @@ PyArray_FromInterface(PyObject *origin)
/* Case for data access through buffer */
else if (attr) {
- if (n == 0) {
- PyErr_SetString(PyExc_ValueError,
- "__array_interface__ shape must be at least size 1");
- goto fail;
- }
- if (attr && (attr != Py_None)) {
+ if (attr != Py_None) {
base = attr;
}
else {
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index b1a6fbe44..4808303ae 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -6529,6 +6529,32 @@ def test_array_interface_itemsize():
assert_equal(descr_t.itemsize, typestr_t.itemsize)
+def test_array_interface_empty_shape():
+ # See gh-7994
+ arr = np.array([1, 2, 3])
+ interface1 = dict(arr.__array_interface__)
+ interface1['shape'] = ()
+
+ class DummyArray1(object):
+ __array_interface__ = interface1
+
+ # NOTE: Because Py2 str/Py3 bytes supports the buffer interface, setting
+ # the interface data to bytes would invoke the bug this tests for, that
+ # __array_interface__ with shape=() is not allowed if the data is an object
+ # exposing the buffer interface
+ interface2 = dict(interface1)
+ interface2['data'] = arr[0].tobytes()
+
+ class DummyArray2(object):
+ __array_interface__ = interface2
+
+ arr1 = np.asarray(DummyArray1())
+ arr2 = np.asarray(DummyArray2())
+ arr3 = arr[:1].reshape(())
+ assert_equal(arr1, arr2)
+ assert_equal(arr1, arr3)
+
+
def test_flat_element_deletion():
it = np.ones(3).flat
try: