Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_unnamed_struct():
ffi = FFI(backend=FakeBackend())
ffi.cdef("typedef struct { int x; } foo_t;\n"
"typedef struct { int y; } *bar_p;\n")
assert 'typedef foo_t' in ffi._parser._declarations
assert 'typedef bar_p' in ffi._parser._declarations
assert 'anonymous foo_t' in ffi._parser._declarations
type_foo = ffi._parser.parse_type("foo_t")
type_bar = ffi._parser.parse_type("bar_p").totype
assert repr(type_foo) == ""
assert repr(type_bar) == ""
py.test.raises(VerificationError, type_bar.get_c_name)
assert type_foo.get_c_name() == "foo_t"
def test_duplicate_enum():
ffi = FFI()
ffi.cdef("enum e1 { A1, ... }; enum e2 { A1, ... };")
py.test.raises(VerificationError, verify, ffi, 'test_duplicate_enum',
"enum e1 { A1 }; enum e2 { B1 };")
def test_invalid_dotdotdot_in_macro():
ffi = FFI()
ffi.cdef("#define FOO ...")
target = udir.join('test_invalid_dotdotdot_in_macro.py')
e = py.test.raises(VerificationError, make_py_source, ffi,
'test_invalid_dotdotdot_in_macro', str(target))
assert str(e.value) == ("macro FOO: cannot use the syntax '...' in "
"'#define FOO ...' when using the ABI mode")
def _check_field_match(typename, real, expect_mismatch):
ffi = FFI()
testing_by_size = (expect_mismatch == 'by_size')
if testing_by_size:
expect_mismatch = ffi.sizeof(typename) != ffi.sizeof(real)
ffi.cdef("struct foo_s { %s x; ...; };" % typename)
try:
ffi.verify("struct foo_s { %s x; };" % real)
ffi.new("struct foo_s *", []) # because some mismatches show up lazily
except (VerificationError, ffi.error):
if not expect_mismatch:
if testing_by_size and typename != real:
print("ignoring mismatch between %s* and %s* even though "
"they have the same size" % (typename, real))
return
raise AssertionError("unexpected mismatch: %s should be accepted "
"as equal to %s" % (typename, real))
else:
if expect_mismatch:
raise AssertionError("mismatch not detected: "
"%s != %s" % (typename, real))
def _check_field_match(typename, real, expect_mismatch):
ffi = FFI()
testing_by_size = (expect_mismatch == 'by_size')
if testing_by_size:
expect_mismatch = ffi.sizeof(typename) != ffi.sizeof(real)
ffi.cdef("struct foo_s { %s x; ...; };" % typename)
try:
ffi.verify("struct foo_s { %s x; };" % real)
ffi.new("struct foo_s *", []) # because some mismatches show up lazily
except (VerificationError, ffi.error):
if not expect_mismatch:
if testing_by_size and typename != real:
print("ignoring mismatch between %s* and %s* even though "
"they have the same size" % (typename, real))
return
raise AssertionError("unexpected mismatch: %s should be accepted "
"as equal to %s" % (typename, real))
else:
if expect_mismatch:
raise AssertionError("mismatch not detected: "
"%s != %s" % (typename, real))
def test_unnamed_struct():
ffi = FFI(backend=FakeBackend())
ffi.cdef("typedef struct { int x; } foo_t;\n"
"typedef struct { int y; } *bar_p;\n")
assert 'typedef foo_t' in ffi._parser._declarations
assert 'typedef bar_p' in ffi._parser._declarations
assert 'anonymous foo_t' in ffi._parser._declarations
type_foo = ffi._parser.parse_type("foo_t")
type_bar = ffi._parser.parse_type("bar_p").totype
assert repr(type_foo) == ""
assert repr(type_bar) == ""
py.test.raises(VerificationError, type_bar.get_c_name)
assert type_foo.get_c_name() == "foo_t"
def test_missing_function(ffi=None):
# uses the FFI hacked above with '-Werror'
if ffi is None:
ffi = FFI()
ffi.cdef("void some_completely_unknown_function();")
try:
lib = ffi.verify()
except (VerificationError, OSError, ImportError):
pass # expected case: we get a VerificationError
else:
# but depending on compiler and loader details, maybe
# 'lib' could actually be imported but will fail if we
# actually try to call the unknown function... Hard
# to test anything more.
pass
def test_misdeclared_field_1():
ffi = FFI()
ffi.cdef("struct foo_s { int a[5]; };")
try:
verify(ffi, 'test_misdeclared_field_1',
"struct foo_s { int a[6]; };")
except VerificationError:
pass # ok, fail during compilation already (e.g. C++)
else:
assert ffi.sizeof("struct foo_s") == 24 # found by the actual C code
try:
# lazily build the fields and boom:
p = ffi.new("struct foo_s *")
p.a
assert False, "should have raised"
except ffi.error as e:
assert str(e).startswith("struct foo_s: wrong size for field 'a' "
"(cdef says 20, but C compiler says 24)")
def _Wconversion(cdef, source, **kargs):
if sys.platform in ('win32', 'darwin'):
py.test.skip("needs GCC")
ffi = FFI()
ffi.cdef(cdef)
py.test.raises(VerificationError, ffi.verify, source, **kargs)
extra_compile_args_orig = extra_compile_args[:]
extra_compile_args.remove('-Wconversion')
try:
lib = ffi.verify(source, **kargs)
finally:
extra_compile_args[:] = extra_compile_args_orig
return lib
version_info = tuple(map(int, __version__.split('.')))
# clean up old compiled library binaries (not needed in production?)
cffi.verifier.cleanup_tmpdir()
thisdir = os.path.dirname(os.path.realpath(__file__))
# load FFI definitions and custom C code
ffi = cffi.FFI()
with open(os.path.join(thisdir, 'pyuv_cffi_cdef.c')) as f:
ffi.cdef(f.read())
try:
with open(os.path.join(thisdir, 'pyuv_cffi.c')) as f:
libuv = ffi.verify(f.read(), libraries=['uv'])
except VerificationError as e:
print(e)
exit(1)
UV_READABLE = libuv.UV_READABLE
UV_WRITABLE = libuv.UV_WRITABLE
UV_RUN_DEFAULT = libuv.UV_RUN_DEFAULT
UV_RUN_ONCE = libuv.UV_RUN_ONCE
UV_RUN_NOWAIT = libuv.UV_RUN_NOWAIT
alive = []
class Loop:
def __init__(self):
self.loop_h = ffi.new('uv_loop_t *')