From 8a659e7795178a5cda84d9d708ee62b267c32741 Mon Sep 17 00:00:00 2001 From: Hugo Defois Date: Fri, 2 Jul 2021 15:52:46 +0200 Subject: [PATCH 1/3] BUG: fix a numpy.npiter leak in npyiter_multi_index_set --- numpy/core/src/multiarray/nditer_pywrap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/core/src/multiarray/nditer_pywrap.c b/numpy/core/src/multiarray/nditer_pywrap.c index 7698ae43d07e..73df962e4520 100644 --- a/numpy/core/src/multiarray/nditer_pywrap.c +++ b/numpy/core/src/multiarray/nditer_pywrap.c @@ -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; } } From a275b5130d2ac1d4edf2be482549e77c131eb724 Mon Sep 17 00:00:00 2001 From: Hugo Defois Date: Sat, 3 Jul 2021 17:42:17 +0200 Subject: [PATCH 2/3] TST: add multi_index set tests on non-error cases --- numpy/core/tests/test_nditer.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/numpy/core/tests/test_nditer.py b/numpy/core/tests/test_nditer.py index b44343c5755c..b77a276c8248 100644 --- a/numpy/core/tests/test_nditer.py +++ b/numpy/core/tests/test_nditer.py @@ -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 inde 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 From 96dcd082ef7f7c2c6123f8c4e7449f3b1abc32c4 Mon Sep 17 00:00:00 2001 From: Matti Picus Date: Tue, 6 Jul 2021 13:05:06 +0300 Subject: [PATCH 3/3] ENH: fix typo --- numpy/core/tests/test_nditer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/core/tests/test_nditer.py b/numpy/core/tests/test_nditer.py index b77a276c8248..adcf921f6016 100644 --- a/numpy/core/tests/test_nditer.py +++ b/numpy/core/tests/test_nditer.py @@ -197,7 +197,7 @@ def test_nditer_multi_index_set(): @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts") def test_nditer_multi_index_set_refcount(): - # Test if the reference count on inde variable is decreased + # Test if the reference count on index variable is decreased index = 0 i = np.nditer(np.array([111, 222, 333, 444]), flags=['multi_index'])