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

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

#countcontent
1n/aimport unittest
2n/afrom ctypes.test import need_symbol
3n/a
4n/aclass SimpleTypesTestCase(unittest.TestCase):
5n/a
6n/a def setUp(self):
7n/a import ctypes
8n/a try:
9n/a from _ctypes import set_conversion_mode
10n/a except ImportError:
11n/a pass
12n/a else:
13n/a self.prev_conv_mode = set_conversion_mode("ascii", "strict")
14n/a
15n/a def tearDown(self):
16n/a try:
17n/a from _ctypes import set_conversion_mode
18n/a except ImportError:
19n/a pass
20n/a else:
21n/a set_conversion_mode(*self.prev_conv_mode)
22n/a
23n/a def test_subclasses(self):
24n/a from ctypes import c_void_p, c_char_p
25n/a # ctypes 0.9.5 and before did overwrite from_param in SimpleType_new
26n/a class CVOIDP(c_void_p):
27n/a def from_param(cls, value):
28n/a return value * 2
29n/a from_param = classmethod(from_param)
30n/a
31n/a class CCHARP(c_char_p):
32n/a def from_param(cls, value):
33n/a return value * 4
34n/a from_param = classmethod(from_param)
35n/a
36n/a self.assertEqual(CVOIDP.from_param("abc"), "abcabc")
37n/a self.assertEqual(CCHARP.from_param("abc"), "abcabcabcabc")
38n/a
39n/a @need_symbol('c_wchar_p')
40n/a def test_subclasses_c_wchar_p(self):
41n/a from ctypes import c_wchar_p
42n/a
43n/a class CWCHARP(c_wchar_p):
44n/a def from_param(cls, value):
45n/a return value * 3
46n/a from_param = classmethod(from_param)
47n/a
48n/a self.assertEqual(CWCHARP.from_param("abc"), "abcabcabc")
49n/a
50n/a # XXX Replace by c_char_p tests
51n/a def test_cstrings(self):
52n/a from ctypes import c_char_p
53n/a
54n/a # c_char_p.from_param on a Python String packs the string
55n/a # into a cparam object
56n/a s = b"123"
57n/a self.assertIs(c_char_p.from_param(s)._obj, s)
58n/a
59n/a # new in 0.9.1: convert (encode) unicode to ascii
60n/a self.assertEqual(c_char_p.from_param(b"123")._obj, b"123")
61n/a self.assertRaises(TypeError, c_char_p.from_param, "123\377")
62n/a self.assertRaises(TypeError, c_char_p.from_param, 42)
63n/a
64n/a # calling c_char_p.from_param with a c_char_p instance
65n/a # returns the argument itself:
66n/a a = c_char_p(b"123")
67n/a self.assertIs(c_char_p.from_param(a), a)
68n/a
69n/a @need_symbol('c_wchar_p')
70n/a def test_cw_strings(self):
71n/a from ctypes import c_wchar_p
72n/a
73n/a c_wchar_p.from_param("123")
74n/a
75n/a self.assertRaises(TypeError, c_wchar_p.from_param, 42)
76n/a self.assertRaises(TypeError, c_wchar_p.from_param, b"123\377")
77n/a
78n/a pa = c_wchar_p.from_param(c_wchar_p("123"))
79n/a self.assertEqual(type(pa), c_wchar_p)
80n/a
81n/a def test_int_pointers(self):
82n/a from ctypes import c_short, c_uint, c_int, c_long, POINTER, pointer
83n/a LPINT = POINTER(c_int)
84n/a
85n/a## p = pointer(c_int(42))
86n/a## x = LPINT.from_param(p)
87n/a x = LPINT.from_param(pointer(c_int(42)))
88n/a self.assertEqual(x.contents.value, 42)
89n/a self.assertEqual(LPINT(c_int(42)).contents.value, 42)
90n/a
91n/a self.assertEqual(LPINT.from_param(None), None)
92n/a
93n/a if c_int != c_long:
94n/a self.assertRaises(TypeError, LPINT.from_param, pointer(c_long(42)))
95n/a self.assertRaises(TypeError, LPINT.from_param, pointer(c_uint(42)))
96n/a self.assertRaises(TypeError, LPINT.from_param, pointer(c_short(42)))
97n/a
98n/a def test_byref_pointer(self):
99n/a # The from_param class method of POINTER(typ) classes accepts what is
100n/a # returned by byref(obj), it type(obj) == typ
101n/a from ctypes import c_short, c_uint, c_int, c_long, POINTER, byref
102n/a LPINT = POINTER(c_int)
103n/a
104n/a LPINT.from_param(byref(c_int(42)))
105n/a
106n/a self.assertRaises(TypeError, LPINT.from_param, byref(c_short(22)))
107n/a if c_int != c_long:
108n/a self.assertRaises(TypeError, LPINT.from_param, byref(c_long(22)))
109n/a self.assertRaises(TypeError, LPINT.from_param, byref(c_uint(22)))
110n/a
111n/a def test_byref_pointerpointer(self):
112n/a # See above
113n/a from ctypes import c_short, c_uint, c_int, c_long, pointer, POINTER, byref
114n/a
115n/a LPLPINT = POINTER(POINTER(c_int))
116n/a LPLPINT.from_param(byref(pointer(c_int(42))))
117n/a
118n/a self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_short(22))))
119n/a if c_int != c_long:
120n/a self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_long(22))))
121n/a self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_uint(22))))
122n/a
123n/a def test_array_pointers(self):
124n/a from ctypes import c_short, c_uint, c_int, c_long, POINTER
125n/a INTARRAY = c_int * 3
126n/a ia = INTARRAY()
127n/a self.assertEqual(len(ia), 3)
128n/a self.assertEqual([ia[i] for i in range(3)], [0, 0, 0])
129n/a
130n/a # Pointers are only compatible with arrays containing items of
131n/a # the same type!
132n/a LPINT = POINTER(c_int)
133n/a LPINT.from_param((c_int*3)())
134n/a self.assertRaises(TypeError, LPINT.from_param, c_short*3)
135n/a self.assertRaises(TypeError, LPINT.from_param, c_long*3)
136n/a self.assertRaises(TypeError, LPINT.from_param, c_uint*3)
137n/a
138n/a def test_noctypes_argtype(self):
139n/a import _ctypes_test
140n/a from ctypes import CDLL, c_void_p, ArgumentError
141n/a
142n/a func = CDLL(_ctypes_test.__file__)._testfunc_p_p
143n/a func.restype = c_void_p
144n/a # TypeError: has no from_param method
145n/a self.assertRaises(TypeError, setattr, func, "argtypes", (object,))
146n/a
147n/a class Adapter(object):
148n/a def from_param(cls, obj):
149n/a return None
150n/a
151n/a func.argtypes = (Adapter(),)
152n/a self.assertEqual(func(None), None)
153n/a self.assertEqual(func(object()), None)
154n/a
155n/a class Adapter(object):
156n/a def from_param(cls, obj):
157n/a return obj
158n/a
159n/a func.argtypes = (Adapter(),)
160n/a # don't know how to convert parameter 1
161n/a self.assertRaises(ArgumentError, func, object())
162n/a self.assertEqual(func(c_void_p(42)), 42)
163n/a
164n/a class Adapter(object):
165n/a def from_param(cls, obj):
166n/a raise ValueError(obj)
167n/a
168n/a func.argtypes = (Adapter(),)
169n/a # ArgumentError: argument 1: ValueError: 99
170n/a self.assertRaises(ArgumentError, func, 99)
171n/a
172n/a
173n/a################################################################
174n/a
175n/aif __name__ == '__main__':
176n/a unittest.main()