Skip to content
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

BUG: fix a numpy.npiter leak in npyiter_multi_index_set #19500

Merged
merged 3 commits into from Jul 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion numpy/core/src/multiarray/nditer_pywrap.c
Expand Up @@ -1595,8 +1595,8 @@ npyiter_multi_index_set(NewNpyArrayIterObject *self, PyObject *value)
for (idim = 0; idim < ndim; ++idim) {
PyObject *v = PySequence_GetItem(value, idim);
multi_index[idim] = PyLong_AsLong(v);
Py_DECREF(v);
if (error_converting(multi_index[idim])) {
Py_XDECREF(v);
return -1;
}
}
Expand Down
23 changes: 23 additions & 0 deletions numpy/core/tests/test_nditer.py
Expand Up @@ -185,6 +185,29 @@ def test_iter_c_or_f_order():
assert_equal([x for x in i],
aview.swapaxes(0, 1).ravel(order='A'))

def test_nditer_multi_index_set():
# Test the multi_index set
a = np.arange(6).reshape(2, 3)
it = np.nditer(a, flags=['multi_index'])

# Removes the iteration on two first elements of a[0]
it.multi_index = (0, 2,)

assert_equal([i for i in it], [2, 3, 4, 5])

@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
def test_nditer_multi_index_set_refcount():
# Test if the reference count on index variable is decreased

index = 0
i = np.nditer(np.array([111, 222, 333, 444]), flags=['multi_index'])

start_count = sys.getrefcount(index)
i.multi_index = (index,)
end_count = sys.getrefcount(index)

assert_equal(start_count, end_count)

def test_iter_best_order_multi_index_1d():
# The multi-indices should be correct with any reordering

Expand Down