Debian Codegraph @

Lib/test/test_ctypes/test_refcounts.py

raw
1 import ctypes
2 import gc
3 import sys
4 import unittest
5 from test import support
6 from test.support import import_helper
7 from test.support import script_helper
8 _ctypes_test = import_helper.import_module("_ctypes_test")
9
10
11 MyCallback = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)
12 OtherCallback = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_ulonglong)
13
14 dll = ctypes.CDLL(_ctypes_test.__file__)
15
16
17 class RefcountTestCase(unittest.TestCase):
18 @support.refcount_test
19 def test_1(self):
20 f = dll._testfunc_callback_i_if
21 f.restype = ctypes.c_int
22 f.argtypes = [ctypes.c_int, MyCallback]
23
24 def callback(value):
25 return value
26
27 self.assertEqual(sys.getrefcount(callback), 2)
28 cb = MyCallback(callback)
29
30 self.assertGreater(sys.getrefcount(callback), 2)
31 result = f(-10, cb)
32 self.assertEqual(result, -18)
33 cb = None
34
35 gc.collect()
36
37 self.assertEqual(sys.getrefcount(callback), 2)
38
39 @support.refcount_test
40 def test_refcount(self):
41 def func(*args):
42 pass
43 # this is the standard refcount for func
44 self.assertEqual(sys.getrefcount(func), 2)
45
46 # the CFuncPtr instance holds at least one refcount on func:
47 f = OtherCallback(func)
48 self.assertGreater(sys.getrefcount(func), 2)
49
50 # and may release it again
51 del f
52 self.assertGreaterEqual(sys.getrefcount(func), 2)
53
54 # but now it must be gone
55 gc.collect()
56 self.assertEqual(sys.getrefcount(func), 2)
57
58 class X(ctypes.Structure):
59 _fields_ = [("a", OtherCallback)]
60 x = X()
61 x.a = OtherCallback(func)
62
63 # the CFuncPtr instance holds at least one refcount on func:
64 self.assertGreater(sys.getrefcount(func), 2)
65
66 # and may release it again
67 del x
68 self.assertGreaterEqual(sys.getrefcount(func), 2)
69
70 # and now it must be gone again
71 gc.collect()
72 self.assertEqual(sys.getrefcount(func), 2)
73
74 f = OtherCallback(func)
75
76 # the CFuncPtr instance holds at least one refcount on func:
77 self.assertGreater(sys.getrefcount(func), 2)
78
79 # create a cycle
80 f.cycle = f
81
82 del f
83 gc.collect()
84 self.assertEqual(sys.getrefcount(func), 2)
85
86
87 class AnotherLeak(unittest.TestCase):
88 def test_callback(self):
89 proto = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_int)
90 def func(a, b):
91 return a * b * 2
92 f = proto(func)
93
94 a = sys.getrefcount(ctypes.c_int)
95 f(1, 2)
96 self.assertEqual(sys.getrefcount(ctypes.c_int), a)
97
98 @support.refcount_test
99 def test_callback_py_object_none_return(self):
100 # bpo-36880: test that returning None from a py_object callback
101 # does not decrement the refcount of None.
102
103 for FUNCTYPE in (ctypes.CFUNCTYPE, ctypes.PYFUNCTYPE):
104 with self.subTest(FUNCTYPE=FUNCTYPE):
105 @FUNCTYPE(ctypes.py_object)
106 def func():
107 return None
108
109 # Check that calling func does not affect None's refcount.
110 for _ in range(10000):
111 func()
112
113
114 class ModuleIsolationTest(unittest.TestCase):
115 def test_finalize(self):
116 # check if gc_decref() succeeds
117 script = (
118 "import ctypes;"
119 "import sys;"
120 "del sys.modules['_ctypes'];"
121 "import _ctypes;"
122 "exit()"
123 )
124 script_helper.assert_python_ok("-c", script)
125
126
127 class PyObjectRestypeTest(unittest.TestCase):
128 def test_restype_py_object_with_null_return(self):
129 # Test that a function which returns a NULL PyObject *
130 # without setting an exception does not crash.
131 PyErr_Occurred = ctypes.pythonapi.PyErr_Occurred
132 PyErr_Occurred.argtypes = []
133 PyErr_Occurred.restype = ctypes.py_object
134
135 # At this point, there's no exception set, so PyErr_Occurred
136 # returns NULL. Given the restype is py_object, the
137 # ctypes machinery will raise a custom error.
138 with self.assertRaisesRegex(ValueError, "PyObject is NULL"):
139 PyErr_Occurred()
140
141
142 if __name__ == '__main__':
143 unittest.main()