本文整理匯總了Python中cffi.FFI屬性的典型用法代碼示例。如果您正苦於以下問題:Python cffi.FFI屬性的具體用法?Python cffi.FFI怎麽用?Python cffi.FFI使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類cffi
的用法示例。
在下文中一共展示了cffi.FFI屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: cast_int_addr
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def cast_int_addr(n):
"""Cast an address to a Python int
This could be a Python integer or a CFFI pointer
"""
if isinstance(n, (int, long)):
return n
try:
import cffi
except ImportError:
pass
else:
# from pyzmq, this is an FFI void *
ffi = cffi.FFI()
if isinstance(n, ffi.CData):
return int(ffi.cast("size_t", n))
raise ValueError("Cannot cast %r to int" % n)
示例2: __init__
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def __init__(self, on_device='cpu', blank_label=0):
libpath = get_ctc_lib()
self.ffi = FFI()
self.ffi.cdef(ctc_header())
self.ctclib = self.ffi.dlopen(libpath)
supported_devices = ['cpu', 'gpu']
if on_device not in supported_devices:
print("the requested device {} is not supported".format(
on_device), file=sys.stderr)
sys.exit(1)
assign_device = 0 if on_device is 'cpu' else 1
self.options = self.ffi.new('ctcOptions*',
{"loc": assign_device,
"blank_label": blank_label})[0]
self.size_in_bytes = self.ffi.new("size_t*")
self.nout = None
self.bsz = None
示例3: test_callback_onerror
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def test_callback_onerror(self):
ffi = FFI(backend=self.Backend())
seen = []
def oops(*args):
seen.append(args)
def otherfunc():
raise LookupError
def cb(n):
otherfunc()
a = ffi.callback("int(*)(int)", cb, error=42, onerror=oops)
res = a(234)
assert res == 42
assert len(seen) == 1
exc, val, tb = seen[0]
assert exc is LookupError
assert isinstance(val, LookupError)
assert tb.tb_frame.f_code.co_name == 'cb'
assert tb.tb_frame.f_locals['n'] == 234
示例4: test_ffi_new_allocator_4
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def test_ffi_new_allocator_4(self):
ffi = FFI(backend=self.Backend())
py.test.raises(TypeError, ffi.new_allocator, free=lambda x: None)
#
def myalloc2(size):
raise LookupError
alloc2 = ffi.new_allocator(myalloc2)
py.test.raises(LookupError, alloc2, "int[5]")
#
def myalloc3(size):
return 42
alloc3 = ffi.new_allocator(myalloc3)
e = py.test.raises(TypeError, alloc3, "int[5]")
assert str(e.value) == "alloc() must return a cdata object (got int)"
#
def myalloc4(size):
return ffi.cast("int", 42)
alloc4 = ffi.new_allocator(myalloc4)
e = py.test.raises(TypeError, alloc4, "int[5]")
assert str(e.value) == "alloc() must return a cdata pointer, not 'int'"
#
def myalloc5(size):
return ffi.NULL
alloc5 = ffi.new_allocator(myalloc5)
py.test.raises(MemoryError, alloc5, "int[5]")
示例5: test_callback_returning_void
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def test_callback_returning_void(self):
ffi = FFI(backend=self.Backend())
for returnvalue in [None, 42]:
def cb():
return returnvalue
fptr = ffi.callback("void(*)(void)", cb)
old_stderr = sys.stderr
try:
sys.stderr = StringIO()
returned = fptr()
printed = sys.stderr.getvalue()
finally:
sys.stderr = old_stderr
assert returned is None
if returnvalue is None:
assert printed == ''
else:
assert "None" in printed
示例6: test_fputs_custom_FILE
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def test_fputs_custom_FILE(self):
if self.Backend is CTypesBackend:
py.test.skip("FILE not supported with the ctypes backend")
filename = str(udir.join('fputs_custom_FILE'))
ffi = FFI(backend=self.Backend())
ffi.cdef("int fputs(const char *, FILE *);")
C = ffi.dlopen(None)
with open(filename, 'wb') as f:
f.write(b'[')
C.fputs(b"hello from custom file", f)
f.write(b'][')
C.fputs(b"some more output", f)
f.write(b']')
with open(filename, 'rb') as f:
res = f.read()
assert res == b'[hello from custom file][some more output]'
示例7: test_free_callback_cycle
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def test_free_callback_cycle(self):
if self.Backend is CTypesBackend:
py.test.skip("seems to fail with the ctypes backend on windows")
import weakref
def make_callback(data):
container = [data]
callback = ffi.callback('int()', lambda: len(container))
container.append(callback)
# Ref cycle: callback -> lambda (closure) -> container -> callback
return callback
class Data(object):
pass
ffi = FFI(backend=self.Backend())
data = Data()
callback = make_callback(data)
wr = weakref.ref(data)
del callback, data
for i in range(3):
if wr() is not None:
import gc; gc.collect()
assert wr() is None # 'data' does not leak
示例8: test_setting_errno
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def test_setting_errno(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("fails, errno at multiple addresses")
if self.Backend is CTypesBackend and '__pypy__' in sys.modules:
py.test.skip("XXX errno issue with ctypes on pypy?")
ffi = FFI(backend=self.Backend())
ffi.cdef("""
int test_setting_errno(void);
""")
ownlib = ffi.dlopen(self.module)
ffi.errno = 42
res = ownlib.test_setting_errno()
assert res == 42
assert ffi.errno == 42
示例9: test_my_array_7
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
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("""
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
示例10: test_my_array_no_length
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def test_my_array_no_length(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 supported by the ctypes backend")
ffi = FFI(backend=self.Backend())
ffi.cdef("""
int my_array[];
""")
ownlib = ffi.dlopen(self.module)
for i in range(7):
assert ownlib.my_array[i] == i
py.test.raises(TypeError, len, ownlib.my_array)
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
示例11: test_keepalive_lib
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def test_keepalive_lib(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("""
int test_getting_errno(void);
""")
ownlib = ffi.dlopen(self.module)
ffi_r = weakref.ref(ffi)
ownlib_r = weakref.ref(ownlib)
func = ownlib.test_getting_errno
del ffi
import gc; gc.collect() # ownlib stays alive
assert ownlib_r() is not None
assert ffi_r() is not None # kept alive by ownlib
res = func()
assert res == -1
示例12: test_remove_comments
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def test_remove_comments():
ffi = FFI(backend=FakeBackend())
ffi.cdef("""
double /*comment here*/ sin // blah blah
/* multi-
line-
//comment */ (
// foo
double // bar /* <- ignored, because it's in a comment itself
x, double/*several*//*comment*/y) /*on the same line*/
;
""")
m = ffi.dlopen(lib_m)
func = m.sin
assert func.name == 'sin'
assert func.BType == '<func (<double>, <double>), <double>, False>'
示例13: test_remove_line_continuation_comments
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def test_remove_line_continuation_comments():
ffi = FFI(backend=FakeBackend())
ffi.cdef("""
double // blah \\
more comments
x(void);
double // blah\\\\
y(void);
double // blah\\ \
etc
z(void);
""")
m = ffi.dlopen(lib_m)
m.x
m.y
m.z
示例14: test__is_constant_globalvar
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def test__is_constant_globalvar():
for input, expected_output in [
("int a;", False),
("const int a;", True),
("int *a;", False),
("const int *a;", False),
("int const *a;", False),
("int *const a;", True),
("int a[5];", False),
("const int a[5];", False),
("int *a[5];", False),
("const int *a[5];", False),
("int const *a[5];", False),
("int *const a[5];", False),
("int a[5][6];", False),
("const int a[5][6];", False),
]:
ffi = FFI()
ffi.cdef(input)
declarations = ffi._parser._declarations
assert ('constant a' in declarations) == expected_output
assert ('variable a' in declarations) == (not expected_output)
示例15: _import_cffi
# 需要導入模塊: import cffi [as 別名]
# 或者: from cffi import FFI [as 別名]
def _import_cffi():
global ffi, CData
if ffi is not None:
return
try:
import cffi
ffi = cffi.FFI()
CData = ffi.CData
except ImportError:
ffi = False