summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
Diffstat (limited to 'testing')
-rw-r--r--testing/cffi0/test_ownlib.py26
-rw-r--r--testing/cffi1/test_re_python.py21
-rw-r--r--testing/cffi1/test_recompiler.py25
3 files changed, 61 insertions, 11 deletions
diff --git a/testing/cffi0/test_ownlib.py b/testing/cffi0/test_ownlib.py
index 9295163..990f259 100644
--- a/testing/cffi0/test_ownlib.py
+++ b/testing/cffi0/test_ownlib.py
@@ -371,3 +371,29 @@ class TestOwnLib(object):
assert s.top == 22
assert s.right == 33
assert s.bottom == 44
+
+ def test_dlopen_handle(self):
+ if self.module is None:
+ py.test.skip("fix the auto-generation of the tiny test lib")
+ if sys.platform == 'win32':
+ py.test.skip("uses 'dl' explicitly")
+ if self.__class__.Backend is CTypesBackend:
+ py.test.skip("not for the ctypes backend")
+ backend = self.Backend()
+ ffi1 = FFI(backend=backend)
+ ffi1.cdef("""void *dlopen(const char *filename, int flags);
+ int dlclose(void *handle);""")
+ lib1 = ffi1.dlopen('dl')
+ handle = lib1.dlopen(self.module.encode(sys.getfilesystemencoding()),
+ backend.RTLD_LAZY)
+ assert ffi1.typeof(handle) == ffi1.typeof("void *")
+ assert handle
+
+ ffi = FFI(backend=backend)
+ ffi.cdef("""unsigned short foo_2bytes(unsigned short a);""")
+ lib = ffi.dlopen(handle)
+ x = lib.foo_2bytes(1000)
+ assert x == 1042
+
+ err = lib1.dlclose(handle)
+ assert err == 0
diff --git a/testing/cffi1/test_re_python.py b/testing/cffi1/test_re_python.py
index 8b998d7..dce4f40 100644
--- a/testing/cffi1/test_re_python.py
+++ b/testing/cffi1/test_re_python.py
@@ -260,3 +260,24 @@ def test_selfref():
# based on issue #429
from re_python_pysrc import ffi
ffi.new("selfref_ptr_t")
+
+def test_dlopen_handle():
+ import _cffi_backend
+ from re_python_pysrc import ffi
+ if sys.platform == 'win32':
+ py.test.skip("uses 'dl' explicitly")
+ ffi1 = FFI()
+ ffi1.cdef("""void *dlopen(const char *filename, int flags);
+ int dlclose(void *handle);""")
+ lib1 = ffi1.dlopen('dl')
+ handle = lib1.dlopen(extmod.encode(sys.getfilesystemencoding()),
+ _cffi_backend.RTLD_LAZY)
+ assert ffi1.typeof(handle) == ffi1.typeof("void *")
+ assert handle
+
+ lib = ffi.dlopen(handle)
+ assert lib.add42(-10) == 32
+ assert type(lib.add42) is _cffi_backend.FFI.CData
+
+ err = lib1.dlclose(handle)
+ assert err == 0
diff --git a/testing/cffi1/test_recompiler.py b/testing/cffi1/test_recompiler.py
index f4ce11b..3704d97 100644
--- a/testing/cffi1/test_recompiler.py
+++ b/testing/cffi1/test_recompiler.py
@@ -26,13 +26,14 @@ def check_type_table(input, expected_output, included=None):
def verify(ffi, module_name, source, *args, **kwds):
no_cpp = kwds.pop('no_cpp', False)
+ ignore_warnings = kwds.pop('ignore_warnings', False)
kwds.setdefault('undef_macros', ['NDEBUG'])
module_name = '_CFFI_' + module_name
ffi.set_source(module_name, source)
if not os.environ.get('NO_CPP') and not no_cpp: # test the .cpp mode too
kwds.setdefault('source_extension', '.cpp')
source = 'extern "C" {\n%s\n}' % (source,)
- elif sys.platform != 'win32':
+ elif sys.platform != 'win32' and not ignore_warnings:
# add '-Werror' to the existing 'extra_compile_args' flags
from testing.support import extra_compile_args
kwds['extra_compile_args'] = (kwds.get('extra_compile_args', []) +
@@ -136,7 +137,8 @@ def test_math_sin():
import math
ffi = FFI()
ffi.cdef("float sin(double); double cos(double);")
- lib = verify(ffi, 'test_math_sin', '#include <math.h>')
+ lib = verify(ffi, 'test_math_sin', '#include <math.h>',
+ ignore_warnings=True)
assert lib.cos(1.43) == math.cos(1.43)
def test_repr_lib():
@@ -348,9 +350,9 @@ def test_verify_exact_field_offset():
lib = verify(ffi, 'test_verify_exact_field_offset',
"""struct foo_s { short a; int b; };""")
e = py.test.raises(ffi.error, ffi.new, "struct foo_s *", []) # lazily
- assert str(e.value) == ("struct foo_s: wrong offset for field 'b' (cdef "
- 'says 0, but C compiler says 4). fix it or use "...;" '
- "in the cdef for struct foo_s to make it flexible")
+ assert str(e.value).startswith(
+ "struct foo_s: wrong offset for field 'b' (cdef "
+ 'says 0, but C compiler says 4). fix it or use "...;" ')
def test_type_caching():
ffi1 = FFI(); ffi1.cdef("struct foo_s;")
@@ -645,7 +647,7 @@ def test_include_3():
ffi.cdef("sshort_t ff3(sshort_t);")
lib = verify(ffi, "test_include_3",
"typedef short sshort_t; //usually from a #include\n"
- "sshort_t ff3(sshort_t x) { return x + 42; }")
+ "sshort_t ff3(sshort_t x) { return (sshort_t)(x + 42); }")
assert lib.ff3(10) == 52
assert ffi.typeof(ffi.cast("sshort_t", 42)) is ffi.typeof("short")
assert ffi1.typeof("sshort_t") is ffi.typeof("sshort_t")
@@ -754,7 +756,7 @@ def test_unicode_libraries():
ffi = FFI()
ffi.cdef(unicode("float sin(double); double cos(double);"))
lib = verify(ffi, 'test_math_sin_unicode', unicode('#include <math.h>'),
- libraries=[unicode(lib_m)])
+ libraries=[unicode(lib_m)], ignore_warnings=True)
assert lib.cos(1.43) == math.cos(1.43)
def test_incomplete_struct_as_arg():
@@ -898,7 +900,7 @@ def test_address_of_function():
ffi.cdef("long myfunc(long x);")
lib = verify(ffi, "test_addressof_function", """
char myfunc(char x) { return (char)(x + 42); }
- """)
+ """, ignore_warnings=True)
assert lib.myfunc(5) == 47
assert lib.myfunc(0xABC05) == 47
assert not isinstance(lib.myfunc, ffi.CData)
@@ -1171,7 +1173,7 @@ def test_some_float_invalid_3():
lib = verify(ffi, 'test_some_float_invalid_3', """
typedef long double foo_t;
foo_t neg(foo_t x) { return -x; }
- """)
+ """, ignore_warnings=True)
if ffi.sizeof("long double") == ffi.sizeof("double"):
assert lib.neg(12.3) == -12.3
else:
@@ -1845,7 +1847,7 @@ def test_introspect_function():
ffi = FFI()
ffi.cdef("float f1(double);")
lib = verify(ffi, 'test_introspect_function', """
- float f1(double x) { return x; }
+ float f1(double x) { return (float)x; }
""")
assert dir(lib) == ['f1']
FUNC = ffi.typeof(lib.f1)
@@ -2150,7 +2152,8 @@ def test_call_with_nested_anonymous_struct():
lib = verify(ffi, "test_call_with_nested_anonymous_struct", """
struct foo { int a; union { int b, c; }; };
struct foo f(void) {
- struct foo s = { 40 };
+ struct foo s;
+ s.a = 40;
s.b = 200;
return s;
}