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

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

#countcontent
1n/aimport unittest, sys
2n/a
3n/afrom ctypes import *
4n/aimport _ctypes_test
5n/a
6n/actype_types = [c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint,
7n/a c_long, c_ulong, c_longlong, c_ulonglong, c_double, c_float]
8n/apython_types = [int, int, int, int, int, int,
9n/a int, int, int, int, float, float]
10n/a
11n/aclass PointersTestCase(unittest.TestCase):
12n/a
13n/a def test_pointer_crash(self):
14n/a
15n/a class A(POINTER(c_ulong)):
16n/a pass
17n/a
18n/a POINTER(c_ulong)(c_ulong(22))
19n/a # Pointer can't set contents: has no _type_
20n/a self.assertRaises(TypeError, A, c_ulong(33))
21n/a
22n/a def test_pass_pointers(self):
23n/a dll = CDLL(_ctypes_test.__file__)
24n/a func = dll._testfunc_p_p
25n/a if sizeof(c_longlong) == sizeof(c_void_p):
26n/a func.restype = c_longlong
27n/a else:
28n/a func.restype = c_long
29n/a
30n/a i = c_int(12345678)
31n/a## func.argtypes = (POINTER(c_int),)
32n/a address = func(byref(i))
33n/a self.assertEqual(c_int.from_address(address).value, 12345678)
34n/a
35n/a func.restype = POINTER(c_int)
36n/a res = func(pointer(i))
37n/a self.assertEqual(res.contents.value, 12345678)
38n/a self.assertEqual(res[0], 12345678)
39n/a
40n/a def test_change_pointers(self):
41n/a dll = CDLL(_ctypes_test.__file__)
42n/a func = dll._testfunc_p_p
43n/a
44n/a i = c_int(87654)
45n/a func.restype = POINTER(c_int)
46n/a func.argtypes = (POINTER(c_int),)
47n/a
48n/a res = func(pointer(i))
49n/a self.assertEqual(res[0], 87654)
50n/a self.assertEqual(res.contents.value, 87654)
51n/a
52n/a # C code: *res = 54345
53n/a res[0] = 54345
54n/a self.assertEqual(i.value, 54345)
55n/a
56n/a # C code:
57n/a # int x = 12321;
58n/a # res = &x
59n/a x = c_int(12321)
60n/a res.contents = x
61n/a self.assertEqual(i.value, 54345)
62n/a
63n/a x.value = -99
64n/a self.assertEqual(res.contents.value, -99)
65n/a
66n/a def test_callbacks_with_pointers(self):
67n/a # a function type receiving a pointer
68n/a PROTOTYPE = CFUNCTYPE(c_int, POINTER(c_int))
69n/a
70n/a self.result = []
71n/a
72n/a def func(arg):
73n/a for i in range(10):
74n/a## print arg[i],
75n/a self.result.append(arg[i])
76n/a## print
77n/a return 0
78n/a callback = PROTOTYPE(func)
79n/a
80n/a dll = CDLL(_ctypes_test.__file__)
81n/a # This function expects a function pointer,
82n/a # and calls this with an integer pointer as parameter.
83n/a # The int pointer points to a table containing the numbers 1..10
84n/a doit = dll._testfunc_callback_with_pointer
85n/a
86n/a## i = c_int(42)
87n/a## callback(byref(i))
88n/a## self.assertEqual(i.value, 84)
89n/a
90n/a doit(callback)
91n/a## print self.result
92n/a doit(callback)
93n/a## print self.result
94n/a
95n/a def test_basics(self):
96n/a from operator import delitem
97n/a for ct, pt in zip(ctype_types, python_types):
98n/a i = ct(42)
99n/a p = pointer(i)
100n/a## print type(p.contents), ct
101n/a self.assertIs(type(p.contents), ct)
102n/a # p.contents is the same as p[0]
103n/a## print p.contents
104n/a## self.assertEqual(p.contents, 42)
105n/a## self.assertEqual(p[0], 42)
106n/a
107n/a self.assertRaises(TypeError, delitem, p, 0)
108n/a
109n/a def test_from_address(self):
110n/a from array import array
111n/a a = array('i', [100, 200, 300, 400, 500])
112n/a addr = a.buffer_info()[0]
113n/a
114n/a p = POINTER(POINTER(c_int))
115n/a## print dir(p)
116n/a## print p.from_address
117n/a## print p.from_address(addr)[0][0]
118n/a
119n/a def test_other(self):
120n/a class Table(Structure):
121n/a _fields_ = [("a", c_int),
122n/a ("b", c_int),
123n/a ("c", c_int)]
124n/a
125n/a pt = pointer(Table(1, 2, 3))
126n/a
127n/a self.assertEqual(pt.contents.a, 1)
128n/a self.assertEqual(pt.contents.b, 2)
129n/a self.assertEqual(pt.contents.c, 3)
130n/a
131n/a pt.contents.c = 33
132n/a
133n/a from ctypes import _pointer_type_cache
134n/a del _pointer_type_cache[Table]
135n/a
136n/a def test_basic(self):
137n/a p = pointer(c_int(42))
138n/a # Although a pointer can be indexed, it has no length
139n/a self.assertRaises(TypeError, len, p)
140n/a self.assertEqual(p[0], 42)
141n/a self.assertEqual(p[0:1], [42])
142n/a self.assertEqual(p.contents.value, 42)
143n/a
144n/a def test_charpp(self):
145n/a """Test that a character pointer-to-pointer is correctly passed"""
146n/a dll = CDLL(_ctypes_test.__file__)
147n/a func = dll._testfunc_c_p_p
148n/a func.restype = c_char_p
149n/a argv = (c_char_p * 2)()
150n/a argc = c_int( 2 )
151n/a argv[0] = b'hello'
152n/a argv[1] = b'world'
153n/a result = func( byref(argc), argv )
154n/a self.assertEqual(result, b'world')
155n/a
156n/a def test_bug_1467852(self):
157n/a # http://sourceforge.net/tracker/?func=detail&atid=532154&aid=1467852&group_id=71702
158n/a x = c_int(5)
159n/a dummy = []
160n/a for i in range(32000):
161n/a dummy.append(c_int(i))
162n/a y = c_int(6)
163n/a p = pointer(x)
164n/a pp = pointer(p)
165n/a q = pointer(y)
166n/a pp[0] = q # <==
167n/a self.assertEqual(p[0], 6)
168n/a def test_c_void_p(self):
169n/a # http://sourceforge.net/tracker/?func=detail&aid=1518190&group_id=5470&atid=105470
170n/a if sizeof(c_void_p) == 4:
171n/a self.assertEqual(c_void_p(0xFFFFFFFF).value,
172n/a c_void_p(-1).value)
173n/a self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFF).value,
174n/a c_void_p(-1).value)
175n/a elif sizeof(c_void_p) == 8:
176n/a self.assertEqual(c_void_p(0xFFFFFFFF).value,
177n/a 0xFFFFFFFF)
178n/a self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFF).value,
179n/a c_void_p(-1).value)
180n/a self.assertEqual(c_void_p(0xFFFFFFFFFFFFFFFFFFFFFFFF).value,
181n/a c_void_p(-1).value)
182n/a
183n/a self.assertRaises(TypeError, c_void_p, 3.14) # make sure floats are NOT accepted
184n/a self.assertRaises(TypeError, c_void_p, object()) # nor other objects
185n/a
186n/a def test_pointers_bool(self):
187n/a # NULL pointers have a boolean False value, non-NULL pointers True.
188n/a self.assertEqual(bool(POINTER(c_int)()), False)
189n/a self.assertEqual(bool(pointer(c_int())), True)
190n/a
191n/a self.assertEqual(bool(CFUNCTYPE(None)(0)), False)
192n/a self.assertEqual(bool(CFUNCTYPE(None)(42)), True)
193n/a
194n/a # COM methods are boolean True:
195n/a if sys.platform == "win32":
196n/a mth = WINFUNCTYPE(None)(42, "name", (), None)
197n/a self.assertEqual(bool(mth), True)
198n/a
199n/a def test_pointer_type_name(self):
200n/a LargeNamedType = type('T' * 2 ** 25, (Structure,), {})
201n/a self.assertTrue(POINTER(LargeNamedType))
202n/a
203n/a # to not leak references, we must clean _pointer_type_cache
204n/a from ctypes import _pointer_type_cache
205n/a del _pointer_type_cache[LargeNamedType]
206n/a
207n/a def test_pointer_type_str_name(self):
208n/a large_string = 'T' * 2 ** 25
209n/a P = POINTER(large_string)
210n/a self.assertTrue(P)
211n/a
212n/a # to not leak references, we must clean _pointer_type_cache
213n/a from ctypes import _pointer_type_cache
214n/a del _pointer_type_cache[id(P)]
215n/a
216n/a
217n/aif __name__ == '__main__':
218n/a unittest.main()