10000 Backport 7407, BUG: Fix decref before incref for in-place accumulate by charris · Pull Request #7412 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Backport 7407, BUG: Fix decref before incref for in-place accumulate #7412

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

Merged
merged 1 commit into from
Mar 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
BUG: Fix decref before incref for in-place accumulate
closes gh-7402
  • Loading branch information
seberg authored and charris committed Mar 12, 2016
commit e42812701d70769b5849c95ae61c3ffd794128d5
17 changes: 14 additions & 3 deletions numpy/core/src/umath/ufunc_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -3239,17 +3239,22 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out,
NPY_BEGIN_THREADS_NDITER(iter);

do {

dataptr_copy[0] = dataptr[0];
dataptr_copy[1] = dataptr[1];
dataptr_copy[2] = dataptr[0];

/* Copy the first element to start the reduction */
if (otype == NPY_OBJECT) {
/*
* Input (dataptr[0]) and output (dataptr[1]) may point
* to the same memory (i.e. np.add.accumulate(a, out=a)).
* In that case need to incref before decref to avoid the
* possibility of the reference count being zero temporarily.
*/
Py_XINCREF(*(PyObject **)dataptr_copy[1]);
Py_XDECREF(*(PyObject **)dataptr_copy[0]);
*(PyObject **)dataptr_copy[0] =
*(PyObject **)dataptr_copy[1];
Py_XINCREF(*(PyObject **)dataptr_copy[0]);
}
else {
memcpy(dataptr_copy[0], dataptr_copy[1], itemsize);
Expand Down Expand Up @@ -3302,10 +3307,16 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out,

/* Copy the first element to start the reduction */
if (otype == NPY_OBJECT) {
/*
* Input (dataptr[0]) and output (dataptr[1]) may point
* to the same memory (i.e. np.add.accumulate(a, out=a, axis=0)).
* In that case need to incref before decref to avoid the
* possibility of the reference count being zero temporarily.
*/
Py_XINCREF(*(PyObject **)dataptr_copy[1]);
Py_XDECREF(*(PyObject **)dataptr_copy[0]);
*(PyObject **)dataptr_copy[0] =
*(PyObject **)dataptr_copy[1];
Py_XINCREF(*(PyObject **)dataptr_copy[0]);
}
else {
memcpy(dataptr_copy[0], dataptr_copy[1], itemsize);
Expand Down
16 changes: 16 additions & 0 deletions numpy/core/tests/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,22 @@ def test_object_array_reduction(self):
assert_equal(np.array([[1]], dtype=object).sum(), 1)
assert_equal(np.array([[[1, 2]]], dtype=object).sum((0, 1)), [1, 2])

def test_object_array_accumulate_inplace(self):
# Checks that in-place accumulates work, see also gh-7402
arr = np.ones(4, dtype=object)
arr[:] = [[1] for i in range(4)]
# Twice reproduced also for tuples:
np.add.accumulate(arr, out=arr)
np.add.accumulate(arr, out=arr)
assert_array_equal(arr, np.array([[1]*i for i in [1, 3, 6, 10]]))

# And the same if the axis argument is used
arr = np.ones((2, 4), dtype=object)
arr[0, :] = [[2] for i in range(4)]
np.add.accumulate(arr, out=arr, axis=-1)
np.add.accumulate(arr, out=arr, axis=-1)
assert_array_equal(arr[0, :], np.array([[2]*i for i in [1, 3, 6, 10]]))

def test_object_scalar_multiply(self):
# Tickets #2469 and #4482
arr = np.matrix([1, 2], dtype=object)
Expand Down
0