How to use cffi - 10 common examples

To help you get started, we’ve selected a few cffi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github openstack / oslo.privsep / oslo_privsep / capabilities.py View on Github external
__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

github chrippa / python-librtmp / librtmp_ffi / verifier.py View on Github external
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"])
github JarryShaw / f2format / vendor / pypy / extra_tests / cffi_tests / cffi0 / test_function.py View on Github external
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
github JarryShaw / f2format / vendor / pypy / extra_tests / cffi_tests / cffi0 / test_ownlib.py View on Github external
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
github holzschu / python3_ios / extraPackages / cffi-1.11.5 / testing / cffi1 / test_recompiler.py View on Github external
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 *")
github holzschu / python3_ios / extraPackages / cffi-1.11.5 / testing / cffi0 / test_parsing.py View on Github external
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:')
github JarryShaw / f2format / vendor / pypy / extra_tests / cffi_tests / cffi1 / test_recompiler.py View on Github external
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)")
github JarryShaw / f2format / vendor / pypy / extra_tests / cffi_tests / cffi1 / test_recompiler.py View on Github external
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];")
github holzschu / python3_ios / extraPackages / cffi-1.11.5 / testing / cffi1 / test_dlopen.py View on Github external
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
github JarryShaw / f2format / vendor / pypy / extra_tests / cffi_tests / cffi1 / test_dlopen.py View on Github external
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