Debian Codegraph @

Lib/test/test_ctypes/test_cfuncs.py

raw
1 import ctypes
2 import unittest
3 from ctypes import (CDLL,
4 c_byte, c_ubyte, c_char,
5 c_short, c_ushort, c_int, c_uint,
6 c_long, c_ulong, c_longlong, c_ulonglong,
7 c_float, c_double, c_longdouble)
8 from test import support
9 from test.support import import_helper, threading_helper
10 _ctypes_test = import_helper.import_module("_ctypes_test")
11
12
13 class CFunctions(unittest.TestCase):
14 _dll = CDLL(_ctypes_test.__file__)
15
16 def S(self):
17 return _ctypes_test.get_last_tf_arg_s()
18
19 def U(self):
20 return _ctypes_test.get_last_tf_arg_u()
21
22 def test_byte(self):
23 self._dll.tf_b.restype = c_byte
24 self._dll.tf_b.argtypes = (c_byte,)
25 self.assertEqual(self._dll.tf_b(-126), -42)
26 self.assertEqual(self.S(), -126)
27
28 def test_byte_plus(self):
29 self._dll.tf_bb.restype = c_byte
30 self._dll.tf_bb.argtypes = (c_byte, c_byte)
31 self.assertEqual(self._dll.tf_bb(0, -126), -42)
32 self.assertEqual(self.S(), -126)
33
34 def test_ubyte(self):
35 self._dll.tf_B.restype = c_ubyte
36 self._dll.tf_B.argtypes = (c_ubyte,)
37 self.assertEqual(self._dll.tf_B(255), 85)
38 self.assertEqual(self.U(), 255)
39
40 def test_ubyte_plus(self):
41 self._dll.tf_bB.restype = c_ubyte
42 self._dll.tf_bB.argtypes = (c_byte, c_ubyte)
43 self.assertEqual(self._dll.tf_bB(0, 255), 85)
44 self.assertEqual(self.U(), 255)
45
46 def test_short(self):
47 self._dll.tf_h.restype = c_short
48 self._dll.tf_h.argtypes = (c_short,)
49 self.assertEqual(self._dll.tf_h(-32766), -10922)
50 self.assertEqual(self.S(), -32766)
51
52 def test_short_plus(self):
53 self._dll.tf_bh.restype = c_short
54 self._dll.tf_bh.argtypes = (c_byte, c_short)
55 self.assertEqual(self._dll.tf_bh(0, -32766), -10922)
56 self.assertEqual(self.S(), -32766)
57
58 def test_ushort(self):
59 self._dll.tf_H.restype = c_ushort
60 self._dll.tf_H.argtypes = (c_ushort,)
61 self.assertEqual(self._dll.tf_H(65535), 21845)
62 self.assertEqual(self.U(), 65535)
63
64 def test_ushort_plus(self):
65 self._dll.tf_bH.restype = c_ushort
66 self._dll.tf_bH.argtypes = (c_byte, c_ushort)
67 self.assertEqual(self._dll.tf_bH(0, 65535), 21845)
68 self.assertEqual(self.U(), 65535)
69
70 def test_int(self):
71 self._dll.tf_i.restype = c_int
72 self._dll.tf_i.argtypes = (c_int,)
73 self.assertEqual(self._dll.tf_i(-2147483646), -715827882)
74 self.assertEqual(self.S(), -2147483646)
75
76 def test_int_plus(self):
77 self._dll.tf_bi.restype = c_int
78 self._dll.tf_bi.argtypes = (c_byte, c_int)
79 self.assertEqual(self._dll.tf_bi(0, -2147483646), -715827882)
80 self.assertEqual(self.S(), -2147483646)
81
82 def test_uint(self):
83 self._dll.tf_I.restype = c_uint
84 self._dll.tf_I.argtypes = (c_uint,)
85 self.assertEqual(self._dll.tf_I(4294967295), 1431655765)
86 self.assertEqual(self.U(), 4294967295)
87
88 def test_uint_plus(self):
89 self._dll.tf_bI.restype = c_uint
90 self._dll.tf_bI.argtypes = (c_byte, c_uint)
91 self.assertEqual(self._dll.tf_bI(0, 4294967295), 1431655765)
92 self.assertEqual(self.U(), 4294967295)
93
94 def test_long(self):
95 self._dll.tf_l.restype = c_long
96 self._dll.tf_l.argtypes = (c_long,)
97 self.assertEqual(self._dll.tf_l(-2147483646), -715827882)
98 self.assertEqual(self.S(), -2147483646)
99
100 def test_long_plus(self):
101 self._dll.tf_bl.restype = c_long
102 self._dll.tf_bl.argtypes = (c_byte, c_long)
103 self.assertEqual(self._dll.tf_bl(0, -2147483646), -715827882)
104 self.assertEqual(self.S(), -2147483646)
105
106 def test_ulong(self):
107 self._dll.tf_L.restype = c_ulong
108 self._dll.tf_L.argtypes = (c_ulong,)
109 self.assertEqual(self._dll.tf_L(4294967295), 1431655765)
110 self.assertEqual(self.U(), 4294967295)
111
112 def test_ulong_plus(self):
113 self._dll.tf_bL.restype = c_ulong
114 self._dll.tf_bL.argtypes = (c_char, c_ulong)
115 self.assertEqual(self._dll.tf_bL(b' ', 4294967295), 1431655765)
116 self.assertEqual(self.U(), 4294967295)
117
118 def test_longlong(self):
119 self._dll.tf_q.restype = c_longlong
120 self._dll.tf_q.argtypes = (c_longlong, )
121 self.assertEqual(self._dll.tf_q(-9223372036854775806), -3074457345618258602)
122 self.assertEqual(self.S(), -9223372036854775806)
123
124 def test_longlong_plus(self):
125 self._dll.tf_bq.restype = c_longlong
126 self._dll.tf_bq.argtypes = (c_byte, c_longlong)
127 self.assertEqual(self._dll.tf_bq(0, -9223372036854775806), -3074457345618258602)
128 self.assertEqual(self.S(), -9223372036854775806)
129
130 def test_ulonglong(self):
131 self._dll.tf_Q.restype = c_ulonglong
132 self._dll.tf_Q.argtypes = (c_ulonglong, )
133 self.assertEqual(self._dll.tf_Q(18446744073709551615), 6148914691236517205)
134 self.assertEqual(self.U(), 18446744073709551615)
135
136 def test_ulonglong_plus(self):
137 self._dll.tf_bQ.restype = c_ulonglong
138 self._dll.tf_bQ.argtypes = (c_byte, c_ulonglong)
139 self.assertEqual(self._dll.tf_bQ(0, 18446744073709551615), 6148914691236517205)
140 self.assertEqual(self.U(), 18446744073709551615)
141
142 def test_float(self):
143 self._dll.tf_f.restype = c_float
144 self._dll.tf_f.argtypes = (c_float,)
145 self.assertEqual(self._dll.tf_f(-42.), -14.)
146 self.assertEqual(self.S(), -42)
147
148 def test_float_plus(self):
149 self._dll.tf_bf.restype = c_float
150 self._dll.tf_bf.argtypes = (c_byte, c_float)
151 self.assertEqual(self._dll.tf_bf(0, -42.), -14.)
152 self.assertEqual(self.S(), -42)
153
154 def test_double(self):
155 self._dll.tf_d.restype = c_double
156 self._dll.tf_d.argtypes = (c_double,)
157 self.assertEqual(self._dll.tf_d(42.), 14.)
158 self.assertEqual(self.S(), 42)
159
160 def test_double_plus(self):
161 self._dll.tf_bd.restype = c_double
162 self._dll.tf_bd.argtypes = (c_byte, c_double)
163 self.assertEqual(self._dll.tf_bd(0, 42.), 14.)
164 self.assertEqual(self.S(), 42)
165
166 def test_longdouble(self):
167 self._dll.tf_D.restype = c_longdouble
168 self._dll.tf_D.argtypes = (c_longdouble,)
169 self.assertEqual(self._dll.tf_D(42.), 14.)
170 self.assertEqual(self.S(), 42)
171
172 def test_longdouble_plus(self):
173 self._dll.tf_bD.restype = c_longdouble
174 self._dll.tf_bD.argtypes = (c_byte, c_longdouble)
175 self.assertEqual(self._dll.tf_bD(0, 42.), 14.)
176 self.assertEqual(self.S(), 42)
177
178 def test_callwithresult(self):
179 def process_result(result):
180 return result * 2
181 self._dll.tf_i.restype = process_result
182 self._dll.tf_i.argtypes = (c_int,)
183 self.assertEqual(self._dll.tf_i(42), 28)
184 self.assertEqual(self.S(), 42)
185 self.assertEqual(self._dll.tf_i(-42), -28)
186 self.assertEqual(self.S(), -42)
187
188 def test_void(self):
189 self._dll.tv_i.restype = None
190 self._dll.tv_i.argtypes = (c_int,)
191 self.assertEqual(self._dll.tv_i(42), None)
192 self.assertEqual(self.S(), 42)
193 self.assertEqual(self._dll.tv_i(-42), None)
194 self.assertEqual(self.S(), -42)
195
196 @threading_helper.requires_working_threading()
197 @support.requires_resource("cpu")
198 @unittest.skipUnless(support.Py_GIL_DISABLED, "only meaningful on free-threading")
199 def test_thread_safety(self):
200 from threading import Thread
201
202 def concurrent():
203 for _ in range(100):
204 self._dll.tf_b.restype = c_byte
205 self._dll.tf_b.argtypes = (c_byte,)
206
207 with threading_helper.catch_threading_exception() as exc:
208 with threading_helper.start_threads((Thread(target=concurrent) for _ in range(10))):
209 pass
210
211 self.assertIsNone(exc.exc_value)
212
213
214 # The following repeats the above tests with stdcall functions (where
215 # they are available)
216 if hasattr(ctypes, 'WinDLL'):
217 class stdcall_dll(ctypes.WinDLL):
218 def __getattr__(self, name):
219 if name[:2] == '__' and name[-2:] == '__':
220 raise AttributeError(name)
221 func = self._FuncPtr(("s_" + name, self))
222 setattr(self, name, func)
223 return func
224
225 class stdcallCFunctions(CFunctions):
226 _dll = stdcall_dll(_ctypes_test.__file__)
227
228
229 if __name__ == '__main__':
230 unittest.main()