Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
EXPORT int my_array[7] = {0, 1, 2, 3, 4, 5, 6};
EXPORT unsigned short foo_2bytes(unsigned short a)
{
return (unsigned short)(a + 42);
}
EXPORT unsigned int foo_4bytes(unsigned int a)
{
return (unsigned int)(a + 42);
}
"""
class TestOwnLib(object):
Backend = CTypesBackend
def setup_class(cls):
cls.module = None
from testing.udir import udir
udir.join('testownlib.c').write(SOURCE)
if sys.platform == 'win32':
import os
# did we already build it?
if cls.Backend is CTypesBackend:
dll_path = str(udir) + '\\testownlib1.dll' # only ascii for the ctypes backend
else:
dll_path = str(udir) + '\\' + (u+'testownlib\u03be.dll') # non-ascii char
if os.path.exists(dll_path):
cls.module = dll_path
return
# try (not too hard) to find the version used to compile this python
def test_function_with_struct_argument(self):
if sys.platform == 'win32':
py.test.skip("no 'inet_ntoa'")
if (self.Backend is CTypesBackend and
'__pypy__' in sys.builtin_module_names):
py.test.skip("ctypes limitation on pypy")
ffi = FFI(backend=self.Backend())
ffi.cdef("""
struct in_addr { unsigned int s_addr; };
char *inet_ntoa(struct in_addr in);
""")
needs_dlopen_none()
ffi.C = ffi.dlopen(None)
ina = ffi.new("struct in_addr *", [0x04040404])
a = ffi.C.inet_ntoa(ina[0])
assert ffi.string(a) == b'4.4.4.4'
def test_char16_char32_t(self):
if self.module is None:
py.test.skip("fix the auto-generation of the tiny test lib")
if self.Backend is CTypesBackend:
py.test.skip("not implemented with the ctypes backend")
ffi = FFI(backend=self.Backend())
ffi.cdef("""
char16_t foo_2bytes(char16_t);
char32_t foo_4bytes(char32_t);
""")
lib = ffi.dlopen(self.module)
assert lib.foo_2bytes(u+'\u1234') == u+'\u125e'
assert lib.foo_4bytes(u+'\u1234') == u+'\u125e'
assert lib.foo_4bytes(u+'\U00012345') == u+'\U0001236f'
def test_ctypes_backend_forces_generic_engine():
from cffi.backend_ctypes import CTypesBackend
ffi = FFI(backend=CTypesBackend())
ffi.cdef("int func(int a);")
lib = ffi.verify("int func(int a) { return a * 42; }")
assert not hasattr(lib, '_cffi_python_module')
assert hasattr(lib, '_cffi_generic_module')
assert lib.func(100) == 4200
def test_ctypes_backend_forces_generic_engine():
from cffi.backend_ctypes import CTypesBackend
ffi = FFI(backend=CTypesBackend())
ffi.cdef("int func(int a);")
lib = ffi.verify("int func(int a) { return a * 42; }")
assert not hasattr(lib, '_cffi_python_module')
assert hasattr(lib, '_cffi_generic_module')
assert lib.func(100) == 4200
def test_stdcall_only_on_windows(self):
ffi = FFI(backend=self.Backend())
ffi.cdef("double __stdcall sin(double x);") # stdcall ignored
m = ffi.dlopen(lib_m)
if (sys.platform == 'win32' and sys.maxsize < 2**32 and
self.Backend is not CTypesBackend):
assert "double(__stdcall *)(double)" in str(ffi.typeof(m.sin))
else:
assert "double(*)(double)" in str(ffi.typeof(m.sin))
x = m.sin(1.23)
assert x == math.sin(1.23)
{
return (unsigned short)(a + 42);
}
EXPORT unsigned int foo_4bytes(unsigned int a)
{
return (unsigned int)(a + 42);
}
EXPORT void modify_struct_value(RECT r)
{
r.left = r.right = r.top = r.bottom = 500;
}
"""
class TestOwnLib(object):
Backend = CTypesBackend
def setup_class(cls):
cls.module = None
from extra_tests.cffi_tests.udir import udir
udir.join('testownlib.c').write(SOURCE)
if sys.platform == 'win32':
# did we already build it?
if cls.Backend is CTypesBackend:
dll_path = str(udir) + '\\testownlib1.dll' # only ascii for the ctypes backend
else:
dll_path = str(udir) + '\\' + (u+'testownlib\u03be.dll') # non-ascii char
if os.path.exists(dll_path):
cls.module = dll_path
return
# try (not too hard) to find the version used to compile this python
# no mingw
def test_my_array_7(self):
if self.module is None:
py.test.skip("fix the auto-generation of the tiny test lib")
ffi = FFI(backend=self.Backend())
ffi.cdef("""
extern int my_array[7];
""")
ownlib = ffi.dlopen(self.module)
for i in range(7):
assert ownlib.my_array[i] == i
assert len(ownlib.my_array) == 7
if self.Backend is CTypesBackend:
py.test.skip("not supported by the ctypes backend")
ownlib.my_array = list(range(10, 17))
for i in range(7):
assert ownlib.my_array[i] == 10 + i
ownlib.my_array = list(range(7))
for i in range(7):
assert ownlib.my_array[i] == i
def __init__(self, include_path=None, **kwargs):
self.ffi = cffi_patches.FFIWithBetterParser(backend=cffi.backend_ctypes.CTypesBackend())
self.pp = preprocessor.Preprocessor(lexer.build())
self._last_source = None
if include_path is None:
include_path = DEFAULT_INCLUDE_PATH
for path in include_path:
self.pp.add_path(path)
if sys.platform != "ios":
warnings.warn(UserWarning("This library is meant to run in the Pythonista app on iOS. You seem to be on a different platform. Things may not work well."))
self.cdef_include("builtin_arm.h")
if sys.maxsize > 2**31 - 1:
# 64-bit