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