-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
BUG: test, fix PyArray_DiscardWritebackIfCopy refcount issue and docu… #10824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…ment
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,14 +170,19 @@ extern "C" CONFUSE_EMACS | |
(k)*PyArray_STRIDES(obj)[2] + \ | ||
(l)*PyArray_STRIDES(obj)[3])) | ||
|
||
/* Move to arrayobject.c once PyArray_XDECREF_ERR is removed */ | ||
static NPY_INLINE void | ||
PyArray_DiscardWritebackIfCopy(PyArrayObject *arr) | ||
{ | ||
if (arr != NULL) { | ||
PyArrayObject_fields *fa = (PyArrayObject_fields *)arr; | ||
if ((PyArray_FLAGS(arr) & NPY_ARRAY_WRITEBACKIFCOPY) || | ||
(PyArray_FLAGS(arr) & NPY_ARRAY_UPDATEIFCOPY)) { | ||
PyArrayObject *base = (PyArrayObject *)PyArray_BASE(arr); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't you continue using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks to do the same thing, just in one step instead of two. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed to be more like |
||
PyArray_ENABLEFLAGS(base, NPY_ARRAY_WRITEABLE); | ||
if (fa->base) { | ||
PyArray_ENABLEFLAGS((PyArrayObject*)fa->base, NPY_ARRAY_WRITEABLE); | ||
Py_DECREF(fa->base); | ||
fa->base = NULL; | ||
} | ||
PyArray_CLEARFLAGS(arr, NPY_ARRAY_WRITEBACKIFCOPY); | ||
PyArray_CLEARFLAGS(arr, NPY_ARRAY_UPDATEIFCOPY); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7255,6 +7255,7 @@ def test_view_assign(self): | |
assert_equal(arr, -100) | ||
# after resolve, the two arrays no longer reference each other | ||
assert_(not arr_wb.ctypes.data == 0) | ||
assert_equal(arr_wb.base, None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, but shouldn't the base point to something valid even if the array is about to be destroyed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
arr_wb[:] = 100 | ||
assert_equal(arr, -100) | ||
|
||
|
@@ -7266,6 +7267,28 @@ def test_dealloc_warning(self): | |
_multiarray_tests.npy_abuse_writebackifcopy(v) | ||
assert len(sup.log) == 1 | ||
|
||
def test_view_discard_refcount(self): | ||
from numpy.core._multiarray_tests import npy_create_writebackifcopy, npy_discard | ||
arr = np.arange(9).reshape(3, 3).T | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blank line above this. |
||
orig = arr.copy() | ||
if HAS_REFCOUNT: | ||
arr_cnt = sys.getrefcount(arr) | ||
arr_wb = npy_create_writebackifcopy(arr) | ||
assert_(arr_wb.flags.writebackifcopy) | ||
assert_(arr_wb.base is arr) | ||
arr_wb[:] = -100 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
npy_discard(arr_wb) | ||
# arr remains unchanged after discard | ||
assert_equal(arr, orig) | ||
# after discard, the two arrays no longer reference each other | ||
assert_(not arr_wb.ctypes.data == 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
assert_equal(arr_wb.base, None) | ||
if HAS_REFCOUNT: | ||
assert_equal(arr_cnt, sys.getrefcount(arr)) | ||
arr_wb[:] = 100 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. I don't quite see what this tests except that you can use the array. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added comments to try to clarify the intent. I wanted to test that I can use |
||
assert_equal(arr, orig) | ||
|
||
|
||
class TestArange(object): | ||
def test_infinite(self): | ||
assert_raises_regex( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs more indentation to separate it from the block. Alignment isn't desirable here.