|
6 | 6 | c_char, c_wchar, c_byte, c_char_p, c_wchar_p, |
7 | 7 | c_short, c_int, c_long, c_longlong, c_void_p, |
8 | 8 | c_float, c_double, c_longdouble) |
9 | | -from test.support import import_helper, refcount_test |
| 9 | +from test.support import import_helper |
10 | 10 | _ctypes_test = import_helper.import_module("_ctypes_test") |
11 | 11 | from _ctypes import _Pointer, _SimpleCData |
12 | 12 | import gc |
| 13 | +import weakref |
13 | 14 |
|
14 | 15 |
|
15 | 16 | try: |
@@ -480,28 +481,22 @@ def original(): |
480 | 481 | self.assertEqual(original(), "original") |
481 | 482 | self.assertEqual(original(), "replacement") |
482 | 483 |
|
483 | | - @refcount_test |
484 | | - def test_function_code_object_memory_leak(self): |
| 484 | + def test_function_code_object_no_leak(self): |
485 | 485 | def get_code(i): |
486 | 486 | ns = {} |
487 | 487 | exec(f"def f(): return {i}", ns) |
488 | 488 | return ns["f"].__code__ |
489 | 489 |
|
490 | | - codes = [get_code(i) for i in range(100)] |
491 | | - refcounts = [sys.getrefcount(code) for code in codes] |
492 | | - |
493 | | - def f(): |
494 | | - pass |
495 | | - |
496 | | - for i, code in enumerate(codes): |
497 | | - f.__code__ = code |
| 490 | + refs = [] |
| 491 | + def f(): pass |
| 492 | + for i in range(100): |
| 493 | + f.__code__ = get_code(i) |
| 494 | + refs.append(weakref.ref(f.__code__)) |
498 | 495 | self.assertEqual(f(), i) |
499 | 496 |
|
500 | 497 | del f |
501 | 498 | gc.collect() |
502 | | - |
503 | | - for code, refcount in zip(codes, refcounts): |
504 | | - self.assertEqual(sys.getrefcount(code), refcount) |
| 499 | + self.assertTrue(all(ref() is None for ref in refs)) |
505 | 500 |
|
506 | 501 |
|
507 | 502 | if __name__ == '__main__': |
|
0 commit comments