summaryrefslogtreecommitdiff
path: root/numpy/core
diff options
context:
space:
mode:
authorMatti Picus <matti.picus@gmail.com>2020-07-13 15:49:11 +0300
committerGitHub <noreply@github.com>2020-07-13 15:49:11 +0300
commitc409143450a39d2670d5ecb79c76a10b6ed2d78a (patch)
treef065790a531017cdd6e1cc9f9a02c09a3315d0e0 /numpy/core
parenta7d82be935b8e7f29282afbb384cdb81154f9e46 (diff)
parentc05a09c25591424f19d5b896e77f301b98f020b7 (diff)
downloadnumpy-c409143450a39d2670d5ecb79c76a10b6ed2d78a.tar.gz
Merge pull request #16820 from seberg/voidscalar-to-array
MAINT: Make void scalar to array creation copy when dtype is given
Diffstat (limited to 'numpy/core')
-rw-r--r--numpy/core/src/multiarray/ctors.c8
-rw-r--r--numpy/core/tests/test_multiarray.py15
2 files changed, 21 insertions, 2 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 0c4ffe141..cb448756b 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -1438,11 +1438,15 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth,
}
else if (cache == NULL && PyArray_IsScalar(op, Void) &&
!(((PyVoidScalarObject *)op)->flags & NPY_ARRAY_OWNDATA) &&
- PyArray_EquivTypes(((PyVoidScalarObject *)op)->descr, dtype)) {
+ newtype == NULL) {
/*
* Special case, we return a *view* into void scalars, mainly to
- * allow "reversed" assignment:
+ * allow things similar to the "reversed" assignment:
* arr[indx]["field"] = val # instead of arr["field"][indx] = val
+ *
+ * It is unclear that this is necessary in this particular code path.
+ * Note that this path is only activated when the user did _not_
+ * provide a dtype (newtype is NULL).
*/
assert(ndim == 0);
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index fb3a5f50b..b7d4a6a92 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -1390,6 +1390,21 @@ class TestStructured:
assert_raises(ValueError, lambda : a[['b','b']]) # field exists, but repeated
a[['b','c']] # no exception
+ def test_structured_asarray_is_view(self):
+ # A scalar viewing an array preserves its view even when creating a
+ # new array. This test documents behaviour, it may not be the best
+ # desired behaviour.
+ arr = np.array([1], dtype="i,i")
+ scalar = arr[0]
+ assert not scalar.flags.owndata # view into the array
+ assert np.asarray(scalar).base is scalar
+ # But never when a dtype is passed in:
+ assert np.asarray(scalar, dtype=scalar.dtype).base is None
+ # A scalar which owns its data does not have this property.
+ # It is not easy to create one, one method is to use pickle:
+ scalar = pickle.loads(pickle.dumps(scalar))
+ assert scalar.flags.owndata
+ assert np.asarray(scalar).base is None
class TestBool:
def test_test_interning(self):