diff --git a/testing/python/raises.py b/testing/python/raises.py index 2b7e926159d..4607ef32768 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -159,6 +159,7 @@ def test_raises_cyclic_reference(self, method): """ Ensure pytest.raises does not leave a reference cycle (#1965). """ + import gc class T: def __call__(self): @@ -170,7 +171,7 @@ def __call__(self): raise ValueError t = T() - refcount = sys.getrefcount(t) + refcount = len(gc.get_referrers(t)) if method == "function": pytest.raises(ValueError, t) @@ -181,7 +182,7 @@ def __call__(self): # ensure both forms of pytest.raises don't leave exceptions in sys.exc_info() assert sys.exc_info() == (None, None, None) - assert sys.getrefcount(t) == refcount + assert refcount == len(gc.get_referrers(t)) def test_raises_match(self): msg = r"with base \d+"