summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/ctors.c20
-rw-r--r--numpy/core/tests/test_multiarray.py8
-rw-r--r--numpy/core/tests/test_regression.py7
3 files changed, 29 insertions, 6 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 1b7e1c428..a47b8625a 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -412,7 +412,7 @@ setArrayFromSequence(PyArrayObject *a, PyObject *s,
int dim, PyArrayObject * dst)
{
Py_ssize_t i, slen;
- int res = 0;
+ int res = -1;
/* first recursion, view equal destination */
if (dst == NULL)
@@ -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)
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index b98746e1b..23a71d2f1 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -1922,5 +1922,12 @@ class TestRegression(TestCase):
[sixu('F'), sixu('o'), sixu('o'), sixu('b'), sixu('')]]])
assert_array_equal(arr, arr2)
+ def test_assign_from_sequence_error(self):
+ # Ticket #4024.
+ arr = np.array([1, 2, 3])
+ assert_raises(ValueError, arr.__setitem__, slice(None), [9, 9])
+ arr.__setitem__(slice(None), [9])
+ assert_equal(arr, [9, 9, 9])
+
if __name__ == "__main__":
run_module_suite()