31 """A subclass of unittest.TestCase which checks for reference leaks.
34 - Use testing_refleak.BaseTestCase instead of unittest.TestCase
35 - Configure and compile Python with --with-pydebug
37 If sys.gettotalrefcount() is not available (because Python was built without
38 the Py_DEBUG option), then this module is a no-op and tests will run normally.
47 class LocalTestResult(unittest.TestResult):
48 """A TestResult which forwards events to a parent object, except for Skips."""
51 unittest.TestResult.__init__(self)
64 class ReferenceLeakCheckerMixin(object):
65 """A mixin class for TestCase, which checks reference counts."""
69 def run(self, result=None):
76 super(ReferenceLeakCheckerMixin, self).
run(result=result)
77 super(ReferenceLeakCheckerMixin, self).
run(result=result)
85 super(ReferenceLeakCheckerMixin, self).
run(result=local_result)
87 refcount_deltas.append(newrefcount - oldrefcount)
88 print(refcount_deltas, self)
91 self.assertEqual(refcount_deltas, [0] * self.
NB_RUNS)
93 result.addError(self, sys.exc_info())
96 copyreg.dispatch_table.clear()
103 return sys.gettotalrefcount()
106 if hasattr(sys,
'gettotalrefcount'):
109 new_bases = (ReferenceLeakCheckerMixin,) + test_class.__bases__
110 new_class =
type(test_class)(
111 test_class.__name__, new_bases, dict(test_class.__dict__))
113 SkipReferenceLeakChecker = unittest.skip