Skip to content

Commit 18a10e6

Browse files
committed
TST: Make test_partial_iteration_cleanup robust but require leak checker
This makes sure the test is not flaky, but the test now requires a leak checker (both valgrind or reference count based should work in CPython at least). Closes numpygh-21169
1 parent 13d55a3 commit 18a10e6

1 file changed

Lines changed: 9 additions & 13 deletions

File tree

‎numpy/core/tests/test_nditer.py‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3169,41 +3169,37 @@ def test_warn_noclose():
31693169

31703170
@pytest.mark.skipif(sys.version_info[:2] == (3, 9) and sys.platform == "win32",
31713171
reason="Errors with Python 3.9 on Windows")
3172-
@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
31733172
@pytest.mark.parametrize(["in_dtype", "buf_dtype"],
31743173
[("i", "O"), ("O", "i"), # most simple cases
31753174
("i,O", "O,O"), # structured partially only copying O
31763175
("O,i", "i,O"), # structured casting to and from O
31773176
])
31783177
@pytest.mark.parametrize("steps", [1, 2, 3])
31793178
def test_partial_iteration_cleanup(in_dtype, buf_dtype, steps):
3180-
value = 123 # relies on python cache (leak-check will still find it)
3179+
"""
3180+
Checks for reference counting leaks during cleanup. Using explicit
3181+
reference counts lead to occasional false positives (at least in parallel
3182+
test setups). This test now should still test leaks correctly when
3183+
run e.g. with pytest-valgrind or pytest-leaks
3184+
"""
3185+
value = 2**30 + 1 # just a random value that Python won't intern
31813186
arr = np.full(int(np.BUFSIZE * 2.5), value).astype(in_dtype)
3182-
count = sys.getrefcount(value)
31833187

31843188
it = np.nditer(arr, op_dtypes=[np.dtype(buf_dtype)],
31853189
flags=["buffered", "external_loop", "refs_ok"], casting="unsafe")
31863190
for step in range(steps):
31873191
# The iteration finishes in 3 steps, the first two are partial
31883192
next(it)
31893193

3190-
# Note that resetting does not free references
3191-
del it
3192-
break_cycles()
3193-
break_cycles()
3194-
assert count == sys.getrefcount(value)
3194+
del it # not necessary, but we test the cleanup
31953195

31963196
# Repeat the test with `iternext`
31973197
it = np.nditer(arr, op_dtypes=[np.dtype(buf_dtype)],
31983198
flags=["buffered", "external_loop", "refs_ok"], casting="unsafe")
31993199
for step in range(steps):
32003200
it.iternext()
32013201

3202-
del it # should ensure cleanup
3203-
break_cycles()
3204-
break_cycles()
3205-
assert count == sys.getrefcount(value)
3206-
3202+
del it # not necessary, but we test the cleanup
32073203

32083204
@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
32093205
@pytest.mark.parametrize(["in_dtype", "buf_dtype"],

0 commit comments

Comments
 (0)