Skip to content

Commit 5262271

Browse files
committed
fix tests
1 parent 6c27db7 commit 5262271

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

Lib/test/test_ctypes/test_functions.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
c_char, c_wchar, c_byte, c_char_p, c_wchar_p,
77
c_short, c_int, c_long, c_longlong, c_void_p,
88
c_float, c_double, c_longdouble)
9-
from test.support import import_helper, refcount_test
9+
from test.support import import_helper
1010
_ctypes_test = import_helper.import_module("_ctypes_test")
1111
from _ctypes import _Pointer, _SimpleCData
1212
import gc
13+
import weakref
1314

1415

1516
try:
@@ -480,28 +481,22 @@ def original():
480481
self.assertEqual(original(), "original")
481482
self.assertEqual(original(), "replacement")
482483

483-
@refcount_test
484-
def test_function_code_object_memory_leak(self):
484+
def test_function_code_object_no_leak(self):
485485
def get_code(i):
486486
ns = {}
487487
exec(f"def f(): return {i}", ns)
488488
return ns["f"].__code__
489489

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__))
498495
self.assertEqual(f(), i)
499496

500497
del f
501498
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))
505500

506501

507502
if __name__ == '__main__':

0 commit comments

Comments
 (0)