'''Test case for "self.__dict__ = self" circular reference bug (#1469629)''' from test import test_support import unittest import gc class LeakyDict(dict): def __init__(self): dict.__init__(self) self.__dict__ = self class DictSelfLeakTestCase(unittest.TestCase): def test_for_dict_eq_self_leak(self): runs = 1000 gc.collect(); gc.collect(); gc.collect(); objcount_before = len(gc.get_objects()) for x in range(runs): a = LeakyDict() del x, a # to keep object count constant gc.collect(); gc.collect(); gc.collect() objcount_after = len(gc.get_objects()) # so assertion error traceback gets the actual assertion error_string = "leak found: %d new objects in %d runs " % \ (runs, objcount_after - objcount_before) assert objcount_after == objcount_before, error_string def test_main(): test_support.run_unittest(DictSelfLeakTestCase) if __name__ == '__main__': test_main()