8000 fix from review · numpy/numpy@d15c30f · GitHub
[go: up one dir, main page]

Skip to content

Commit d15c30f

Browse files
committed
fix from review
1 parent 05d94b9 commit d15c30f

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

numpy/core/include/numpy/ndarrayobject.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,12 @@ extern "C" CONFUSE_EMACS
174174
static NPY_INLINE void
175175
PyArray_DiscardWritebackIfCopy(PyArrayObject *arr)
176176
{
177-
if (arr != NULL) {
178-
PyArrayObject_fields *fa = (PyArrayObject_fields *)arr;
179-
if ((PyArray_FLAGS(arr) & NPY_ARRAY_WRITEBACKIFCOPY) ||
180-
(PyArray_FLAGS(arr) & NPY_ARRAY_UPDATEIFCOPY)) {
181-
if (fa->base) {
182-
PyArray_ENABLEFLAGS((PyArrayObject*)fa->base, NPY_ARRAY_WRITEABLE);
183-
Py_DECREF(fa->base);
184-
fa->base = NULL;
185-
}
177+
PyArrayObject_fields *fa = (PyArrayObject_fields *)arr;
178+
if (fa && fa->base) {
179+
if ((fa->flags & NPY_ARRAY_UPDATEIFCOPY) || (fa->flags & NPY_ARRAY_WRITEBACKIFCOPY)) {
180+
PyArray_ENABLEFLAGS((PyArrayObject*)fa->base, NPY_ARRAY_WRITEABLE);
181+
Py_DECREF(fa->base);
182+
fa->base = NULL;
186183
PyArray_CLEARFLAGS(arr, NPY_ARRAY_WRITEBACKIFCOPY);
187184
PyArray_CLEARFLAGS(arr, NPY_ARRAY_UPDATEIFCOPY);
188185
}

numpy/core/tests/test_multiarray.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7246,17 +7246,20 @@ def test_dot_out(self):
72467246

72477247
def test_view_assign(self):
72487248
from numpy.core._multiarray_tests import npy_create_writebackifcopy, npy_resolve
7249+
72497250
arr = np.arange(9).reshape(3, 3).T
72507251
arr_wb = npy_create_writebackifcopy(arr)
72517252
assert_(arr_wb.flags.writebackifcopy)
72527253
assert_(arr_wb.base is arr)
7253-
arr_wb[:] = -100
7254+
arr_wb[...] = -100
72547255
npy_resolve(arr_wb)
7256+
# arr changes after resolve, even though we assigned to arr_wb
72557257
assert_equal(arr, -100)
72567258
# after resolve, the two arrays no longer reference each other
7257-
assert_(not arr_wb.ctypes.data == 0)
7259+
assert_(arr_wb.ctypes.data != 0)
72587260
assert_equal(arr_wb.base, None)
7259-
arr_wb[:] = 100
7261+
# assigning to arr_wb does not get transfered to arr
7262+
arr_wb[...] = 100
72607263
assert_equal(arr, -100)
72617264

72627265
def test_dealloc_warning(self):
@@ -7269,23 +7272,25 @@ def test_dealloc_warning(self):
72697272

72707273
def test_view_discard_refcount(self):
72717274
from numpy.core._multiarray_tests import npy_create_writebackifcopy, npy_discard
7275+
72727276
arr = np.arange(9).reshape(3, 3).T
72737277
orig = arr.copy()
72747278
if HAS_REFCOUNT:
72757279
arr_cnt = sys.getrefcount(arr)
72767280
arr_wb = npy_create_writebackifcopy(arr)
72777281
assert_(arr_wb.flags.writebackifcopy)
72787282
assert_(arr_wb.base is arr)
7279-
arr_wb[:] = -100
7283+
arr_wb[...] = -100
72807284
npy_discard(arr_wb)
72817285
# arr remains unchanged after discard
72827286
assert_equal(arr, orig)
72837287
# after discard, the two arrays no longer reference each other
7284-
assert_(not arr_wb.ctypes.data == 0)
7288+
assert_(arr_wb.ctypes.data != 0)
72857289
assert_equal(arr_wb.base, None)
72867290
if HAS_REFCOUNT:
72877291
assert_equal(arr_cnt, sys.getrefcount(arr))
7288-
arr_wb[:] = 100
7292+
# assigning to arr_wb does not get transfered to arr
7293+
arr_wb[...] = 100
72897294
assert_equal(arr, orig)
72907295

72917296

0 commit comments

Comments
 (0)
0