Close #19880: Fix a reference leak in unittest.TestCase. Explicitly break

reference cycles between frames and the _Outcome instance.
This commit is contained in:
Victor Stinner 2013-12-09 01:52:50 +01:00
parent 28dd6deca8
commit 031bd532c4
3 changed files with 42 additions and 1 deletions

View file

@ -1533,6 +1533,32 @@ test case
del case
self.assertFalse(wr())
def test_no_exception_leak(self):
# Issue #19880: TestCase.run() should not keep a reference
# to the exception
class MyException(Exception):
ninstance = 0
def __init__(self):
MyException.ninstance += 1
Exception.__init__(self)
def __del__(self):
MyException.ninstance -= 1
class TestCase(unittest.TestCase):
def test1(self):
raise MyException()
@unittest.expectedFailure
def test2(self):
raise MyException()
for method_name in ('test1', 'test2'):
testcase = TestCase(method_name)
testcase.run()
self.assertEqual(MyException.ninstance, 0)
if __name__ == "__main__":
unittest.main()