1 | n/a | import unittest |
---|
2 | n/a | from test import support |
---|
3 | n/a | import ctypes |
---|
4 | n/a | import gc |
---|
5 | n/a | |
---|
6 | n/a | MyCallback = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int) |
---|
7 | n/a | OtherCallback = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_ulonglong) |
---|
8 | n/a | |
---|
9 | n/a | import _ctypes_test |
---|
10 | n/a | dll = ctypes.CDLL(_ctypes_test.__file__) |
---|
11 | n/a | |
---|
12 | n/a | class RefcountTestCase(unittest.TestCase): |
---|
13 | n/a | |
---|
14 | n/a | @support.refcount_test |
---|
15 | n/a | def test_1(self): |
---|
16 | n/a | from sys import getrefcount as grc |
---|
17 | n/a | |
---|
18 | n/a | f = dll._testfunc_callback_i_if |
---|
19 | n/a | f.restype = ctypes.c_int |
---|
20 | n/a | f.argtypes = [ctypes.c_int, MyCallback] |
---|
21 | n/a | |
---|
22 | n/a | def callback(value): |
---|
23 | n/a | #print "called back with", value |
---|
24 | n/a | return value |
---|
25 | n/a | |
---|
26 | n/a | self.assertEqual(grc(callback), 2) |
---|
27 | n/a | cb = MyCallback(callback) |
---|
28 | n/a | |
---|
29 | n/a | self.assertGreater(grc(callback), 2) |
---|
30 | n/a | result = f(-10, cb) |
---|
31 | n/a | self.assertEqual(result, -18) |
---|
32 | n/a | cb = None |
---|
33 | n/a | |
---|
34 | n/a | gc.collect() |
---|
35 | n/a | |
---|
36 | n/a | self.assertEqual(grc(callback), 2) |
---|
37 | n/a | |
---|
38 | n/a | |
---|
39 | n/a | @support.refcount_test |
---|
40 | n/a | def test_refcount(self): |
---|
41 | n/a | from sys import getrefcount as grc |
---|
42 | n/a | def func(*args): |
---|
43 | n/a | pass |
---|
44 | n/a | # this is the standard refcount for func |
---|
45 | n/a | self.assertEqual(grc(func), 2) |
---|
46 | n/a | |
---|
47 | n/a | # the CFuncPtr instance holds at least one refcount on func: |
---|
48 | n/a | f = OtherCallback(func) |
---|
49 | n/a | self.assertGreater(grc(func), 2) |
---|
50 | n/a | |
---|
51 | n/a | # and may release it again |
---|
52 | n/a | del f |
---|
53 | n/a | self.assertGreaterEqual(grc(func), 2) |
---|
54 | n/a | |
---|
55 | n/a | # but now it must be gone |
---|
56 | n/a | gc.collect() |
---|
57 | n/a | self.assertEqual(grc(func), 2) |
---|
58 | n/a | |
---|
59 | n/a | class X(ctypes.Structure): |
---|
60 | n/a | _fields_ = [("a", OtherCallback)] |
---|
61 | n/a | x = X() |
---|
62 | n/a | x.a = OtherCallback(func) |
---|
63 | n/a | |
---|
64 | n/a | # the CFuncPtr instance holds at least one refcount on func: |
---|
65 | n/a | self.assertGreater(grc(func), 2) |
---|
66 | n/a | |
---|
67 | n/a | # and may release it again |
---|
68 | n/a | del x |
---|
69 | n/a | self.assertGreaterEqual(grc(func), 2) |
---|
70 | n/a | |
---|
71 | n/a | # and now it must be gone again |
---|
72 | n/a | gc.collect() |
---|
73 | n/a | self.assertEqual(grc(func), 2) |
---|
74 | n/a | |
---|
75 | n/a | f = OtherCallback(func) |
---|
76 | n/a | |
---|
77 | n/a | # the CFuncPtr instance holds at least one refcount on func: |
---|
78 | n/a | self.assertGreater(grc(func), 2) |
---|
79 | n/a | |
---|
80 | n/a | # create a cycle |
---|
81 | n/a | f.cycle = f |
---|
82 | n/a | |
---|
83 | n/a | del f |
---|
84 | n/a | gc.collect() |
---|
85 | n/a | self.assertEqual(grc(func), 2) |
---|
86 | n/a | |
---|
87 | n/a | class AnotherLeak(unittest.TestCase): |
---|
88 | n/a | def test_callback(self): |
---|
89 | n/a | import sys |
---|
90 | n/a | |
---|
91 | n/a | proto = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_int) |
---|
92 | n/a | def func(a, b): |
---|
93 | n/a | return a * b * 2 |
---|
94 | n/a | f = proto(func) |
---|
95 | n/a | |
---|
96 | n/a | a = sys.getrefcount(ctypes.c_int) |
---|
97 | n/a | f(1, 2) |
---|
98 | n/a | self.assertEqual(sys.getrefcount(ctypes.c_int), a) |
---|
99 | n/a | |
---|
100 | n/a | if __name__ == '__main__': |
---|
101 | n/a | unittest.main() |
---|