(self)
| 1045 | self.assertNotRegex(stderr, "_Py_Dealloc: Deallocator of type 'TestObj'") |
| 1046 | |
| 1047 | def test_clearing_weakrefs_in_gc(self): |
| 1048 | # This test checks that when finalizers are called: |
| 1049 | # 1. weakrefs with callbacks have been cleared |
| 1050 | # 2. weakrefs without callbacks have not been cleared |
| 1051 | errors = [] |
| 1052 | def test(): |
| 1053 | class Class: |
| 1054 | def __init__(self): |
| 1055 | self._self = self |
| 1056 | self.wr1 = weakref.ref(Class, lambda x: None) |
| 1057 | self.wr2 = weakref.ref(Class) |
| 1058 | |
| 1059 | def __del__(self): |
| 1060 | # we can't use assert* here, because gc will swallow |
| 1061 | # exceptions |
| 1062 | if self.wr1() is not None: |
| 1063 | errors.append("weakref with callback as cleared") |
| 1064 | if self.wr2() is not Class: |
| 1065 | errors.append("weakref without callback was cleared") |
| 1066 | |
| 1067 | Class() |
| 1068 | |
| 1069 | test() |
| 1070 | gc.collect() |
| 1071 | self.assertEqual(errors, []) |
| 1072 | |
| 1073 | |
| 1074 | class SubclassableWeakrefTestCase(TestBase): |
nothing calls this directly
no test coverage detected