Skip to content

Commit 3bd9113

Browse files
committed
fix iadd pythonization for np/array buffers
1 parent c6d623a commit 3bd9113

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/Pythonize.cxx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,20 @@ PyObject* VectorIAdd(PyObject* self, PyObject* args, PyObject* /* kwds */)
448448
if (PyObject_CheckBuffer(fi) && !(CPyCppyy_PyText_Check(fi) || PyBytes_Check(fi))) {
449449
PyObject* vend = PyObject_CallMethodNoArgs(self, PyStrings::gEnd);
450450
if (vend) {
451-
PyObject* result = PyObject_CallMethodObjArgs(self, PyStrings::gInsert, vend, fi, nullptr);
451+
// when __iadd__ is overriden, the operation does not end with
452+
// calling the __iadd__ method, but also assigns the result to the
453+
// lhs of the iadd. For example, performing vec += arr, Python
454+
// first calls our override, and then does vec = vec.iadd(arr).
455+
PyObject *it = PyObject_CallMethodObjArgs(self, PyStrings::gInsert, vend, fi, nullptr);
452456
Py_DECREF(vend);
453-
return result;
457+
458+
if (!it)
459+
return nullptr;
460+
461+
Py_DECREF(it);
462+
// Assign the result of the __iadd__ override to the std::vector
463+
Py_INCREF(self);
464+
return self;
454465
}
455466
}
456467
}

0 commit comments

Comments
 (0)