summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
authorBruno Oliveira <nicoddemus@gmail.com>2016-11-08 21:34:45 -0200
committerBruno Oliveira <nicoddemus@gmail.com>2016-11-08 22:20:27 -0200
commit1130b9f742d053a429149e56b8a0c8aec72a9968 (patch)
treedb4245baa65837fee4ceba8d082cabdd814b3f27 /testing
parent552c7d4286f582255752730d2c512d0aff4a44c4 (diff)
downloadpytest-1130b9f742d053a429149e56b8a0c8aec72a9968.tar.gz
Fix the stubborn test about cyclic references left by pytest.raises
In Python 2, a context manager's __exit__() leaves sys.exc_info with the exception values even when it was supposed to suppress the exception, so we explicitly call sys.exc_clear() which removes the traceback and allow the object to be released. Also updated the test to not depend on the immediate destruction of the object but instead to ensure it is not being tracked as a cyclic reference. Fix #1965
Diffstat (limited to 'testing')
-rw-r--r--testing/python/raises.py22
1 files changed, 17 insertions, 5 deletions
diff --git a/testing/python/raises.py b/testing/python/raises.py
index 80c3df2ab..8f141cfa1 100644
--- a/testing/python/raises.py
+++ b/testing/python/raises.py
@@ -1,4 +1,6 @@
import pytest
+import sys
+
class TestRaises:
def test_raises(self):
@@ -98,10 +100,13 @@ class TestRaises:
assert False, "Expected pytest.raises.Exception"
@pytest.mark.parametrize('method', ['function', 'with'])
- def test_raises_memoryleak(self, method):
- import weakref
+ def test_raises_cyclic_reference(self, method):
+ """
+ Ensure pytest.raises does not leave a reference cycle (#1965).
+ """
+ import gc
- class T:
+ class T(object):
def __call__(self):
raise ValueError
@@ -112,5 +117,12 @@ class TestRaises:
with pytest.raises(ValueError):
t()
- t = weakref.ref(t)
- assert t() is None
+ # ensure both forms of pytest.raises don't leave exceptions in sys.exc_info()
+ assert sys.exc_info() == (None, None, None)
+
+ del t
+
+ # ensure the t instance is not stuck in a cyclic reference
+ for o in gc.get_objects():
+ assert type(o) is not T
+