summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/convert_datatype.c6
-rw-r--r--numpy/core/src/multiarray/ctors.c4
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.c45
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.h7
-rw-r--r--numpy/core/src/umath/umathmodule.c15
-rw-r--r--numpy/core/tests/test_casting_unittests.py8
6 files changed, 49 insertions, 36 deletions
diff --git a/numpy/core/src/multiarray/convert_datatype.c b/numpy/core/src/multiarray/convert_datatype.c
index 19127291a..cd0e21098 100644
--- a/numpy/core/src/multiarray/convert_datatype.c
+++ b/numpy/core/src/multiarray/convert_datatype.c
@@ -3297,8 +3297,10 @@ void_to_void_resolve_descriptors(
casting = NPY_NO_CASTING | _NPY_CAST_IS_VIEW;
}
}
- NPY_CASTING field_casting = PyArray_GetCastSafety(
- given_descrs[0]->subarray->base, given_descrs[1]->subarray->base, NULL);
+
+ PyArray_Descr *from_base = (from_sub == NULL) ? given_descrs[0] : from_sub->base;
+ PyArray_Descr *to_base = (to_sub == NULL) ? given_descrs[1] : to_sub->base;
+ NPY_CASTING field_casting = PyArray_GetCastSafety(from_base, to_base, NULL);
if (field_casting < 0) {
return -1;
}
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index ef28d7797..aaa645c16 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -3983,8 +3983,8 @@ _array_fill_strides(npy_intp *strides, npy_intp const *dims, int nd, size_t item
NPY_NO_EXPORT PyArrayObject *
PyArray_SubclassWrap(PyArrayObject *arr_of_subclass, PyArrayObject *towrap)
{
- PyObject *wrapped = PyObject_CallMethod((PyObject *)arr_of_subclass,
- "__array_wrap__", "O", towrap);
+ PyObject *wrapped = PyObject_CallMethodObjArgs((PyObject *)arr_of_subclass,
+ npy_ma_str_array_wrap, (PyObject *)towrap, NULL);
if (wrapped == NULL) {
return NULL;
}
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c
index f7c3ea093..b8ab52340 100644
--- a/numpy/core/src/multiarray/multiarraymodule.c
+++ b/numpy/core/src/multiarray/multiarraymodule.c
@@ -4606,16 +4606,9 @@ set_flaginfo(PyObject *d)
return;
}
-NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array = NULL;
-NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_prepare = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_wrap = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_finalize = NULL;
-NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_ufunc = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_implementation = NULL;
-NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_order = NULL;
-NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_copy = NULL;
-NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_dtype = NULL;
-NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_ndmin = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_axis1 = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_axis2 = NULL;
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_like = NULL;
@@ -4624,27 +4617,35 @@ NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_numpy = NULL;
static int
intern_strings(void)
{
- npy_ma_str_array = PyUnicode_InternFromString("__array__");
- npy_ma_str_array_prepare = PyUnicode_InternFromString("__array_prepare__");
npy_ma_str_array_wrap = PyUnicode_InternFromString("__array_wrap__");
+ if (npy_ma_str_array_wrap == NULL) {
+ return -1;
+ }
npy_ma_str_array_finalize = PyUnicode_InternFromString("__array_finalize__");
- npy_ma_str_ufunc = PyUnicode_InternFromString("__array_ufunc__");
+ if (npy_ma_str_array_finalize == NULL) {
+ return -1;
+ }
npy_ma_str_implementation = PyUnicode_InternFromString("_implementation");
- npy_ma_str_order = PyUnicode_InternFromString("order");
- npy_ma_str_copy = PyUnicode_InternFromString("copy");
- npy_ma_str_dtype = PyUnicode_InternFromString("dtype");
- npy_ma_str_ndmin = PyUnicode_InternFromString("ndmin");
+ if (npy_ma_str_implementation == NULL) {
+ return -1;
+ }
npy_ma_str_axis1 = PyUnicode_InternFromString("axis1");
+ if (npy_ma_str_axis1 == NULL) {
+ return -1;
+ }
npy_ma_str_axis2 = PyUnicode_InternFromString("axis2");
+ if (npy_ma_str_axis2 == NULL) {
+ return -1;
+ }
npy_ma_str_like = PyUnicode_InternFromString("like");
+ if (npy_ma_str_like == NULL) {
+ return -1;
+ }
npy_ma_str_numpy = PyUnicode_InternFromString("numpy");
-
- return npy_ma_str_array && npy_ma_str_array_prepare &&
- npy_ma_str_array_wrap && npy_ma_str_array_finalize &&
- npy_ma_str_ufunc && npy_ma_str_implementation &&
- npy_ma_str_order && npy_ma_str_copy && npy_ma_str_dtype &&
- npy_ma_str_ndmin && npy_ma_str_axis1 && npy_ma_str_axis2 &&
- npy_ma_str_like && npy_ma_str_numpy;
+ if (npy_ma_str_numpy == NULL) {
+ return -1;
+ }
+ return 0;
}
static struct PyModuleDef moduledef = {
@@ -4876,7 +4877,7 @@ PyMODINIT_FUNC PyInit__multiarray_umath(void) {
goto err;
}
- if (!intern_strings()) {
+ if (intern_strings() < 0) {
goto err;
}
diff --git a/numpy/core/src/multiarray/multiarraymodule.h b/numpy/core/src/multiarray/multiarraymodule.h
index d3ee3337c..4cdb6ef72 100644
--- a/numpy/core/src/multiarray/multiarraymodule.h
+++ b/numpy/core/src/multiarray/multiarraymodule.h
@@ -1,16 +1,9 @@
#ifndef _NPY_MULTIARRAY_H_
#define _NPY_MULTIARRAY_H_
-NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array;
-NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_prepare;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_wrap;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_finalize;
-NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_ufunc;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_implementation;
-NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_order;
-NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_copy;
-NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_dtype;
-NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_ndmin;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_axis1;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_axis2;
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_like;
diff --git a/numpy/core/src/umath/umathmodule.c b/numpy/core/src/umath/umathmodule.c
index b4b7db760..6a718889b 100644
--- a/numpy/core/src/umath/umathmodule.c
+++ b/numpy/core/src/umath/umathmodule.c
@@ -224,9 +224,18 @@ NPY_VISIBILITY_HIDDEN PyObject *npy_um_str_pyvals_name = NULL;
static int
intern_strings(void)
{
- if (!(npy_um_str_array_prepare = PyUnicode_InternFromString("__array_prepare__"))) return -1;
- if (!(npy_um_str_array_wrap = PyUnicode_InternFromString("__array_wrap__"))) return -1;
- if (!(npy_um_str_pyvals_name = PyUnicode_InternFromString(UFUNC_PYVALS_NAME))) return -1;
+ npy_um_str_array_prepare = PyUnicode_InternFromString("__array_prepare__");
+ if (npy_um_str_array_prepare == NULL) {
+ return -1;
+ }
+ npy_um_str_array_wrap = PyUnicode_InternFromString("__array_wrap__");
+ if (npy_um_str_array_wrap == NULL) {
+ return -1;
+ }
+ npy_um_str_pyvals_name = PyUnicode_InternFromString(UFUNC_PYVALS_NAME);
+ if (npy_um_str_pyvals_name == NULL) {
+ return -1;
+ }
return 0;
}
diff --git a/numpy/core/tests/test_casting_unittests.py b/numpy/core/tests/test_casting_unittests.py
index c15de3e88..1c465fea1 100644
--- a/numpy/core/tests/test_casting_unittests.py
+++ b/numpy/core/tests/test_casting_unittests.py
@@ -649,3 +649,11 @@ class TestCasting:
with pytest.raises(TypeError,
match="casting from object to the parametric DType"):
cast._resolve_descriptors((np.dtype("O"), None))
+
+ @pytest.mark.parametrize("casting", ["no", "unsafe"])
+ def test_void_and_structured_with_subarray(self, casting):
+ # test case corresponding to gh-19325
+ dtype = np.dtype([("foo", "<f4", (3, 2))])
+ expected = casting == "unsafe"
+ assert np.can_cast("V4", dtype, casting=casting) == expected
+ assert np.can_cast(dtype, "V4", casting=casting) == expected