-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG: Fixed deepcopy of object arrays. #6456
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
Conversation
c34aa83
to
2112604
Compare
@@ -457,7 +457,7 @@ def test_swap_real(self, level=rlevel): | |||
|
|||
def test_object_array_from_list(self, level=rlevel): | |||
# Ticket #270 | |||
np.array([1, 'A', None]) # Should succeed |
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.
It does succeed - I just wanted it to make an assertion.
Happy to revert though.
2112604
to
2c24cbf
Compare
@@ -1426,21 +1429,44 @@ array_deepcopy(PyArrayObject *self, PyObject *args) | |||
if (deepcopy == NULL) { | |||
return NULL; | |||
} | |||
it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self); |
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.
The problem is that IterNew is iterating in C order, and optr is therefore getting out of synch.
Not that I'm against using the new iterator, but wouldn't you get the same result if you changed this line:
with
and got rid of all the |
I think you may be right. I was concerned about the fact that |
_deepcopy_call(it->dataptr, optr, PyArray_DESCR(self), deepcopy, visit); | ||
optr += PyArray_DESCR(self)->elsize; | ||
PyArray_ITER_NEXT(it); | ||
NpyIter_IterNextFunc *iternext = NpyIter_GetIterNext(iter, NULL); |
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.
This has to be declared at the beginning of the function to comply with the C89 strictness of MSVC.
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.
All declarations must go at the beginning of a block.
My personal view is that, for the new iterator to replace the old one, it needs a middle layer that makes implementing simple iterations like this one much simpler and less verbose: the alternative to the 26 lines that your PR adds to Then again, this is your PR, not mine, and there is nothing fundamentally wrong with using |
assert_equal(arr, arr_cp) | ||
self.assertTrue(arr is not arr_cp) | ||
# Ensure that we have actually copied the item. | ||
self.assertIsNot(arr[0, 1], arr_cp[1, 1]) |
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.
assertIs
and assertIsNot
are not available in Python 2.6, use assertTrue
and assertFalse
with proper arguments.
Thanks @jaimefrio and @charris - I've updated the PR based on your review. I've kept the new API implementation, but am still not averse to choosing the easier solution of updating the existing implementation (either way, we can keep the new tests). |
|
||
if (!PyArg_ParseTuple(args, "O", &visit)) { | ||
return NULL; | ||
} | ||
ret = (PyArrayObject *)PyArray_NewCopy(self, NPY_KEEPORDER); | ||
copied_array = (PyArrayObject*) PyArray_NewCopy(self, NPY_KEEPORDER); |
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.
We should be checking that copied_array
is not NULL
, and if it is, return NULL
to signal an error happened.
There seemed to be a potential memory leak in case of failure. Was already there, but would be nice if you could fix it also as part of this PR. |
Yep, that seems reasonable! I've added a commit (d158c59). Happy to squash if that is necessary. |
Additionally, if there are stylistic improvements to the code, do let me know - I'm happy to fix them, and would appreciate critique. |
d158c59
to
5ba71b1
Compare
Has this missed the v1.11 boat @charris? |
@pelson I'm going to leave this with @jaimefrio for when he gets back. Don't worry, we are shooting for branching 1.12 in April, not that far off. |
☔ The latest upstream changes (presumably #6054) made this pull request unmergeable. Please resolve the merge conflicts. |
Hmm, we should get this in. @pelson, could you rebase this? |
Thanks for taking this forward @charris. Great to have it fixed in master 👍 |
Thanks guys for fixing the issue so quickly! |
@aojeda Thanks, but this is the PR that caused your problem 8-) |
Ah ah true, I'll take it back for now then ;-) |
Fixes a bug with deepcopy of object arrays in F-order:
This was discovered as part of a bug found with biggus in SciTools/biggus#157.