summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/core/src/multiarray/ctors.c7
-rw-r--r--numpy/core/tests/test_multiarray.py11
2 files changed, 18 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 87406bdbc..c2abc299c 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -1715,6 +1715,12 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth,
/* Create a new array and copy the data */
Py_INCREF(dtype); /* hold on in case of a subarray that is replaced */
+ if( flags & NPY_ARRAY_ENSURENOCOPY ) {
+ PyErr_SetString(PyExc_ValueError,
+ "Unable to avoid copy while creating "
+ "an array from descriptor.");
+ return NULL;
+ }
ret = (PyArrayObject *)PyArray_NewFromDescr(
&PyArray_Type, dtype, ndim, dims, NULL, NULL,
flags&NPY_ARRAY_F_CONTIGUOUS, NULL);
@@ -1839,6 +1845,7 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth,
if (obj == NULL) {
return NULL;
}
+
if ((requires & NPY_ARRAY_ELEMENTSTRIDES) &&
!PyArray_ElementStrides(obj)) {
PyObject *ret;
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index ba8caddda..8fd7ccf4e 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -7750,6 +7750,17 @@ class TestArrayCreationCopyArgument(object):
true_vals = [True, np.CopyMode.ALWAYS, np.True_]
false_vals = [False, np.CopyMode.IF_NEEDED, np.False_]
+ def test_scalars(self):
+ # Test both numpy and python scalars
+ for dtype in np.typecodes["All"]:
+ arr = np.zeros((), dtype=dtype)
+ scalar = arr[()]
+ pyscalar = arr.item(0)
+
+ # Test never-copy raises error:
+ assert_raises(ValueError, np.array, scalar, copy=np.CopyMode.NEVER)
+ assert_raises(ValueError, np.array, pyscalar, copy=np.CopyMode.NEVER)
+
def test_compatible_cast(self):
# Some types are compatible even though they are different, no