@@ -3270,17 +3270,22 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out,
3270
3270
NPY_BEGIN_THREADS_NDITER (iter );
3271
3271
3272
3272
do {
3273
-
3274
3273
dataptr_copy [0 ] = dataptr [0 ];
3275
3274
dataptr_copy [1 ] = dataptr [1 ];
3276
3275
dataptr_copy [2 ] = dataptr [0 ];
3277
3276
3278
3277
/* Copy the first element to start the reduction */
3279
3278
if (otype == NPY_OBJECT ) {
3279
+ /*
3280
+ * Input (dataptr[0]) and output (dataptr[1]) may point
3281
+ * to the same memory (i.e. np.add.accumulate(a, out=a)).
3282
+ * In that case need to incref before decref to avoid the
3283
+ * possibility of the reference count being zero temporarily.
3284
+ */
3285
+ Py_XINCREF (* (PyObject * * )dataptr_copy [1 ]);
3280
3286
Py_XDECREF (* (PyObject * * )dataptr_copy [0 ]);
3281
3287
* (PyObject * * )dataptr_copy [0 ] =
3282
3288
* (PyObject * * )dataptr_copy [1 ];
3283
- Py_XINCREF (* (PyObject * * )dataptr_copy [0 ]);
3284
3289
}
3285
3290
else {
3286
3291
memcpy (dataptr_copy [0 ], dataptr_copy [1 ], itemsize );
@@ -3333,10 +3338,16 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out,
3333
3338
3334
3339
/* Copy the first element to start the reduction */
3335
3340
if (otype == NPY_OBJECT ) {
3341
+ /*
3342
+ * Input (dataptr[0]) and output (dataptr[1]) may point
3343
+ * to the same memory (i.e. np.add.accumulate(a, out=a, axis=0)).
3344
+ * In that case need to incref before decref to avoid the
3345
+ * possibility of the reference count being zero temporarily.
3346
+ */
3347
+ Py_XINCREF (* (PyObject * * )dataptr_copy [1 ]);
3336
3348
Py_XDECREF (* (PyObject * * )dataptr_copy [0 ]);
3337
3349
* (PyObject * * )dataptr_copy [0 ] =
3338
3350
* (PyObject * * )dataptr_copy [1 ];
3339
- Py_XINCREF (* (PyObject * * )dataptr_copy [0 ]);
3340
3351
}
3341
3352
else {
3342
3353
memcpy (dataptr_copy [0 ], dataptr_copy [1 ], itemsize );
0 commit comments