Debian Codegraph @

extra_tests/ctypes_tests/test_extra.py

raw symbols SCIP
1 """
2 The purpose of this test file is to check how ctypes really work,
3 down to what aliases what and what exact types operations return.
4 """
5
6 import pytest
7 from ctypes import *
8
9 def test_primitive_pointer():
10 x = c_int(5)
11 assert x.value == 5
12 x.value = 6
13 assert x.value == 6
14
15 p = pointer(x) # p ---> x = 6
16 assert isinstance(p.contents, c_int)
17 p.contents.value += 1
18 assert x.value == 7 # p ---> x = 7
19
20 y = c_int(12)
21 p.contents = y # p ---> y = 12
22 p.contents.value += 2 # p ---> y = 14
23 assert y.value == 14
24 assert x.value == 7
25
26 pp = pointer(p) # pp ---> p ---> y = 14
27 pp.contents.contents = x # pp ---> p ---> x = 7
28 p.contents.value += 2 # pp ---> p ---> x = 9
29 assert x.value == 9
30
31 assert isinstance(p[0], int)
32 p[0] += 1 # pp ---> p ---> x = 10
33 assert x.value == 10
34 z = c_int(86)
35 p[0] = z # pp ---> p ---> x = 86 (not z!)
36 assert x.value == 86
37 z.value = 84
38 assert x.value == 86
39
40 assert isinstance(pp[0], POINTER(c_int))
41 assert pp[0].contents.value == x.value == 86
42 pp[0].contents = z # pp ---> p ---> z = 84
43 assert p.contents.value == z.value == 84
44
45 ## *** the rest is commented out because it should work but occasionally
46 ## *** trigger a ctypes bug (SourceForge bug #1467852). ***
47 ## q = pointer(y)
48 ## pp[0] = q # pp ---> p ---> y = 14
49 ## assert y.value == 14 # (^^^ not q! )
50 ## assert p.contents.value == 14
51 ## assert pp.contents.contents.value == 14
52 ## q.contents = x
53 ## assert pp.contents.contents.value == 14
54
55
56 def test_char_p():
57 x = c_char_p(b"hello\x00world")
58 assert x.value == b"hello"
59 x.value = b"world"
60 assert x.value == b"world"
61
62 p = pointer(x)
63 assert p[0] == x.value == b"world"
64 p[0] = b"other"
65 assert x.value == p.contents.value == p[0] == b"other"
66
67 myarray = (c_char_p * 10)()
68 myarray[7] = b"hello"
69 assert isinstance(myarray[7], bytes)
70 assert myarray[7] == b"hello"
71
72 def test_struct():
73 class tagpoint(Structure):
74 _fields_ = [('x', c_int),
75 ('p', POINTER(c_short))]
76
77 y = c_short(123)
78 z = c_short(-33)
79 s = tagpoint()
80 s.p.contents = z
81 assert s.p.contents.value == -33
82 s.p = pointer(y)
83 assert s.p.contents.value == 123
84 s.p.contents.value = 124
85 assert y.value == 124
86
87 s = tagpoint(x=12)
88 assert s.x == 12
89 s = tagpoint(17, p=pointer(z))
90 assert s.x == 17
91 assert s.p.contents.value == -33
92
93 def test_ptr_array():
94 a = (POINTER(c_ushort) * 5)()
95 x = c_ushort(52)
96 y = c_ushort(1000)
97
98 a[2] = pointer(x)
99 assert a[2].contents.value == 52
100 a[2].contents.value += 1
101 assert x.value == 53
102
103 a[3].contents = y
104 assert a[3].contents.value == 1000
105 a[3].contents.value += 1
106 assert y.value == 1001
107
108 def test_void_p():
109 x = c_int(12)
110 p1 = cast(pointer(x), c_void_p)
111 p2 = cast(p1, POINTER(c_int))
112 assert p2.contents.value == 12
113
114 def test_char_array():
115 a = (c_char * 3)()
116 a[0] = b'x'
117 a[1] = b'y'
118 assert a.value == b'xy'
119 a[2] = b'z'
120 assert a.value == b'xyz'
121
122 b = create_string_buffer(3)
123 assert type(b) is type(a)
124 assert len(b) == 3
125
126 b.value = b"nxw"
127 assert b[0] == b'n'
128 assert b[1] == b'x'
129 assert b[2] == b'w'
130
131 b.value = b"?"
132 assert b[0] == b'?'
133 assert b[1] == b'\x00'
134 assert b[2] == b'w'
135
136 class S(Structure):
137 _fields_ = [('p', POINTER(c_char))]
138
139 s = S()
140 s.p = b
141 s.p.contents.value = b'!'
142 assert b.value == b'!'
143
144 assert len(create_string_buffer(0)) == 0
145
146 def test_array():
147 a = (c_int * 10)()
148
149 class S(Structure):
150 _fields_ = [('p', POINTER(c_int))]
151
152 s = S()
153 s.p = a
154 s.p.contents.value = 42
155 assert a[0] == 42
156
157 a = (c_int * 5)(5, 6, 7)
158 assert list(a) == [5, 6, 7, 0, 0]
159
160 def test_truth_value():
161 p = POINTER(c_int)()
162 assert not p
163 p.contents = c_int(12)
164 assert p
165 # I can't figure out how to reset p to NULL...
166
167 assert c_int(12)
168 assert not c_int(0) # a bit strange, if you ask me
169 assert c_int(-1)
170 assert not c_byte(0)
171 assert not c_char(b'\x00') # hum
172 assert not c_float(0.0)
173 assert not c_double(0.0)
174 assert not c_ulonglong(0)
175 assert c_ulonglong(2**42)
176
177 assert c_char_p(b"hello")
178 assert c_char_p(b"")
179 assert not c_char_p(None)
180
181 assert not c_void_p()
182
183 def test_sizeof():
184 x = create_string_buffer(117)
185 assert sizeof(x) == 117 # assumes that chars are one byte each
186 x = (c_int * 42)()
187 assert sizeof(x) == 42 * sizeof(c_int)
188
189 def test_convert_pointers(dll):
190 func = dll._testfunc_p_p
191 func.restype = c_char_p
192
193 # automatic conversions to c_char_p
194 func.argtypes = [c_char_p]
195 assert func(b"hello") == b"hello"
196 assert func(c_char_p(b"hello")) == b"hello"
197 assert func((c_char * 6)(*b"hello")) == b"hello"
198 assert func(create_string_buffer(b"hello")) == b"hello"
199
200 # automatic conversions to c_void_p
201 func.argtypes = [c_void_p]
202 assert func(b"hello") == b"hello"
203 assert func(c_char_p(b"hello")) == b"hello"
204 assert func((c_char * 6)(*b"hello")) == b"hello"
205 assert func((c_byte * 6)(104,101,108,108,111)) ==b"hello"
206 assert func(create_string_buffer(b"hello")) == b"hello"
207
208 def test_varsize_cast():
209 import struct
210 N = struct.calcsize("l")
211 x = c_long()
212 p = cast(pointer(x), POINTER(c_ubyte*N))
213 for i, c in enumerate(struct.pack("l", 12345678)):
214 p.contents[i] = c
215 assert x.value == 12345678
216
217 def test_cfunctype_inspection():
218 T = CFUNCTYPE(c_int, c_ubyte)
219 # T.argtypes and T.restype don't work, must use a dummy instance
220 assert list(T().argtypes) == [c_ubyte]
221 assert T().restype == c_int
222
223 def test_from_param():
224 # other working cases of from_param
225 assert isinstance(c_void_p.from_param((c_int * 4)()), c_int * 4)
226
227 def test_array_mul():
228 assert c_int * 10 == 10 * c_int
229 with pytest.raises(TypeError):
230 c_int * c_int
231 with pytest.raises(TypeError):
232 c_int * (-1.5)
233 with pytest.raises(TypeError):
234 c_int * "foo"
235 with pytest.raises(TypeError):
236 (-1.5) * c_int
237 with pytest.raises(TypeError):
238 "foo" * c_int
239
240 def test_cast_none():
241 def check(P):
242 x = cast(None, P)
243 assert isinstance(x, P)
244 assert not x
245 check(c_void_p)
246 check(c_char_p)
247 check(POINTER(c_int))
248 check(POINTER(c_int * 10))