8000 Merge pull request #6259 from charris/backport-gh-6254 · numpy/numpy@3e41359 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e41359

Browse files
committed
Merge pull request #6259 from charris/backport-gh-6254
BUG: Fix refcounting for string comparison in array_richcompare
2 parents b49adda + 2c74891 commit 3e41359

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

numpy/core/src/multiarray/arrayobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,9 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
13111311
/* Never mind, carry on, see what happens */
13121312
}
13131313
else {
1314-
return _strings_richcompare(self, array_other, cmp_op, 0);
1314+
result = _strings_richcompare(self, array_other, cmp_op, 0);
1315+
Py_DECREF(array_other);
1316+
return result;
13151317
}
13161318
/* If we reach this point, it means that we are not comparing
13171319
* string-to-string. It's possible that this will still work out,

numpy/core/tests/test_regression.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,6 +2152,26 @@ def f(x):
21522152
assert_equal(uf(a), ())
21532153
assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]])
21542154

2155+
def test_leak_in_structured_dtype_comparison(self):
2156+
# gh-6250
2157+
recordtype = np.dtype([('a', np.float64),
2158+
('b', np.int32),
2159+
('d', (np.str, 5))])
2160+
2161+
# Simple case
2162+
a = np.zeros(2, dtype=recordtype)
2163+
for i in range(100):
2164+
a == a
2165+
assert_(sys.getrefcount(a) < 10)
2166+
2167+
# The case in the bug report.
2168+
before = sys.getrefcount(a)
2169+
u, v = a[0], a[1]
2170+
u == v
2171+
del u, v
2172+
gc.collect()
2173+
after = sys.getrefcount(a)
2174+
assert_equal(before, after)
21552175

21562176
if __name__ == "__main__":
21572177
run_module_suite()

0 commit comments

Comments
 (0)
0