(self)
| 1264 | class CodeWeakRefTest(unittest.TestCase): |
| 1265 | |
| 1266 | def test_basic(self): |
| 1267 | # Create a code object in a clean environment so that we know we have |
| 1268 | # the only reference to it left. |
| 1269 | namespace = {} |
| 1270 | exec("def f(): pass", globals(), namespace) |
| 1271 | f = namespace["f"] |
| 1272 | del namespace |
| 1273 | |
| 1274 | self.called = False |
| 1275 | def callback(code): |
| 1276 | self.called = True |
| 1277 | |
| 1278 | # f is now the last reference to the function, and through it, the code |
| 1279 | # object. While we hold it, check that we can create a weakref and |
| 1280 | # deref it. Then delete it, and check that the callback gets called and |
| 1281 | # the reference dies. |
| 1282 | coderef = weakref.ref(f.__code__, callback) |
| 1283 | self.assertTrue(bool(coderef())) |
| 1284 | del f |
| 1285 | gc_collect() # For PyPy or other GCs. |
| 1286 | self.assertFalse(bool(coderef())) |
| 1287 | self.assertTrue(self.called) |
| 1288 | |
| 1289 | # Python implementation of location table parsing algorithm |
| 1290 | def read(it): |
nothing calls this directly
no test coverage detected