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

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

#countcontent
1n/aimport unittest
2n/afrom ctypes import *
3n/afrom ctypes.test import need_symbol
4n/aimport _ctypes_test
5n/a
6n/adll = CDLL(_ctypes_test.__file__)
7n/a
8n/atry:
9n/a CALLBACK_FUNCTYPE = WINFUNCTYPE
10n/aexcept NameError:
11n/a # fake to enable this test on Linux
12n/a CALLBACK_FUNCTYPE = CFUNCTYPE
13n/a
14n/aclass POINT(Structure):
15n/a _fields_ = [("x", c_int), ("y", c_int)]
16n/a
17n/aclass BasicWrapTestCase(unittest.TestCase):
18n/a def wrap(self, param):
19n/a return param
20n/a
21n/a @need_symbol('c_wchar')
22n/a def test_wchar_parm(self):
23n/a f = dll._testfunc_i_bhilfd
24n/a f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
25n/a result = f(self.wrap(1), self.wrap("x"), self.wrap(3), self.wrap(4), self.wrap(5.0), self.wrap(6.0))
26n/a self.assertEqual(result, 139)
27n/a self.assertTrue(type(result), int)
28n/a
29n/a def test_pointers(self):
30n/a f = dll._testfunc_p_p
31n/a f.restype = POINTER(c_int)
32n/a f.argtypes = [POINTER(c_int)]
33n/a
34n/a # This only works if the value c_int(42) passed to the
35n/a # function is still alive while the pointer (the result) is
36n/a # used.
37n/a
38n/a v = c_int(42)
39n/a
40n/a self.assertEqual(pointer(v).contents.value, 42)
41n/a result = f(self.wrap(pointer(v)))
42n/a self.assertEqual(type(result), POINTER(c_int))
43n/a self.assertEqual(result.contents.value, 42)
44n/a
45n/a # This on works...
46n/a result = f(self.wrap(pointer(v)))
47n/a self.assertEqual(result.contents.value, v.value)
48n/a
49n/a p = pointer(c_int(99))
50n/a result = f(self.wrap(p))
51n/a self.assertEqual(result.contents.value, 99)
52n/a
53n/a def test_shorts(self):
54n/a f = dll._testfunc_callback_i_if
55n/a
56n/a args = []
57n/a expected = [262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048,
58n/a 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
59n/a
60n/a def callback(v):
61n/a args.append(v)
62n/a return v
63n/a
64n/a CallBack = CFUNCTYPE(c_int, c_int)
65n/a
66n/a cb = CallBack(callback)
67n/a f(self.wrap(2**18), self.wrap(cb))
68n/a self.assertEqual(args, expected)
69n/a
70n/a ################################################################
71n/a
72n/a def test_callbacks(self):
73n/a f = dll._testfunc_callback_i_if
74n/a f.restype = c_int
75n/a f.argtypes = None
76n/a
77n/a MyCallback = CFUNCTYPE(c_int, c_int)
78n/a
79n/a def callback(value):
80n/a #print "called back with", value
81n/a return value
82n/a
83n/a cb = MyCallback(callback)
84n/a
85n/a result = f(self.wrap(-10), self.wrap(cb))
86n/a self.assertEqual(result, -18)
87n/a
88n/a # test with prototype
89n/a f.argtypes = [c_int, MyCallback]
90n/a cb = MyCallback(callback)
91n/a
92n/a result = f(self.wrap(-10), self.wrap(cb))
93n/a self.assertEqual(result, -18)
94n/a
95n/a result = f(self.wrap(-10), self.wrap(cb))
96n/a self.assertEqual(result, -18)
97n/a
98n/a AnotherCallback = CALLBACK_FUNCTYPE(c_int, c_int, c_int, c_int, c_int)
99n/a
100n/a # check that the prototype works: we call f with wrong
101n/a # argument types
102n/a cb = AnotherCallback(callback)
103n/a self.assertRaises(ArgumentError, f, self.wrap(-10), self.wrap(cb))
104n/a
105n/a def test_callbacks_2(self):
106n/a # Can also use simple datatypes as argument type specifiers
107n/a # for the callback function.
108n/a # In this case the call receives an instance of that type
109n/a f = dll._testfunc_callback_i_if
110n/a f.restype = c_int
111n/a
112n/a MyCallback = CFUNCTYPE(c_int, c_int)
113n/a
114n/a f.argtypes = [c_int, MyCallback]
115n/a
116n/a def callback(value):
117n/a #print "called back with", value
118n/a self.assertEqual(type(value), int)
119n/a return value
120n/a
121n/a cb = MyCallback(callback)
122n/a result = f(self.wrap(-10), self.wrap(cb))
123n/a self.assertEqual(result, -18)
124n/a
125n/a def test_longlong_callbacks(self):
126n/a
127n/a f = dll._testfunc_callback_q_qf
128n/a f.restype = c_longlong
129n/a
130n/a MyCallback = CFUNCTYPE(c_longlong, c_longlong)
131n/a
132n/a f.argtypes = [c_longlong, MyCallback]
133n/a
134n/a def callback(value):
135n/a self.assertIsInstance(value, int)
136n/a return value & 0x7FFFFFFF
137n/a
138n/a cb = MyCallback(callback)
139n/a
140n/a self.assertEqual(13577625587, int(f(self.wrap(1000000000000), self.wrap(cb))))
141n/a
142n/a def test_byval(self):
143n/a # without prototype
144n/a ptin = POINT(1, 2)
145n/a ptout = POINT()
146n/a # EXPORT int _testfunc_byval(point in, point *pout)
147n/a result = dll._testfunc_byval(ptin, byref(ptout))
148n/a got = result, ptout.x, ptout.y
149n/a expected = 3, 1, 2
150n/a self.assertEqual(got, expected)
151n/a
152n/a # with prototype
153n/a ptin = POINT(101, 102)
154n/a ptout = POINT()
155n/a dll._testfunc_byval.argtypes = (POINT, POINTER(POINT))
156n/a dll._testfunc_byval.restype = c_int
157n/a result = dll._testfunc_byval(self.wrap(ptin), byref(ptout))
158n/a got = result, ptout.x, ptout.y
159n/a expected = 203, 101, 102
160n/a self.assertEqual(got, expected)
161n/a
162n/a def test_struct_return_2H(self):
163n/a class S2H(Structure):
164n/a _fields_ = [("x", c_short),
165n/a ("y", c_short)]
166n/a dll.ret_2h_func.restype = S2H
167n/a dll.ret_2h_func.argtypes = [S2H]
168n/a inp = S2H(99, 88)
169n/a s2h = dll.ret_2h_func(self.wrap(inp))
170n/a self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))
171n/a
172n/a def test_struct_return_8H(self):
173n/a class S8I(Structure):
174n/a _fields_ = [("a", c_int),
175n/a ("b", c_int),
176n/a ("c", c_int),
177n/a ("d", c_int),
178n/a ("e", c_int),
179n/a ("f", c_int),
180n/a ("g", c_int),
181n/a ("h", c_int)]
182n/a dll.ret_8i_func.restype = S8I
183n/a dll.ret_8i_func.argtypes = [S8I]
184n/a inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
185n/a s8i = dll.ret_8i_func(self.wrap(inp))
186n/a self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
187n/a (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
188n/a
189n/a def test_recursive_as_param(self):
190n/a from ctypes import c_int
191n/a
192n/a class A(object):
193n/a pass
194n/a
195n/a a = A()
196n/a a._as_parameter_ = a
197n/a with self.assertRaises(RecursionError):
198n/a c_int.from_param(a)
199n/a
200n/a
201n/a#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
202n/a
203n/aclass AsParamWrapper(object):
204n/a def __init__(self, param):
205n/a self._as_parameter_ = param
206n/a
207n/aclass AsParamWrapperTestCase(BasicWrapTestCase):
208n/a wrap = AsParamWrapper
209n/a
210n/a#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
211n/a
212n/aclass AsParamPropertyWrapper(object):
213n/a def __init__(self, param):
214n/a self._param = param
215n/a
216n/a def getParameter(self):
217n/a return self._param
218n/a _as_parameter_ = property(getParameter)
219n/a
220n/aclass AsParamPropertyWrapperTestCase(BasicWrapTestCase):
221n/a wrap = AsParamPropertyWrapper
222n/a
223n/a#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
224n/a
225n/aif __name__ == '__main__':
226n/a unittest.main()