Skip to content

Commit 5765325

Browse files
author
Mark Wiebe
committed
BUG: core: PyArray_NewFromDescr needs to update flags when strides != NULL (fixes numpy#1863
This appears to have been a longstanding bug, but has come to the surface because PyArray_NewLikeArray uses the function with a non-NULL strides parameter and is used more frequently then other such uses.
1 parent 878ab94 commit 5765325

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

numpy/core/src/multiarray/ctors.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,14 @@ PyArray_NewFromDescr(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
10731073
}
10741074
self->data = data;
10751075

1076+
/*
1077+
* If the strides were provided to the function, need to
1078+
* update the flags to get the right CONTIGUOUS, ALIGN properties
1079+
*/
1080+
if (strides != NULL) {
1081+
PyArray_UpdateFlags(self, UPDATE_ALL);
1082+
}
1083+
10761084
/*
10771085
* call the __array_finalize__
10781086
* method if a subtype.
@@ -1083,13 +1091,6 @@ PyArray_NewFromDescr(PyTypeObject *subtype, PyArray_Descr *descr, int nd,
10831091

10841092
func = PyObject_GetAttrString((PyObject *)self, "__array_finalize__");
10851093
if (func && func != Py_None) {
1086-
if (strides != NULL) {
1087-
/*
1088-
* did not allocate own data or funny strides
1089-
* update flags before finalize function
1090-
*/
1091-
PyArray_UpdateFlags(self, UPDATE_ALL);
1092-
}
10931094
if (NpyCapsule_Check(func)) {
10941095
/* A C-function is stored here */
10951096
PyArray_FinalizeFunc *cfunc;

numpy/core/tests/test_numeric.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,8 @@ def check_like_function(self, like_function, value):
12271227
assert_equal(dz.shape, d.shape)
12281228
assert_equal(array(dz.strides)*d.dtype.itemsize,
12291229
array(d.strides)*dz.dtype.itemsize)
1230+
assert_equal(d.flags.c_contiguous, dz.flags.c_contiguous)
1231+
assert_equal(d.flags.f_contiguous, dz.flags.f_contiguous)
12301232
if dtype is None:
12311233
assert_equal(dz.dtype, d.dtype)
12321234
else:

0 commit comments

Comments
 (0)