fix iadd pythonization for np/array buffers#51
Conversation
|
@wlav Apologies for pinging you again on this, would be great to get a review on this when possible. Thanks! |
|
Don't change the indentation convention (the patch goes to 2 spaces instead of 4 for some reason). If the For the last comment, the change is to allow the assignment of the result in case of an override, not to prevent an assignment. Aside, since the assignment is to |
Thanks! I have addressed these comments.
I am unsure how Python handles that... but yes, that would make sense if the reassignment causes the original vector to be garbage collected, without an extra reference to prevent that (if I understand correctly) |
Based on root-project/root#18768, pinging @wlav cc @guitargeek
Currently, the
iaddimplementation forstd::vectorinPythonize.cxxworks perfectly with Python lists and iterables for which we have anItemGetter.But the second code path for buffers:
CPyCppyy/src/Pythonize.cxx
Lines 444 to 456 in c6d623a
which we hit when i-adding an
array.arrayornp.array, does not work as expected:If I try the equivalent by directly dispatching to
std::vector::insertit works:here, the difference between
x+=yandx.__iadd__(y)leads to unintuitive behaviour: if the users intention is to obtain the resulting iterator when calling insert, they can dod.insert(d.end(), a)which makes more sense than expecting it fromd+=a. When the user does calld+=a, since__iadd__is overriden, operation does not end with the call toVectorIAddwhich should just return the iterator, and thestd::vectord should reflect the change by insertion . Instead, it ends withd = d.__iadd__(a)which reassigns the vector to the returned iterator, losing the vector in the process.This PR fixes this issue by returning
selfinstead.