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_implicit_unicode_on_windows():
if sys.platform != 'win32':
py.test.skip("win32-only test")
ffi = FFI()
e = py.test.raises(FFIError, ffi.cdef, "int foo(LPTSTR);")
assert str(e.value) == ("The Windows type 'LPTSTR' is only available after"
" you call ffi.set_unicode()")
for with_unicode in [True, False]:
ffi = FFI()
ffi.set_unicode(with_unicode)
ffi.cdef("""
DWORD GetModuleFileName(HMODULE hModule, LPTSTR lpFilename,
DWORD nSize);
""")
lib = ffi.verify("""
#include
""", libraries=['Kernel32'])
outbuf = ffi.new("TCHAR[]", 200)
n = lib.GetModuleFileName(ffi.NULL, outbuf, 500)
assert 0 < n < 500
for i in range(n):
def test_implicit_unicode_on_windows():
if sys.platform != 'win32':
py.test.skip("win32-only test")
ffi = FFI()
e = py.test.raises(FFIError, ffi.cdef, "int foo(LPTSTR);")
assert str(e.value) == ("The Windows type 'LPTSTR' is only available after"
" you call ffi.set_unicode()")
for with_unicode in [True, False]:
ffi = FFI()
ffi.set_unicode(with_unicode)
ffi.cdef("""
DWORD GetModuleFileName(HMODULE hModule, LPTSTR lpFilename,
DWORD nSize);
""")
lib = ffi.verify("""
#include
""", libraries=['Kernel32'])
outbuf = ffi.new("TCHAR[]", 200)
n = lib.GetModuleFileName(ffi.NULL, outbuf, 500)
assert 0 < n < 500
for i in range(n):
def test_override():
ffi = FFI(backend=FakeBackend())
needs_dlopen_none()
C = ffi.dlopen(None)
ffi.cdef("int foo(void);")
py.test.raises(FFIError, ffi.cdef, "long foo(void);")
assert C.foo.BType == ', False>'
ffi.cdef("long foo(void);", override=True)
assert C.foo.BType == ', False>'
def test_implicit_unicode_on_windows():
from cffi import FFIError
if sys.platform != 'win32':
py.test.skip("win32-only test")
ffi = FFI()
e = py.test.raises(FFIError, ffi.cdef, "int foo(LPTSTR);")
assert str(e.value) == ("The Windows type 'LPTSTR' is only available after"
" you call ffi.set_unicode()")
for with_unicode in [True, False]:
ffi = FFI()
ffi.set_unicode(with_unicode)
ffi.cdef("""
DWORD GetModuleFileName(HMODULE hModule, LPTSTR lpFilename,
DWORD nSize);
""")
lib = ffi.verify("""
#include
""", libraries=['Kernel32'])
outbuf = ffi.new("TCHAR[]", 200)
n = lib.GetModuleFileName(ffi.NULL, outbuf, 500)
assert 0 < n < 500
for i in range(n):
def test_unsupported_some_primitive_types():
ffi = FFI()
py.test.raises((FFIError, # with pycparser <= 2.17
CDefError), # with pycparser >= 2.18
ffi.cdef, """typedef void... foo_t;""")
#
ffi.cdef("typedef int... foo_t;")
py.test.raises(VerificationError, ffi.verify, "typedef float foo_t;")
def test_some_float_invalid_1():
ffi = FFI()
py.test.raises((FFIError, # with pycparser <= 2.17
CDefError), # with pycparser >= 2.18
ffi.cdef, "typedef long double... foo_t;")
def test_some_float_invalid_1():
ffi = FFI()
py.test.raises((FFIError, # with pycparser <= 2.17
CDefError), # with pycparser >= 2.18
ffi.cdef, "typedef long double... foo_t;")
def test_implicit_unicode_on_windows():
from cffi import FFIError
if sys.platform != 'win32':
py.test.skip("win32-only test")
ffi = FFI()
e = py.test.raises(FFIError, ffi.cdef, "int foo(LPTSTR);")
assert str(e.value) == ("The Windows type 'LPTSTR' is only available after"
" you call ffi.set_unicode()")
for with_unicode in [True, False]:
ffi = FFI()
ffi.set_unicode(with_unicode)
ffi.cdef("""
DWORD GetModuleFileName(HMODULE hModule, LPTSTR lpFilename,
DWORD nSize);
""")
lib = ffi.verify("""
#include
""", libraries=['Kernel32'])
outbuf = ffi.new("TCHAR[]", 200)
n = lib.GetModuleFileName(ffi.NULL, outbuf, 500)
assert 0 < n < 500
for i in range(n):
def test_some_float_invalid_1():
ffi = FFI()
py.test.raises((FFIError, # with pycparser <= 2.17
CDefError), # with pycparser >= 2.18
ffi.cdef, "typedef long double... foo_t;")
"""
import cffi
import cffi.api
__FFI__ = cffi.FFI()
# C helper functions
try:
__FFI__.cdef('''FILE *fmemopen(void *, size_t, const char*);
int fclose(FILE*);
void free(void *ptr);
''')
except cffi.FFIError:
# only once is needed
pass
def add_cdef_for_type(ctype, cdef, packed=False):
'''
Define the given cdef, only if given ctype isn't defined yet.
Warning: this doesn't work for function ctype !
@return None
'''
try:
__FFI__.getctype(ctype)
except cffi.api.CDefError:
__FFI__.cdef(cdef, packed)