Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'cffi' in functional components in Python. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
__u32 inheritable;
} *cap_user_data_t;
int capset(cap_user_header_t header, const cap_user_data_t data);
int capget(cap_user_header_t header, cap_user_data_t data);
/* Edited highlights from `echo '#include ' | gcc -E -` */
#define PR_GET_KEEPCAPS 7
#define PR_SET_KEEPCAPS 8
int prctl (int __option, ...);
'''
ffi = cffi.FFI()
ffi.cdef(CDEF)
if platform.system() == 'Linux':
# mock.patching crt.* directly seems to upset cffi. Use an
# indirection point here for easier testing.
crt = ffi.dlopen(None)
_prctl = crt.prctl
_capget = crt.capget
_capset = crt.capset
else:
_prctl = None
_capget = None
_capset = None
r->m_numInvokes = count;
}
/* Logging */
#include
void (*python_log_callback)(int level, char *msg);
void c_log_callback(int level, const char *fmt, va_list args) {
char buf[2048];
vsprintf(buf, fmt, args);
python_log_callback(level, buf);
}
"""
verifier = Verifier(ffi, preamble, libraries=["rtmp"],
ext_package="librtmp_ffi", modulename="_binding",
sources=["src/librtmp/amf.c"])
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
def test_modify_struct_value(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("fails with the ctypes backend on some architectures")
ffi = FFI(backend=self.Backend())
ffi.cdef("""
typedef struct {
long left;
long top;
long right;
long bottom;
} RECT;
void modify_struct_value(RECT r);
""")
lib = ffi.dlopen(self.module)
s = ffi.new("RECT *", [11, 22, 33, 44])
lib.modify_struct_value(s[0])
assert s.left == 11
assert s.top == 22
assert s.right == 33
def test_const_fields():
ffi = FFI()
ffi.cdef("""struct foo_s { const int a; void *const b; };""")
lib = verify(ffi, 'test_const_fields', """
struct foo_s { const int a; void *const b; };""")
foo_s = ffi.typeof("struct foo_s")
assert foo_s.fields[0][0] == 'a'
assert foo_s.fields[0][1].type is ffi.typeof("int")
assert foo_s.fields[1][0] == 'b'
assert foo_s.fields[1][1].type is ffi.typeof("void *")
def test_parse_error():
ffi = FFI()
e = py.test.raises(CDefError, ffi.cdef, " x y z ")
assert str(e.value).startswith(
'cannot parse "x y z"\n:1:')
e = py.test.raises(CDefError, ffi.cdef, "\n\n\n x y z ")
assert str(e.value).startswith(
'cannot parse "x y z"\n:4:')
def test_some_float_invalid_2():
ffi = FFI()
ffi.cdef("typedef double... foo_t; foo_t neg(foo_t);")
lib = verify(ffi, 'test_some_float_invalid_2', """
typedef unsigned long foo_t;
foo_t neg(foo_t x) { return -x; }
""")
e = py.test.raises(ffi.error, getattr, lib, 'neg')
assert str(e.value) == ("primitive floating-point type with an unexpected "
"size (or not a float type at all)")
def test_bad_size_of_global_2():
ffi = FFI()
ffi.cdef("extern int glob[10];")
py.test.raises(VerificationError, verify, ffi,
"test_bad_size_of_global_2", "int glob[9];")
def test_enum():
ffi = FFI()
ffi.cdef("enum myenum_e { AA, BB, CC=-42 };")
target = udir.join('test_enum.py')
make_py_source(ffi, 'test_enum', str(target))
assert target.read() == r"""# auto-generated file
import _cffi_backend
def test_struct():
ffi = FFI()
ffi.cdef("struct foo_s { int a; signed char b[]; }; struct bar_s;")
target = udir.join('test_struct.py')
make_py_source(ffi, 'test_struct', str(target))
assert target.read() == r"""# auto-generated file
import _cffi_backend