コード例 #1
0
 def test_load_library(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!3*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic)
     library = v.load_library()
     assert library.sin(12.3) == math.sin(12.3)
 def test_load_library(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!3*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic)
     library = v.load_library()
     assert library.sin(12.3) == math.sin(12.3)
 def test_extension_forces_write_source(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there9!%s*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic)
     assert not os.path.exists(v.sourcefilename)
     v.get_extension()
     assert os.path.exists(v.sourcefilename)
コード例 #4
0
 def test_extension_forces_write_source(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there9!%s*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic)
     assert not os.path.exists(v.sourcefilename)
     v.get_extension()
     assert os.path.exists(v.sourcefilename)
コード例 #5
0
 def test_name_from_checksum_of_csrc(self):
     names = []
     for csrc in ['123', '123', '1234']:
         ffi = FFI()
         ffi.cdef("double sin(double x);")
         v = Verifier(ffi, csrc, force_generic_engine=self.generic)
         names.append(v.get_module_name())
     assert names[0] == names[1] != names[2]
コード例 #6
0
ファイル: test_zdistutils.py プロジェクト: abhinavthomas/pypy
 def test_name_from_checksum_of_csrc(self):
     names = []
     for csrc in ['123', '123', '1234']:
         ffi = FFI()
         ffi.cdef("double sin(double x);")
         v = Verifier(ffi, csrc, force_generic_engine=self.generic)
         names.append(v.get_module_name())
     assert names[0] == names[1] != names[2]
コード例 #7
0
 def test_write_source(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic)
     v.write_source()
     with open(v.sourcefilename, 'r') as f:
         data = f.read()
     assert csrc in data
コード例 #8
0
 def finalize_options(self):
     from cffi.verifier import Verifier
     import _libpassacre
     verifier = Verifier(
         _libpassacre.ffi, _libpassacre.preamble, modulename='_libpassacre_c',
         include_dirs=[libpassacre_build_dir],
         extra_objects=[os.path.join(libpassacre_build_dir, 'libpassacre.a')])
     self.distribution.ext_modules = [verifier.get_extension()]
     _build.finalize_options(self)
 def test_name_from_checksum_of_cdef(self):
     names = []
     for csrc in ['double', 'double', 'float']:
         ffi = FFI()
         ffi.cdef("%s sin(double x);" % csrc)
         v = Verifier(ffi, "#include <math.h>",
                      force_generic_engine=self.generic)
         names.append(v.get_module_name())
     assert names[0] == names[1] != names[2]
 def test_verifier_args(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!4*/#include "test_verifier_args.h"\n' % self
     udir.join('test_verifier_args.h').write('#include <math.h>\n')
     v = Verifier(ffi, csrc, include_dirs=[str(udir)],
                  force_generic_engine=self.generic)
     library = v.load_library()
     assert library.sin(12.3) == math.sin(12.3)
 def test_write_source(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic)
     v.write_source()
     with open(v.sourcefilename, 'r') as f:
         data = f.read()
     assert csrc in data
コード例 #12
0
 def test_name_from_checksum_of_cdef(self):
     names = []
     for csrc in ['double', 'double', 'float']:
         ffi = FFI()
         ffi.cdef("%s sin(double x);" % csrc)
         v = Verifier(ffi, "#include <math.h>",
                      force_generic_engine=self.generic,
                      libraries=[self.lib_m])
         names.append(v.get_module_name())
     assert names[0] == names[1] != names[2]
コード例 #13
0
 def test_verifier_args(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!4*/#include "test_verifier_args.h"\n' % self
     udir.join('test_verifier_args.h').write('#include <math.h>\n')
     v = Verifier(ffi, csrc, include_dirs=[str(udir)],
                  force_generic_engine=self.generic,
                  libraries=[self.lib_m])
     library = v.load_library()
     assert library.sin(12.3) == math.sin(12.3)
コード例 #14
0
ファイル: test_zdistutils.py プロジェクト: abhinavthomas/pypy
 def test_write_source_explicit_filename(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic,
                  libraries=[self.lib_m])
     v.sourcefilename = filename = str(udir.join('write_source.c'))
     v.write_source()
     assert filename == v.sourcefilename
     with open(filename, 'r') as f:
         data = f.read()
     assert csrc in data
 def test_write_source_to_file_obj(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic)
     try:
         from StringIO import StringIO
     except ImportError:
         from io import StringIO
     f = StringIO()
     v.write_source(file=f)
     assert csrc in f.getvalue()
コード例 #16
0
 def test_write_source_to_file_obj(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic)
     try:
         from StringIO import StringIO
     except ImportError:
         from io import StringIO
     f = StringIO()
     v.write_source(file=f)
     assert csrc in f.getvalue()
コード例 #17
0
 def test_write_source_explicit_filename(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic,
                  libraries=[self.lib_m])
     v.sourcefilename = filename = str(udir.join('write_source.c'))
     v.write_source()
     assert filename == v.sourcefilename
     with open(filename, 'r') as f:
         data = f.read()
     assert csrc in data
コード例 #18
0
 def test_compile_module(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic)
     v.compile_module()
     assert v.get_module_name().startswith('_cffi_')
     if v.generates_python_module():
         mod = imp.load_dynamic(v.get_module_name(), v.modulefilename)
         assert hasattr(mod, '_cffi_setup')
コード例 #19
0
 def test_compile_module_explicit_filename(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!2*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic)
     basename = self.__class__.__name__ + 'test_compile_module'
     v.modulefilename = filename = str(udir.join(basename + '.so'))
     v.compile_module()
     assert filename == v.modulefilename
     assert v.get_module_name() == basename
     if v.generates_python_module():
         mod = imp.load_dynamic(v.get_module_name(), v.modulefilename)
         assert hasattr(mod, '_cffi_setup')
 def test_compile_module(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic)
     v.compile_module()
     assert v.get_module_name().startswith('_cffi_')
     if v.generates_python_module():
         mod = imp.load_dynamic(v.get_module_name(), v.modulefilename)
         assert hasattr(mod, '_cffi_setup')
 def test_compile_module_explicit_filename(self):
     ffi = FFI()
     ffi.cdef("double sin(double x);")
     csrc = '/*hi there %s!2*/\n#include <math.h>\n' % self
     v = Verifier(ffi, csrc, force_generic_engine=self.generic)
     basename = self.__class__.__name__ + 'test_compile_module'
     v.modulefilename = filename = str(udir.join(basename + '.so'))
     v.compile_module()
     assert filename == v.modulefilename
     assert v.get_module_name() == basename
     if v.generates_python_module():
         mod = imp.load_dynamic(v.get_module_name(), v.modulefilename)
         assert hasattr(mod, '_cffi_setup')
コード例 #22
0
def build_ffi(cdef_source, verify_source, libraries=[], extra_compile_args=[],
              extra_link_args=[]):
    ffi = FFI()
    ffi.cdef(cdef_source)

    ffi.verifier = Verifier(
        ffi,
        verify_source,
        tmpdir='',
        modulename=_create_modulename(cdef_source, verify_source, sys.version),
        libraries=libraries,
        ext_package="cryptography",
        extra_compile_args=extra_compile_args,
        extra_link_args=extra_link_args,
    )

    ffi.verifier.compile_module = _compile_module
    ffi.verifier._compile_module = _compile_module

    return ffi
コード例 #23
0
            if( (!croaring_get_elt( x, stop - sign , &first_elt)) || (!croaring_get_elt( x, start, &last_elt)) )
                return roaring_bitmap_create();
        }
        _croaring = roaring_bitmap_from_range(first_elt, last_elt + 1, abs(step));
        roaring_bitmap_and_inplace(_croaring, x);
        _croaring->copy_on_write = x->copy_on_write;
        return _croaring;
    }else{
        return NULL;
    }
}
"""
ffi.cdef(CDEF)
ffi.verifier = Verifier(ffi,
                        SOURCE,
                        include_dirs=[include_dir],
                        modulename=_create_modulename(CDEF, SOURCE,
                                                      sys.version),
                        extra_compile_args=['-std=c99', '-O3', '-msse4.2'])

lib = ffi.verifier.load_library()


class BitSet(object):
    def __init__(self, values=None, copy_on_write=False, croaring=None):
        if croaring:
            assert values is None and not copy_on_write
            self._croaring = croaring
            return
        elif values is None:
            self._croaring = lib.roaring_bitmap_create()
        elif isinstance(values, self.__class__):
コード例 #24
0
import math
import sys

from cffi.verifier import Verifier

from _libpassacre import ffi, preamble

ffi.verifier = Verifier(ffi,
                        preamble,
                        ext_package='passacre',
                        modulename='_libpassacre_c')


def _no_compilation(*a, **kw):
    raise RuntimeError('cffi implicit compilation attempted')


ffi.verifier.compile_module = ffi.verifier._compile_module = _no_compilation
C = ffi.verifier.load_library()

_ALGORITHMS = {
    'keccak': C.PASSACRE_KECCAK,
    'skein': C.PASSACRE_SKEIN,
}

if sys.version_info < (3, ):  # pragma: nocover

    def int_of_bytes(b):
        ret = 0
        for c in b:
            ret = (ret << 8) | ord(c)
コード例 #25
0
ファイル: __init__.py プロジェクト: sunzhaoping/python-ikcp
//int ikcp_rcvbuf_count(const ikcpcb *kcp);
//int ikcp_sndbuf_count(const ikcpcb *kcp);

void ikcp_log(ikcpcb *kcp, int mask, const char *fmt, ...);

// setup allocator
void ikcp_allocator(void* (*new_malloc)(size_t), void (*new_free)(void*));

// read conv
IUINT32 ikcp_getconv(const void *ptr);
"""
SOURCE = """
#include <ikcp.c>
"""
ffi.cdef(CDEF)
ffi.verifier = Verifier(ffi,SOURCE , include_dirs=[include_dir], modulename=_create_modulename(CDEF, SOURCE, sys.version))
ffi.verifier.compile_module = _compile_module
ffi.verifier._compile_module = _compile_module

ikcp_impl = LazyLibrary(ffi)

@ffi.callback("int(const char *, int , ikcpcb*, void *)")
def ikcp_output(cdata, size, kcp, user_handle):
    buffer = ffi.buffer(cdata = cdata, size = size)
    kcp = ffi.from_handle(user_handle)
    return kcp.output(buffer)

DEFAULT_MODE = 0
NORMAL_MODE = 1
FAST_MODE = 2
コード例 #26
0
os.chdir(_FILE_PATH)
os.chdir(os.pardir)

#use the absolute path to load the file here so we don't have to worry about working directory issues
_CDEF = open("{}/python4D.h".format(_FILE_PATH)).read()

ffi.cdef(_CDEF)

_SOURCE = """
#include "fourd.h"
"""

source_files = glob.glob('lib4d_sql/*.c')

ffi.verifier = Verifier(ffi, _SOURCE,
                        modulename=_create_modulename(_CDEF, _SOURCE, sys.version),
                        sources=source_files,
                        include_dirs=['lib4d_sql', 'python4D/lib4d_sql'])

#ffi.verifier.compile_module = _compile_module
#ffi.verifier._compile_module = _compile_module

lib4d_sql = LazyLoadLib(ffi)
os.chdir(_CWD)

########################################################################


########################################################################
## Error Classes
########################################################################
class Warning(Exception):
コード例 #27
0
    #include <librtmp/rtmp.h>
    #include <librtmp/log.h>

    /* The librtmp ABI is somewhat lacking. These functions
       help a bit. */

    void RTMP_SetSWFHash(RTMP *r, const char *swfhash,
                         uint32_t swfsize) {

        if (swfhash != NULL && swfsize > 0) {
            memcpy(r->Link.SWFHash, swfhash,
                   sizeof(r->Link.SWFHash));
            r->Link.SWFSize = swfsize;
        } else {
            r->Link.SWFSize = 0;
        }
    }

    int RTMP_GetInvokeCount(RTMP *r) {
        return r->m_numInvokes;
    }

    void RTMP_SetInvokeCount(RTMP *r, int count) {
        r->m_numInvokes = count;
    }

"""

verifier = Verifier(ffi, preamble, libraries=["rtmp"],
                    ext_package="librtmp_ffi", modulename="_binding")
コード例 #28
0
    def __getattr__(self, name):
        self.load()
        return getattr(self._lib, name)

    def load(self):
        if self._lib is None:
            with self._lock:
                if self._lib is None:
                    self._lib = self._ffi.verifier.load_library()


CDEF = pkgutil.get_data('posix_spawn', 'c/cdef.h').decode('ascii')
SOURCE = pkgutil.get_data('posix_spawn', 'c/source.c').decode('ascii')

ffi = FFI()
ffi.cdef(CDEF)
ffi.verifier = Verifier(
    ffi,
    SOURCE,
    modulename=_create_modulename(CDEF, SOURCE, sys.version),
)

# Patch the Verifier() instance to prevent CFFI from compiling the module
ffi.verifier.compile_module = _compile_module
ffi.verifier._compile_module = _compile_module

lib = LazyLibrary(ffi)

__all__ = ('lib', 'ffi')
コード例 #29
0
ファイル: __init__.py プロジェクト: idfumg/http11
# attempting to load the library, which would trigger a compile normally if it
# can't be loaded, which we want to delay so it doesn't happen on import. This
# will enable us to import this module, and use it in our setup.py to get the
# Extension object to allow distutils to build it normally.
ffi.verifier = Verifier(
    ffi,
    SOURCE,

    # This needs to match the value in setup.py
    ext_package="http11",

    # Fix the fact that CFFI doesn't sanely work when you don't have the exact
    # version installed that a library was built against.
    modulename=create_modulename(CDEF, SOURCE, sys.version),

    # We want to compile the http_parser.c instead of trying to link against it
    # or anything like that.
    sources=[
        os.path.join(SRC_DIR, "http11.c"),
    ],

    # We need to include the bundled dir so that we can include the header
    # files located in it.
    include_dirs=[
        SRC_DIR,
    ],
)


class Library:
    def __init__(self, ffi):
コード例 #30
0
ファイル: smi.py プロジェクト: Daemonae/snimpy
    k2 = hex(binascii.crc32(key[1::2]) & 0xffffffff)
    k2 = k2.lstrip('0').rstrip('L')
    return '_{2}_cffi_{0}{1}'.format(k1, k2, prefix)


class LazyLibrary(object):
    def __init__(self, ffi):
        self._ffi = ffi
        self._lib = None
        self._lock = threading.Lock()

    @property
    def lib(self):
        if self._lib is None:
            self._lib = self._ffi.verifier.load_library()
        return self._lib

    def __getattr__(self, name):
        return getattr(self.lib, name)


ffi = FFI()
ffi.cdef(_CDEF)
ffi.verifier = Verifier(ffi,
                        _SOURCE,
                        libraries=["smi"],
                        modulename=create_modulename("Mib", _CDEF, _SOURCE))
smi = LazyLibrary(ffi)

__all__ = [ffi]
コード例 #31
0
ファイル: __init__.py プロジェクト: tinker20/tinker-django
"""

SOURCE = """
    #include "ow-crypt.h"
"""

_ffi = FFI()
_ffi.cdef(CDEF)
_ffi.verifier = Verifier(
    _ffi,
    SOURCE,
    sources=[
        str(os.path.join(_bundled_dir, "crypt_blowfish.c")),
        str(os.path.join(_bundled_dir, "crypt_gensalt.c")),
        str(os.path.join(_bundled_dir, "wrapper.c")),
        # How can we get distutils to work with a .S file?
        #   Set bcrypt/crypt_blowfish-1.3/crypt_blowfish.c#57 back to 1 if we
        #   get ASM loaded.
        # str(os.path.join(_bundled_dir, "x86.S")),
    ],
    include_dirs=[str(_bundled_dir)],
    modulename=_create_modulename(CDEF, SOURCE, sys.version),
)

# Patch the Verifier() instance to prevent CFFI from compiling the module
_ffi.verifier.compile_module = _compile_module
_ffi.verifier._compile_module = _compile_module

_bcrypt_lib = LazyLibrary(_ffi)

コード例 #32
0
    k2 = hex(binascii.crc32(key[1::2]) & 0xffffffff)
    k2 = k2.lstrip('0').rstrip('L')
    return '_gphoto2_cffi_{0}{1}'.format(k1, k2)


def _compile_module(*args, **kwargs):
    raise RuntimeError(
        "Attempted implicit compile of a cffi module. All cffi modules should "
        "be pre-compiled at installation time.")


ffi = FFI()
ffi.cdef(CDEF)
ffi.verifier = Verifier(ffi,
                        SOURCE,
                        modulename=_create_modulename(CDEF, SOURCE,
                                                      sys.version),
                        libraries=['gphoto2'])

# Patch the Verifier() instance to prevent CFFI from compiling the module
ffi.verifier.compile_module = _compile_module
ffi.verifier._compile_module = _compile_module


class _globals(object):
    def __init__(self, lib):
        self._lib = lib

    @property
    def FILE_TYPES(self):
        """ Mapping from libgphoto2 file type constants to human-readable
コード例 #33
0
            r->Link.SWFSize = 0;
        }
    }

    int RTMP_GetInvokeCount(RTMP *r) {
        return r->m_numInvokes;
    }

    void RTMP_SetInvokeCount(RTMP *r, int count) {
        r->m_numInvokes = count;
    }

    /* Logging */
    #include <stdarg.h>

    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"])
コード例 #34
0
# Build our FFI instance
ffi = FFI()

# Add all of our header files, but sort first for consistency of the
# hash that CFFI generates and uses in the .so filename (the order of
# glob() results cannot be relied on)
for header in sorted(HEADERS):
    with open(header, "r") as hfile:
        ffi.cdef(hfile.read())

# TODO: Can we use the ABI of libsodium for this instead?
ffi.verifier = Verifier(
    ffi,
    "#include <sodium.h>",

    # We need to link to the sodium library
    libraries=["sodium"],

    # Our ext_package is nacl so look for it
    ext_package="nacl._lib",
)


class Library(object):
    def __init__(self, ffi):
        self.ffi = ffi
        self._lib = None

        # This prevents the compile_module() from being called, the module
        # should have been compiled by setup.py
        def _compile_module(*args, **kwargs):
            raise RuntimeError("Cannot compile module during runtime")
コード例 #35
0
ファイル: setup.py プロジェクト: koehlma/uv
with open(os.path.join(__dir__, 'cffi_template.py'), 'rb') as cffi_template:
    uvcffi_code = cffi_template.read().decode('utf-8').format(**locals())

with open(os.path.join(__dir__, 'uvcffi', '__init__.py'), 'wb') as uvcffi_module:
    uvcffi_module.write(uvcffi_code.encode('utf-8'))


ffi = cffi.FFI()
ffi.cdef(declarations)

try:
    ffi.set_source('_uvcffi', source)
    extension = ffi.distutils_extension()
except AttributeError:
    from cffi.verifier import Verifier
    verifier = Verifier(ffi, source, modulename='_uvcffi')
    extension = verifier.get_extension()


def choose_path(paths):
    for path in paths:
        if os.path.exists(path):
            return path


def win32_find_python27():
    assert sys.platform == 'win32'
    python27 = None
    if sys.version_info[:2] == (2, 7):
        return sys.executable
    if 'PYTHON' in os.environ and os.environ['PYTHON'].endswith('.exe'):
コード例 #36
0
with open(os.path.join(__dir__, 'cffi_template.py'), 'rb') as cffi_template:
    uvcffi_code = cffi_template.read().decode('utf-8').format(**locals())

with open(os.path.join(__dir__, 'uvcffi', '__init__.py'), 'wb') as uvcffi_module:
    uvcffi_module.write(uvcffi_code.encode('utf-8'))


ffi = cffi.FFI()
ffi.cdef(declarations)

try:
    ffi.set_source('_uvcffi', source)
    extension = ffi.distutils_extension()
except AttributeError:
    from cffi.verifier import Verifier
    verifier = Verifier(ffi, source, modulename='_uvcffi')
    extension = verifier.get_extension()


def choose_path(paths):
    for path in paths:
        if os.path.exists(path):
            return path


def win32_find_python27():
    assert sys.platform == 'win32'
    python27 = None
    if sys.version_info[:2] == (2, 7):
        return sys.executable
    if 'PYTHON' in os.environ and os.environ['PYTHON'].endswith('.exe'):