Debian Codegraph @

extra_tests/cffi_tests/cffi0/test_ownlib.py

raw symbols SCIP
1 # Generated by pypy/tool/import_cffi.py
2 import sys, os
3 import subprocess, weakref
4 import pytest
5 from cffi import FFI
6 from cffi.backend_ctypes import CTypesBackend
7 from extra_tests.cffi_tests.support import u, is_musl
8
9
10 SOURCE = """\
11 #include <errno.h>
12
13 #ifdef _WIN32
14 #define EXPORT __declspec(dllexport)
15 #else
16 #define EXPORT
17 #endif
18
19 EXPORT int test_getting_errno(void) {
20 errno = 123;
21 return -1;
22 }
23
24 EXPORT int test_setting_errno(void) {
25 return errno;
26 };
27
28 typedef struct {
29 long x;
30 long y;
31 } POINT;
32
33 typedef struct {
34 long left;
35 long top;
36 long right;
37 long bottom;
38 } RECT;
39
40 typedef struct {
41 unsigned char a, b, c;
42 } THREEBYTES;
43
44
45 EXPORT int PointInRect(RECT *prc, POINT pt)
46 {
47 if (pt.x < prc->left)
48 return 0;
49 if (pt.x > prc->right)
50 return 0;
51 if (pt.y < prc->top)
52 return 0;
53 if (pt.y > prc->bottom)
54 return 0;
55 return 1;
56 };
57
58 EXPORT long left = 10;
59 EXPORT long top = 20;
60 EXPORT long right = 30;
61 EXPORT long bottom = 40;
62
63 EXPORT RECT ReturnRect(int i, RECT ar, RECT* br, POINT cp, RECT dr,
64 RECT *er, POINT fp, RECT gr)
65 {
66 /*Check input */
67 if (ar.left + br->left + dr.left + er->left + gr.left != left * 5)
68 {
69 ar.left = 100;
70 return ar;
71 }
72 if (ar.right + br->right + dr.right + er->right + gr.right != right * 5)
73 {
74 ar.right = 100;
75 return ar;
76 }
77 if (cp.x != fp.x)
78 {
79 ar.left = -100;
80 }
81 if (cp.y != fp.y)
82 {
83 ar.left = -200;
84 }
85 switch(i)
86 {
87 case 0:
88 return ar;
89 break;
90 case 1:
91 return dr;
92 break;
93 case 2:
94 return gr;
95 break;
96
97 }
98 return ar;
99 }
100
101 EXPORT int my_array[7] = {0, 1, 2, 3, 4, 5, 6};
102
103 EXPORT unsigned short foo_2bytes(unsigned short a)
104 {
105 return (unsigned short)(a + 42);
106 }
107 EXPORT unsigned int foo_4bytes(unsigned int a)
108 {
109 return (unsigned int)(a + 42);
110 }
111
112 EXPORT void modify_struct_value(RECT r)
113 {
114 r.left = r.right = r.top = r.bottom = 500;
115 }
116
117 EXPORT THREEBYTES return_three_bytes(void)
118 {
119 THREEBYTES result;
120 result.a = 12;
121 result.b = 34;
122 result.c = 56;
123 return result;
124 }
125 """
126
127 class TestOwnLib(object):
128 Backend = CTypesBackend
129
130 def setup_class(cls):
131 cls.module = None
132 from extra_tests.cffi_tests.udir import udir
133 udir.join('testownlib.c').write(SOURCE)
134 if sys.platform == 'win32':
135 # did we already build it?
136 if cls.Backend is CTypesBackend:
137 dll_path = str(udir) + '\\testownlib1.dll' # only ascii for the ctypes backend
138 else:
139 dll_path = str(udir) + '\\' + (u+'testownlib\u03be.dll') # non-ascii char
140 if os.path.exists(dll_path):
141 cls.module = dll_path
142 return
143 # try (not too hard) to find the version used to compile this python
144 # no mingw
145 toolsdir = None
146 try:
147 # This will always fail on setuptools>73 which removes msvc9compiler
148 from distutils.msvc9compiler import get_build_version
149 version = get_build_version()
150 toolskey = "VS%0.f0COMNTOOLS" % version
151 toolsdir = os.environ.get(toolskey, None)
152 except Exception:
153 pass
154 if toolsdir is None:
155 return
156 productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
157 productdir = os.path.abspath(productdir)
158 vcvarsall = os.path.join(productdir, "vcvarsall.bat")
159 # 64?
160 arch = 'x86'
161 if sys.maxsize > 2**32:
162 arch = 'amd64'
163 if os.path.isfile(vcvarsall):
164 cmd = '"%s" %s' % (vcvarsall, arch) + ' & cl.exe testownlib.c ' \
165 ' /LD /Fetestownlib.dll'
166 subprocess.check_call(cmd, cwd = str(udir), shell=True)
167 os.rename(str(udir) + '\\testownlib.dll', dll_path)
168 cls.module = dll_path
169 else:
170 encoded = None
171 if cls.Backend is not CTypesBackend:
172 try:
173 unicode_name = u+'testownlibcaf\xe9'
174 encoded = unicode_name.encode(sys.getfilesystemencoding())
175 if sys.version_info >= (3,):
176 encoded = str(unicode_name)
177 except UnicodeEncodeError:
178 pass
179 if encoded is None:
180 unicode_name = u+'testownlib'
181 encoded = str(unicode_name)
182 subprocess.check_call(
183 "cc testownlib.c -shared -fPIC -o '%s.so'" % (encoded,),
184 cwd=str(udir), shell=True)
185 cls.module = os.path.join(str(udir), unicode_name + (u+'.so'))
186 print(repr(cls.module))
187
188 def test_getting_errno(self):
189 if self.module is None:
190 pytest.skip("fix the auto-generation of the tiny test lib")
191 if sys.platform == 'win32':
192 pytest.skip("fails, errno at multiple addresses")
193 ffi = FFI(backend=self.Backend())
194 ffi.cdef("""
195 int test_getting_errno(void);
196 """)
197 ownlib = ffi.dlopen(self.module)
198 res = ownlib.test_getting_errno()
199 assert res == -1
200 assert ffi.errno == 123
201
202 def test_setting_errno(self):
203 if self.module is None:
204 pytest.skip("fix the auto-generation of the tiny test lib")
205 if sys.platform == 'win32':
206 pytest.skip("fails, errno at multiple addresses")
207 if self.Backend is CTypesBackend and '__pypy__' in sys.modules:
208 pytest.skip("XXX errno issue with ctypes on pypy?")
209 ffi = FFI(backend=self.Backend())
210 ffi.cdef("""
211 int test_setting_errno(void);
212 """)
213 ownlib = ffi.dlopen(self.module)
214 ffi.errno = 42
215 res = ownlib.test_setting_errno()
216 assert res == 42
217 assert ffi.errno == 42
218
219 def test_my_array_7(self):
220 if self.module is None:
221 pytest.skip("fix the auto-generation of the tiny test lib")
222 ffi = FFI(backend=self.Backend())
223 ffi.cdef("""
224 extern int my_array[7];
225 """)
226 ownlib = ffi.dlopen(self.module)
227 for i in range(7):
228 assert ownlib.my_array[i] == i
229 assert len(ownlib.my_array) == 7
230 if self.Backend is CTypesBackend:
231 pytest.skip("not supported by the ctypes backend")
232 ownlib.my_array = list(range(10, 17))
233 for i in range(7):
234 assert ownlib.my_array[i] == 10 + i
235 ownlib.my_array = list(range(7))
236 for i in range(7):
237 assert ownlib.my_array[i] == i
238
239 def test_my_array_no_length(self):
240 if self.module is None:
241 pytest.skip("fix the auto-generation of the tiny test lib")
242 if self.Backend is CTypesBackend:
243 pytest.skip("not supported by the ctypes backend")
244 ffi = FFI(backend=self.Backend())
245 ffi.cdef("""
246 extern int my_array[];
247 """)
248 ownlib = ffi.dlopen(self.module)
249 for i in range(7):
250 assert ownlib.my_array[i] == i
251 pytest.raises(TypeError, len, ownlib.my_array)
252 ownlib.my_array = list(range(10, 17))
253 for i in range(7):
254 assert ownlib.my_array[i] == 10 + i
255 ownlib.my_array = list(range(7))
256 for i in range(7):
257 assert ownlib.my_array[i] == i
258
259 def test_keepalive_lib(self):
260 if self.module is None:
261 pytest.skip("fix the auto-generation of the tiny test lib")
262 ffi = FFI(backend=self.Backend())
263 ffi.cdef("""
264 int test_getting_errno(void);
265 """)
266 ownlib = ffi.dlopen(self.module)
267 ffi_r = weakref.ref(ffi)
268 ownlib_r = weakref.ref(ownlib)
269 func = ownlib.test_getting_errno
270 del ffi
271 import gc; gc.collect() # ownlib stays alive
272 assert ownlib_r() is not None
273 assert ffi_r() is not None # kept alive by ownlib
274 res = func()
275 assert res == -1
276
277 def test_keepalive_ffi(self):
278 if self.module is None:
279 pytest.skip("fix the auto-generation of the tiny test lib")
280 ffi = FFI(backend=self.Backend())
281 ffi.cdef("""
282 int test_getting_errno(void);
283 """)
284 ownlib = ffi.dlopen(self.module)
285 ffi_r = weakref.ref(ffi)
286 ownlib_r = weakref.ref(ownlib)
287 func = ownlib.test_getting_errno
288 del ownlib
289 import gc; gc.collect() # ffi stays alive
290 assert ffi_r() is not None
291 assert ownlib_r() is not None # kept alive by ffi
292 res = func()
293 assert res == -1
294 if sys.platform != 'win32': # else, errno at multiple addresses
295 assert ffi.errno == 123
296
297 def test_struct_by_value(self):
298 if self.module is None:
299 pytest.skip("fix the auto-generation of the tiny test lib")
300 ffi = FFI(backend=self.Backend())
301 ffi.cdef("""
302 typedef struct {
303 long x;
304 long y;
305 } POINT;
306
307 typedef struct {
308 long left;
309 long top;
310 long right;
311 long bottom;
312 } RECT;
313
314 extern long left, top, right, bottom;
315
316 RECT ReturnRect(int i, RECT ar, RECT* br, POINT cp, RECT dr,
317 RECT *er, POINT fp, RECT gr);
318 """)
319 ownlib = ffi.dlopen(self.module)
320
321 rect = ffi.new('RECT[1]')
322 pt = ffi.new('POINT[1]')
323 pt[0].x = 15
324 pt[0].y = 25
325 rect[0].left = ownlib.left
326 rect[0].right = ownlib.right
327 rect[0].top = ownlib.top
328 rect[0].bottom = ownlib.bottom
329
330 for i in range(4):
331 ret = ownlib.ReturnRect(i, rect[0], rect, pt[0], rect[0],
332 rect, pt[0], rect[0])
333 assert ret.left == ownlib.left
334 assert ret.right == ownlib.right
335 assert ret.top == ownlib.top
336 assert ret.bottom == ownlib.bottom
337
338 def test_addressof_lib(self):
339 if self.module is None:
340 pytest.skip("fix the auto-generation of the tiny test lib")
341 if self.Backend is CTypesBackend:
342 pytest.skip("not implemented with the ctypes backend")
343 ffi = FFI(backend=self.Backend())
344 ffi.cdef("extern long left; int test_getting_errno(void);")
345 lib = ffi.dlopen(self.module)
346 lib.left = 123456
347 p = ffi.addressof(lib, "left")
348 assert ffi.typeof(p) == ffi.typeof("long *")
349 assert p[0] == 123456
350 p[0] += 1
351 assert lib.left == 123457
352 pfn = ffi.addressof(lib, "test_getting_errno")
353 assert ffi.typeof(pfn) == ffi.typeof("int(*)(void)")
354 assert pfn == lib.test_getting_errno
355
356 def test_char16_char32_t(self):
357 if self.module is None:
358 pytest.skip("fix the auto-generation of the tiny test lib")
359 if self.Backend is CTypesBackend:
360 pytest.skip("not implemented with the ctypes backend")
361 ffi = FFI(backend=self.Backend())
362 ffi.cdef("""
363 char16_t foo_2bytes(char16_t);
364 char32_t foo_4bytes(char32_t);
365 """)
366 lib = ffi.dlopen(self.module)
367 assert lib.foo_2bytes(u+'\u1234') == u+'\u125e'
368 assert lib.foo_4bytes(u+'\u1234') == u+'\u125e'
369 assert lib.foo_4bytes(u+'\U00012345') == u+'\U0001236f'
370
371 def test_modify_struct_value(self):
372 if self.module is None:
373 pytest.skip("fix the auto-generation of the tiny test lib")
374 if self.Backend is CTypesBackend:
375 pytest.skip("fails with the ctypes backend on some architectures")
376 ffi = FFI(backend=self.Backend())
377 ffi.cdef("""
378 typedef struct {
379 long left;
380 long top;
381 long right;
382 long bottom;
383 } RECT;
384
385 void modify_struct_value(RECT r);
386 """)
387 lib = ffi.dlopen(self.module)
388 s = ffi.new("RECT *", [11, 22, 33, 44])
389 lib.modify_struct_value(s[0])
390 assert s.left == 11
391 assert s.top == 22
392 assert s.right == 33
393 assert s.bottom == 44
394
395 def test_dlopen_handle(self):
396 if self.module is None:
397 pytest.skip("fix the auto-generation of the tiny test lib")
398 if sys.platform == 'win32' or is_musl:
399 pytest.skip("uses 'dl' explicitly")
400 if self.__class__.Backend is CTypesBackend:
401 pytest.skip("not for the ctypes backend")
402 backend = self.Backend()
403 ffi1 = FFI(backend=backend)
404 ffi1.cdef("""void *dlopen(const char *filename, int flags);
405 int dlclose(void *handle);""")
406 lib1 = ffi1.dlopen('dl')
407 handle = lib1.dlopen(self.module.encode(sys.getfilesystemencoding()),
408 backend.RTLD_LAZY)
409 assert ffi1.typeof(handle) == ffi1.typeof("void *")
410 assert handle
411
412 ffi = FFI(backend=backend)
413 ffi.cdef("""unsigned short foo_2bytes(unsigned short a);""")
414 lib = ffi.dlopen(handle)
415 x = lib.foo_2bytes(1000)
416 assert x == 1042
417
418 err = lib1.dlclose(handle)
419 assert err == 0
420
421 def test_return_three_bytes(self):
422 if self.module is None:
423 pytest.skip("fix the auto-generation of the tiny test lib")
424 if self.__class__.Backend is CTypesBackend:
425 pytest.skip("not working on win32 on the ctypes backend")
426 ffi = FFI(backend=self.Backend())
427 ffi.cdef("""
428 typedef struct {
429 unsigned char a, b, c;
430 } THREEBYTES;
431
432 THREEBYTES return_three_bytes(void);
433 """)
434 lib = ffi.dlopen(self.module)
435 tb = lib.return_three_bytes()
436 assert tb.a == 12
437 assert tb.b == 34
438 assert tb.c == 56