Debian Codegraph @

extra_tests/ctypes_tests/test_functions.py

raw symbols SCIP
1 from ctypes import *
2 import sys
3 import pytest
4
5 @pytest.fixture
6 def dll(sofile):
7 return CDLL(str(sofile), use_errno=True)
8
9
10 def test_char_result(dll):
11 f = dll._testfunc_i_bhilfd
12 f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
13 f.restype = c_char
14 result = f(0, 0, 0, 0, 0, 0)
15 assert result == b'\x00'
16
17 def test_boolresult(dll):
18 f = dll._testfunc_i_bhilfd
19 f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
20 f.restype = c_bool
21 false_result = f(0, 0, 0, 0, 0, 0)
22 assert false_result is False
23 true_result = f(1, 0, 0, 0, 0, 0)
24 assert true_result is True
25
26 def test_unicode_function_name(dll):
27 f = dll[u'_testfunc_i_bhilfd']
28 f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
29 f.restype = c_int
30 result = f(1, 2, 3, 4, 5.0, 6.0)
31 assert result == 21
32
33 def test_truncate_python_longs(dll):
34 f = dll._testfunc_i_bhilfd
35 f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
36 f.restype = c_int
37 x = sys.maxsize * 2
38 result = f(x, x, x, x, 0, 0)
39 assert result == -8
40
41 def test_convert_pointers(dll):
42 f = dll.deref_LP_c_char_p
43 f.restype = c_char
44 f.argtypes = [POINTER(c_char_p)]
45 #
46 s = c_char_p(b'hello world')
47 ps = pointer(s)
48 assert f(ps) == b'h'
49 assert f(s) == b'h' # automatic conversion from char** to char*
50
51 ################################################################
52
53 def test_call_some_args(dll):
54 f = dll.my_strchr
55 f.argtypes = [c_char_p]
56 f.restype = c_char_p
57 result = f(b"abcd", ord("b"))
58 assert result == b"bcd"
59
60 def test_variadic_sum(dll):
61 f = dll.variadic_sum
62 f.argtypes = [c_long]
63 f.restype = c_long
64 result = f(3, 13, 38, 100)
65 assert result == 13 + 38 + 100
66 result = f(2, 13, 38)
67 assert result == 13 + 38
68
69 @pytest.mark.pypy_only
70 def test_keepalive_buffers(monkeypatch, dll):
71 import gc
72 f = dll.my_strchr
73 f.argtypes = [c_char_p]
74 f.restype = c_char_p
75 #
76 orig__call_funcptr = f._call_funcptr
77 def _call_funcptr(funcptr, *newargs):
78 gc.collect()
79 gc.collect()
80 gc.collect()
81 return orig__call_funcptr(funcptr, *newargs)
82 monkeypatch.setattr(f, '_call_funcptr', _call_funcptr)
83 #
84 result = f(b"abcd", ord("b"))
85 assert result == b"bcd"
86
87 def test_caching_bug_1(dll):
88 # the same test as test_call_some_args, with two extra lines
89 # in the middle that trigger caching in f._ptr, which then
90 # makes the last two lines fail
91 f = dll.my_strchr
92 f.argtypes = [c_char_p, c_int]
93 f.restype = c_char_p
94 result = f(b"abcd", ord("b"))
95 assert result == b"bcd"
96 result = f(b"abcd", ord("b"), 42)
97 assert result == b"bcd"
98
99 def test_argument_conversion_and_checks(dll):
100 #This test is designed to check for segfaults if the wrong type of argument is passed as parameter
101 strlen = dll.my_strchr
102 strlen.argtypes = [c_char_p, c_int]
103 strlen.restype = c_char_p
104 assert strlen(b"eggs", ord("g")) == b"ggs"
105
106 # Should raise ArgumentError, not segfault
107 with pytest.raises(ArgumentError):
108 strlen(0, 0)
109 with pytest.raises(ArgumentError):
110 strlen(False, 0)
111
112 def test_union_as_passed_value(dll):
113 class UN(Union):
114 _fields_ = [("x", c_short),
115 ("y", c_long)]
116 dll.ret_un_func.restype = UN
117 dll.ret_un_func.argtypes = [UN]
118 A = UN * 2
119 a = A()
120 a[1].x = 33
121 u = dll.ret_un_func(a[1])
122 assert u.y == 33 * 10000
123
124 @pytest.mark.pypy_only
125 def test_cache_funcptr(dll):
126 tf_b = dll.tf_b
127 tf_b.restype = c_byte
128 tf_b.argtypes = (c_byte,)
129 assert tf_b(-126) == -42
130 ptr = tf_b._ptr
131 assert ptr is not None
132 assert tf_b(-126) == -42
133 assert tf_b._ptr is ptr
134
135 def test_custom_from_param(dll):
136 class A(c_byte):
137 @classmethod
138 def from_param(cls, obj):
139 seen.append(obj)
140 return -126
141 tf_b = dll.tf_b
142 tf_b.restype = c_byte
143 tf_b.argtypes = (c_byte,)
144 tf_b.argtypes = [A]
145 seen = []
146 assert tf_b("yadda") == -42
147 assert seen == ["yadda"]
148
149 @pytest.mark.xfail(reason="warnings are disabled")
150 def test_warnings(dll):
151 import warnings
152 warnings.simplefilter("always")
153 with warnings.catch_warnings(record=True) as w:
154 dll.get_an_integer()
155 assert len(w) == 1
156 assert issubclass(w[0].category, RuntimeWarning)
157 assert "C function without declared arguments called" in str(w[0].message)
158
159 @pytest.mark.xfail
160 def test_errcheck(dll):
161 import warnings
162 def errcheck(result, func, args):
163 assert result == -42
164 assert type(result) is int
165 arg, = args
166 assert arg == -126
167 assert type(arg) is int
168 return result
169 #
170 tf_b = dll.tf_b
171 tf_b.restype = c_byte
172 tf_b.argtypes = (c_byte,)
173 tf_b.errcheck = errcheck
174 assert tf_b(-126) == -42
175 del tf_b.errcheck
176 with warnings.catch_warnings(record=True) as w:
177 dll.get_an_integer.argtypes = []
178 dll.get_an_integer()
179 assert len(w) == 1
180 assert issubclass(w[0].category, RuntimeWarning)
181 assert "C function without declared return type called" in str(w[0].message)
182
183 with warnings.catch_warnings(record=True) as w:
184 dll.get_an_integer.restype = None
185 dll.get_an_integer()
186 assert len(w) == 0
187
188 warnings.resetwarnings()
189
190 def test_errno(dll):
191 test_errno = dll.test_errno
192 test_errno.restype = c_int
193 set_errno(42)
194 res = test_errno()
195 n = get_errno()
196 assert (res, n) == (42, 43)
197 set_errno(0)
198 assert get_errno() == 0
199
200 def test_issue1655(dll):
201 def ret_list_p(icount):
202 def sz_array_p(obj, func, args):
203 assert ('.LP_c_int object' in repr(obj) or
204 '.LP_c_long object' in repr(obj))
205 assert repr(args) =="(b'testing!', c_int(4))"
206 assert args[icount].value == 4
207 return [obj[i] for i in range(args[icount].value)]
208 return sz_array_p
209
210 get_data_prototype = CFUNCTYPE(POINTER(c_int),
211 c_char_p, POINTER(c_int))
212 get_data_paramflag = ((1,), (2,))
213 get_data_signature = ('test_issue1655', dll)
214
215 get_data = get_data_prototype(get_data_signature, get_data_paramflag)
216 assert get_data(b'testing!') == 4
217
218 get_data.errcheck = ret_list_p(1)
219 assert get_data(b'testing!') == [-1, -2, -3, -4]
220
221 def test_issue2533(tmpdir):
222 import cffi
223 ffi = cffi.FFI()
224 ffi.cdef("int **fetchme(void);")
225 ffi.set_source("_x_cffi", """
226 int **fetchme(void)
227 {
228 static int a = 42;
229 static int *pa = &a;
230 return &pa;
231 }
232 """)
233 ffi.compile(verbose=True, tmpdir=str(tmpdir))
234
235 import sys
236 sys.path.insert(0, str(tmpdir))
237 try:
238 from _x_cffi import ffi, lib
239 finally:
240 sys.path.pop(0)
241 fetchme = ffi.addressof(lib, 'fetchme')
242 fetchme = int(ffi.cast("intptr_t", fetchme))
243
244 FN = CFUNCTYPE(POINTER(POINTER(c_int)))
245 ff = cast(fetchme, FN)
246
247 g = ff()
248 assert g.contents.contents.value == 42
249
250 h = c_int(43)
251 g[0] = pointer(h) # used to crash here
252 assert g.contents.contents.value == 43