ยปCore Development>Code coverage>Lib/ctypes/test/test_functions.py

Python code coverage for Lib/ctypes/test/test_functions.py

#countcontent
1n/a"""
2n/aHere is probably the place to write the docs, since the test-cases
3n/ashow how the type behave.
4n/a
5n/aLater...
6n/a"""
7n/a
8n/afrom ctypes import *
9n/afrom ctypes.test import need_symbol
10n/aimport sys, unittest
11n/a
12n/atry:
13n/a WINFUNCTYPE
14n/aexcept NameError:
15n/a # fake to enable this test on Linux
16n/a WINFUNCTYPE = CFUNCTYPE
17n/a
18n/aimport _ctypes_test
19n/adll = CDLL(_ctypes_test.__file__)
20n/aif sys.platform == "win32":
21n/a windll = WinDLL(_ctypes_test.__file__)
22n/a
23n/aclass POINT(Structure):
24n/a _fields_ = [("x", c_int), ("y", c_int)]
25n/aclass RECT(Structure):
26n/a _fields_ = [("left", c_int), ("top", c_int),
27n/a ("right", c_int), ("bottom", c_int)]
28n/aclass FunctionTestCase(unittest.TestCase):
29n/a
30n/a def test_mro(self):
31n/a # in Python 2.3, this raises TypeError: MRO conflict among bases classes,
32n/a # in Python 2.2 it works.
33n/a #
34n/a # But in early versions of _ctypes.c, the result of tp_new
35n/a # wasn't checked, and it even crashed Python.
36n/a # Found by Greg Chapman.
37n/a
38n/a try:
39n/a class X(object, Array):
40n/a _length_ = 5
41n/a _type_ = "i"
42n/a except TypeError:
43n/a pass
44n/a
45n/a
46n/a from _ctypes import _Pointer
47n/a try:
48n/a class X(object, _Pointer):
49n/a pass
50n/a except TypeError:
51n/a pass
52n/a
53n/a from _ctypes import _SimpleCData
54n/a try:
55n/a class X(object, _SimpleCData):
56n/a _type_ = "i"
57n/a except TypeError:
58n/a pass
59n/a
60n/a try:
61n/a class X(object, Structure):
62n/a _fields_ = []
63n/a except TypeError:
64n/a pass
65n/a
66n/a
67n/a @need_symbol('c_wchar')
68n/a def test_wchar_parm(self):
69n/a f = dll._testfunc_i_bhilfd
70n/a f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
71n/a result = f(1, "x", 3, 4, 5.0, 6.0)
72n/a self.assertEqual(result, 139)
73n/a self.assertEqual(type(result), int)
74n/a
75n/a @need_symbol('c_wchar')
76n/a def test_wchar_result(self):
77n/a f = dll._testfunc_i_bhilfd
78n/a f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
79n/a f.restype = c_wchar
80n/a result = f(0, 0, 0, 0, 0, 0)
81n/a self.assertEqual(result, '\x00')
82n/a
83n/a def test_voidresult(self):
84n/a f = dll._testfunc_v
85n/a f.restype = None
86n/a f.argtypes = [c_int, c_int, POINTER(c_int)]
87n/a result = c_int()
88n/a self.assertEqual(None, f(1, 2, byref(result)))
89n/a self.assertEqual(result.value, 3)
90n/a
91n/a def test_intresult(self):
92n/a f = dll._testfunc_i_bhilfd
93n/a f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
94n/a f.restype = c_int
95n/a result = f(1, 2, 3, 4, 5.0, 6.0)
96n/a self.assertEqual(result, 21)
97n/a self.assertEqual(type(result), int)
98n/a
99n/a result = f(-1, -2, -3, -4, -5.0, -6.0)
100n/a self.assertEqual(result, -21)
101n/a self.assertEqual(type(result), int)
102n/a
103n/a # If we declare the function to return a short,
104n/a # is the high part split off?
105n/a f.restype = c_short
106n/a result = f(1, 2, 3, 4, 5.0, 6.0)
107n/a self.assertEqual(result, 21)
108n/a self.assertEqual(type(result), int)
109n/a
110n/a result = f(1, 2, 3, 0x10004, 5.0, 6.0)
111n/a self.assertEqual(result, 21)
112n/a self.assertEqual(type(result), int)
113n/a
114n/a # You cannot assign character format codes as restype any longer
115n/a self.assertRaises(TypeError, setattr, f, "restype", "i")
116n/a
117n/a def test_floatresult(self):
118n/a f = dll._testfunc_f_bhilfd
119n/a f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
120n/a f.restype = c_float
121n/a result = f(1, 2, 3, 4, 5.0, 6.0)
122n/a self.assertEqual(result, 21)
123n/a self.assertEqual(type(result), float)
124n/a
125n/a result = f(-1, -2, -3, -4, -5.0, -6.0)
126n/a self.assertEqual(result, -21)
127n/a self.assertEqual(type(result), float)
128n/a
129n/a def test_doubleresult(self):
130n/a f = dll._testfunc_d_bhilfd
131n/a f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
132n/a f.restype = c_double
133n/a result = f(1, 2, 3, 4, 5.0, 6.0)
134n/a self.assertEqual(result, 21)
135n/a self.assertEqual(type(result), float)
136n/a
137n/a result = f(-1, -2, -3, -4, -5.0, -6.0)
138n/a self.assertEqual(result, -21)
139n/a self.assertEqual(type(result), float)
140n/a
141n/a def test_longdoubleresult(self):
142n/a f = dll._testfunc_D_bhilfD
143n/a f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_longdouble]
144n/a f.restype = c_longdouble
145n/a result = f(1, 2, 3, 4, 5.0, 6.0)
146n/a self.assertEqual(result, 21)
147n/a self.assertEqual(type(result), float)
148n/a
149n/a result = f(-1, -2, -3, -4, -5.0, -6.0)
150n/a self.assertEqual(result, -21)
151n/a self.assertEqual(type(result), float)
152n/a
153n/a @need_symbol('c_longlong')
154n/a def test_longlongresult(self):
155n/a f = dll._testfunc_q_bhilfd
156n/a f.restype = c_longlong
157n/a f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
158n/a result = f(1, 2, 3, 4, 5.0, 6.0)
159n/a self.assertEqual(result, 21)
160n/a
161n/a f = dll._testfunc_q_bhilfdq
162n/a f.restype = c_longlong
163n/a f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double, c_longlong]
164n/a result = f(1, 2, 3, 4, 5.0, 6.0, 21)
165n/a self.assertEqual(result, 42)
166n/a
167n/a def test_stringresult(self):
168n/a f = dll._testfunc_p_p
169n/a f.argtypes = None
170n/a f.restype = c_char_p
171n/a result = f(b"123")
172n/a self.assertEqual(result, b"123")
173n/a
174n/a result = f(None)
175n/a self.assertEqual(result, None)
176n/a
177n/a def test_pointers(self):
178n/a f = dll._testfunc_p_p
179n/a f.restype = POINTER(c_int)
180n/a f.argtypes = [POINTER(c_int)]
181n/a
182n/a # This only works if the value c_int(42) passed to the
183n/a # function is still alive while the pointer (the result) is
184n/a # used.
185n/a
186n/a v = c_int(42)
187n/a
188n/a self.assertEqual(pointer(v).contents.value, 42)
189n/a result = f(pointer(v))
190n/a self.assertEqual(type(result), POINTER(c_int))
191n/a self.assertEqual(result.contents.value, 42)
192n/a
193n/a # This on works...
194n/a result = f(pointer(v))
195n/a self.assertEqual(result.contents.value, v.value)
196n/a
197n/a p = pointer(c_int(99))
198n/a result = f(p)
199n/a self.assertEqual(result.contents.value, 99)
200n/a
201n/a arg = byref(v)
202n/a result = f(arg)
203n/a self.assertNotEqual(result.contents, v.value)
204n/a
205n/a self.assertRaises(ArgumentError, f, byref(c_short(22)))
206n/a
207n/a # It is dangerous, however, because you don't control the lifetime
208n/a # of the pointer:
209n/a result = f(byref(c_int(99)))
210n/a self.assertNotEqual(result.contents, 99)
211n/a
212n/a def test_errors(self):
213n/a f = dll._testfunc_p_p
214n/a f.restype = c_int
215n/a
216n/a class X(Structure):
217n/a _fields_ = [("y", c_int)]
218n/a
219n/a self.assertRaises(TypeError, f, X()) #cannot convert parameter
220n/a
221n/a ################################################################
222n/a def test_shorts(self):
223n/a f = dll._testfunc_callback_i_if
224n/a
225n/a args = []
226n/a expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048,
227n/a 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
228n/a
229n/a def callback(v):
230n/a args.append(v)
231n/a return v
232n/a
233n/a CallBack = CFUNCTYPE(c_int, c_int)
234n/a
235n/a cb = CallBack(callback)
236n/a f(2**18, cb)
237n/a self.assertEqual(args, expected)
238n/a
239n/a ################################################################
240n/a
241n/a
242n/a def test_callbacks(self):
243n/a f = dll._testfunc_callback_i_if
244n/a f.restype = c_int
245n/a f.argtypes = None
246n/a
247n/a MyCallback = CFUNCTYPE(c_int, c_int)
248n/a
249n/a def callback(value):
250n/a #print "called back with", value
251n/a return value
252n/a
253n/a cb = MyCallback(callback)
254n/a result = f(-10, cb)
255n/a self.assertEqual(result, -18)
256n/a
257n/a # test with prototype
258n/a f.argtypes = [c_int, MyCallback]
259n/a cb = MyCallback(callback)
260n/a result = f(-10, cb)
261n/a self.assertEqual(result, -18)
262n/a
263n/a AnotherCallback = WINFUNCTYPE(c_int, c_int, c_int, c_int, c_int)
264n/a
265n/a # check that the prototype works: we call f with wrong
266n/a # argument types
267n/a cb = AnotherCallback(callback)
268n/a self.assertRaises(ArgumentError, f, -10, cb)
269n/a
270n/a
271n/a def test_callbacks_2(self):
272n/a # Can also use simple datatypes as argument type specifiers
273n/a # for the callback function.
274n/a # In this case the call receives an instance of that type
275n/a f = dll._testfunc_callback_i_if
276n/a f.restype = c_int
277n/a
278n/a MyCallback = CFUNCTYPE(c_int, c_int)
279n/a
280n/a f.argtypes = [c_int, MyCallback]
281n/a
282n/a def callback(value):
283n/a #print "called back with", value
284n/a self.assertEqual(type(value), int)
285n/a return value
286n/a
287n/a cb = MyCallback(callback)
288n/a result = f(-10, cb)
289n/a self.assertEqual(result, -18)
290n/a
291n/a @need_symbol('c_longlong')
292n/a def test_longlong_callbacks(self):
293n/a
294n/a f = dll._testfunc_callback_q_qf
295n/a f.restype = c_longlong
296n/a
297n/a MyCallback = CFUNCTYPE(c_longlong, c_longlong)
298n/a
299n/a f.argtypes = [c_longlong, MyCallback]
300n/a
301n/a def callback(value):
302n/a self.assertIsInstance(value, int)
303n/a return value & 0x7FFFFFFF
304n/a
305n/a cb = MyCallback(callback)
306n/a
307n/a self.assertEqual(13577625587, f(1000000000000, cb))
308n/a
309n/a def test_errors(self):
310n/a self.assertRaises(AttributeError, getattr, dll, "_xxx_yyy")
311n/a self.assertRaises(ValueError, c_int.in_dll, dll, "_xxx_yyy")
312n/a
313n/a def test_byval(self):
314n/a
315n/a # without prototype
316n/a ptin = POINT(1, 2)
317n/a ptout = POINT()
318n/a # EXPORT int _testfunc_byval(point in, point *pout)
319n/a result = dll._testfunc_byval(ptin, byref(ptout))
320n/a got = result, ptout.x, ptout.y
321n/a expected = 3, 1, 2
322n/a self.assertEqual(got, expected)
323n/a
324n/a # with prototype
325n/a ptin = POINT(101, 102)
326n/a ptout = POINT()
327n/a dll._testfunc_byval.argtypes = (POINT, POINTER(POINT))
328n/a dll._testfunc_byval.restype = c_int
329n/a result = dll._testfunc_byval(ptin, byref(ptout))
330n/a got = result, ptout.x, ptout.y
331n/a expected = 203, 101, 102
332n/a self.assertEqual(got, expected)
333n/a
334n/a def test_struct_return_2H(self):
335n/a class S2H(Structure):
336n/a _fields_ = [("x", c_short),
337n/a ("y", c_short)]
338n/a dll.ret_2h_func.restype = S2H
339n/a dll.ret_2h_func.argtypes = [S2H]
340n/a inp = S2H(99, 88)
341n/a s2h = dll.ret_2h_func(inp)
342n/a self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))
343n/a
344n/a @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
345n/a def test_struct_return_2H_stdcall(self):
346n/a class S2H(Structure):
347n/a _fields_ = [("x", c_short),
348n/a ("y", c_short)]
349n/a
350n/a windll.s_ret_2h_func.restype = S2H
351n/a windll.s_ret_2h_func.argtypes = [S2H]
352n/a s2h = windll.s_ret_2h_func(S2H(99, 88))
353n/a self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))
354n/a
355n/a def test_struct_return_8H(self):
356n/a class S8I(Structure):
357n/a _fields_ = [("a", c_int),
358n/a ("b", c_int),
359n/a ("c", c_int),
360n/a ("d", c_int),
361n/a ("e", c_int),
362n/a ("f", c_int),
363n/a ("g", c_int),
364n/a ("h", c_int)]
365n/a dll.ret_8i_func.restype = S8I
366n/a dll.ret_8i_func.argtypes = [S8I]
367n/a inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
368n/a s8i = dll.ret_8i_func(inp)
369n/a self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
370n/a (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
371n/a
372n/a @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
373n/a def test_struct_return_8H_stdcall(self):
374n/a class S8I(Structure):
375n/a _fields_ = [("a", c_int),
376n/a ("b", c_int),
377n/a ("c", c_int),
378n/a ("d", c_int),
379n/a ("e", c_int),
380n/a ("f", c_int),
381n/a ("g", c_int),
382n/a ("h", c_int)]
383n/a windll.s_ret_8i_func.restype = S8I
384n/a windll.s_ret_8i_func.argtypes = [S8I]
385n/a inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
386n/a s8i = windll.s_ret_8i_func(inp)
387n/a self.assertEqual(
388n/a (s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
389n/a (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
390n/a
391n/a def test_sf1651235(self):
392n/a # see http://www.python.org/sf/1651235
393n/a
394n/a proto = CFUNCTYPE(c_int, RECT, POINT)
395n/a def callback(*args):
396n/a return 0
397n/a
398n/a callback = proto(callback)
399n/a self.assertRaises(ArgumentError, lambda: callback((1, 2, 3, 4), POINT()))
400n/a
401n/aif __name__ == '__main__':
402n/a unittest.main()