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.
45 import copy_reg
as copyreg
50 import unittest2
as unittest
56 """A TestResult which forwards events to a parent object, except for Skips."""
59 unittest.TestResult.__init__(self)
72 class ReferenceLeakCheckerMixin(object):
73 """A mixin class for TestCase, which checks reference counts."""
77 def run(self, result=None):
84 super(ReferenceLeakCheckerMixin, self).
run(result=result)
85 super(ReferenceLeakCheckerMixin, self).
run(result=result)
93 super(ReferenceLeakCheckerMixin, self).
run(result=local_result)
95 refcount_deltas.append(newrefcount - oldrefcount)
96 print(refcount_deltas, self)
99 self.assertEqual(refcount_deltas, [0] * self.
NB_RUNS)
101 result.addError(self, sys.exc_info())
104 copyreg.dispatch_table.clear()
111 return sys.gettotalrefcount()
114 if hasattr(sys,
'gettotalrefcount'):
117 new_bases = (ReferenceLeakCheckerMixin,) + test_class.__bases__
118 new_class = type(test_class)(
119 test_class.__name__, new_bases, dict(test_class.__dict__))
121 SkipReferenceLeakChecker = unittest.skip