How to use the cffi.FFI function in cffi

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 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 Rinoahu / SwiftOrtho / bin / deprecate / deprecate2 / apclust_blk.py View on Github external
def __init__(self, name, shape=None, dtype='i'):
        assert isinstance(shape, tuple)
        n, d = shape
        self.n = n
        self.d = d
        self.dtype = dtype
        #self.data = darray(name, n * d, self.dtype)
        self.ffi = FFI()
        sa = np.memmap(name, shape=n*d, dtype=self.dtype, mode='r+')
        self.data = self.ffi.cast('float *', sa.ctypes.data)
        self.shape = shape
        print 'initial', n * d, len(sa), n, d
github facebookexperimental / eden / eden / scm / edenscm / mercurial / cffi / osutilbuild.py View on Github external
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2.

# Copyright 2016 Mercurial Contributors
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

from __future__ import absolute_import

# pyre-fixme[21]: Could not find `cffi`.
import cffi


ffi = cffi.FFI()
ffi.set_source(
    "mercurial.cffi._osutil",
    """
#include 
#include 
#include 
#include 
#include 

typedef struct val_attrs {
    uint32_t          length;
    attribute_set_t   returned;
    attrreference_t   name_info;
    fsobj_type_t      obj_type;
    struct timespec   mtime;
    uint32_t          accessmask;
github brimdata / zed / python / brim / src / build_zqext.py View on Github external
import os
import sys

from cffi import FFI

# This ffibuilder code expects an archive via the Go c-archive buildmode:
# go build -buildmode=c-archive -o python/brim/build/zqext/libzqext.a python/brim/src/zqext.go
zqextbuild = os.path.abspath(
    os.path.join(os.path.dirname(os.path.abspath(__file__)),
                 '..', 'build', 'zqext'))

extra_link_args = []
if sys.platform == 'darwin':
    extra_link_args = ['-framework', 'Security']

ffibuilder = FFI()
ffibuilder.cdef("""
void free(void *ptr);

typedef unsigned char GoUint8;
typedef struct { const char *p; ptrdiff_t n; } GoString;

struct ErrorTest_return {
	char* r0;
	GoUint8 r1;
};

extern struct ErrorTest_return ErrorTest();

struct ZqlFileEval_return {
	char* r0;
	GoUint8 r1;