Beispiel #1
0
 def setup_class(cls):
     if sys.platform == 'win32':
         return
     from pypy.module.test_lib_pypy.cffi_tests.udir import udir
     udir.join('testownlib.c').write(SOURCE)
     subprocess.check_call(
         'gcc testownlib.c -shared -fPIC -o testownlib.so',
         cwd=str(udir), shell=True)
     cls.module = str(udir.join('testownlib.so'))
Beispiel #2
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)
     library = v.load_library()
     assert library.sin(12.3) == math.sin(12.3)
Beispiel #3
0
 def setup_class(cls):
     if sys.platform == 'win32':
         return
     from pypy.module.test_lib_pypy.cffi_tests.udir import udir
     udir.join('testownlib.c').write(SOURCE)
     subprocess.check_call(
         'gcc testownlib.c -shared -fPIC -o testownlib.so',
         cwd=str(udir),
         shell=True)
     cls.module = str(udir.join('testownlib.so'))
Beispiel #4
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)
Beispiel #5
0
def really_run_setup_and_program(dirname, venv_dir_and_paths, python_snippet):
    venv_dir, paths = venv_dir_and_paths

    def remove(dir):
        dir = str(SNIPPET_DIR.join(dirname, dir))
        shutil.rmtree(dir, ignore_errors=True)

    remove('build')
    remove('__pycache__')
    for basedir in os.listdir(str(SNIPPET_DIR.join(dirname))):
        remove(os.path.join(basedir, '__pycache__'))
    olddir = os.getcwd()
    python_f = udir.join('x.py')
    python_f.write(py.code.Source(python_snippet))
    try:
        os.chdir(str(SNIPPET_DIR.join(dirname)))
        if os.name == 'nt':
            bindir = 'Scripts'
        else:
            bindir = 'bin'
        vp = str(venv_dir.join(bindir).join('python'))
        env = os.environ.copy()
        env['PYTHONPATH'] = paths
        subprocess.check_call((vp, 'setup.py', 'clean'), env=env)
        subprocess.check_call((vp, 'setup.py', 'install'), env=env)
        subprocess.check_call((vp, str(python_f)), env=env)
    finally:
        os.chdir(olddir)
def really_run_setup_and_program(dirname, venv_dir_and_paths, python_snippet):
    venv_dir, paths = venv_dir_and_paths
    def remove(dir):
        dir = str(SNIPPET_DIR.join(dirname, dir))
        shutil.rmtree(dir, ignore_errors=True)
    remove('build')
    remove('__pycache__')
    for basedir in os.listdir(str(SNIPPET_DIR.join(dirname))):
        remove(os.path.join(basedir, '__pycache__'))
    olddir = os.getcwd()
    python_f = udir.join('x.py')
    python_f.write(py.code.Source(python_snippet))
    try:
        os.chdir(str(SNIPPET_DIR.join(dirname)))
        if os.name == 'nt':
            bindir = 'Scripts'
        else:
            bindir = 'bin'
        vp = str(venv_dir.join(bindir).join('python'))
        env = os.environ.copy()
        env['PYTHONPATH'] = paths
        subprocess.check_call((vp, 'setup.py', 'clean'), env=env)
        subprocess.check_call((vp, 'setup.py', 'install'), env=env)
        subprocess.check_call((vp, str(python_f)), env=env)
    finally:
        os.chdir(olddir)
Beispiel #7
0
def create_venv(name):
    tmpdir = udir.join(name)
    try:
        subprocess.check_call(['virtualenv', '--distribute',
                               '-p', os.path.abspath(sys.executable),
                               str(tmpdir)])
    except OSError as e:
        py.test.skip("Cannot execute virtualenv: %s" % (e,))

    site_packages = None
    for dirpath, dirnames, filenames in os.walk(str(tmpdir)):
        if os.path.basename(dirpath) == 'site-packages':
            site_packages = dirpath
            break
    if site_packages:
        try:
            from cffi import _pycparser
            modules = ('cffi', '_cffi_backend')
        except ImportError:
            modules = ('cffi', '_cffi_backend', 'pycparser')
            try:
                import ply
            except ImportError:
                pass
            else:
                modules += ('ply',)   # needed for older versions of pycparser
        for module in modules:
            target = imp.find_module(module)[1]
            os.symlink(target, os.path.join(site_packages,
                                            os.path.basename(target)))
    return tmpdir
Beispiel #8
0
def create_venv(name):
    tmpdir = udir.join(name)
    try:
        subprocess.check_call([
            'virtualenv', '--distribute', '-p',
            os.path.abspath(sys.executable),
            str(tmpdir)
        ])
    except OSError as e:
        py.test.skip("Cannot execute virtualenv: %s" % (e, ))

    site_packages = None
    for dirpath, dirnames, filenames in os.walk(str(tmpdir)):
        if os.path.basename(dirpath) == 'site-packages':
            site_packages = dirpath
            break
    if site_packages:
        try:
            from cffi import _pycparser
            modules = ('cffi', '_cffi_backend')
        except ImportError:
            modules = ('cffi', '_cffi_backend', 'pycparser')
            try:
                import ply
            except ImportError:
                pass
            else:
                modules += ('ply', )  # needed for older versions of pycparser
        for module in modules:
            target = imp.find_module(module)[1]
            os.symlink(target,
                       os.path.join(site_packages, os.path.basename(target)))
    return tmpdir
Beispiel #9
0
def really_run_setup_and_program(dirname, venv_dir_and_paths, python_snippet):
    venv_dir, paths = venv_dir_and_paths

    def remove(dir):
        dir = str(SNIPPET_DIR.join(dirname, dir))
        shutil.rmtree(dir, ignore_errors=True)

    remove("build")
    remove("__pycache__")
    for basedir in os.listdir(str(SNIPPET_DIR.join(dirname))):
        remove(os.path.join(basedir, "__pycache__"))
    olddir = os.getcwd()
    python_f = udir.join("x.py")
    python_f.write(py.code.Source(python_snippet))
    try:
        os.chdir(str(SNIPPET_DIR.join(dirname)))
        if os.name == "nt":
            bindir = "Scripts"
        else:
            bindir = "bin"
        vp = str(venv_dir.join(bindir).join("python"))
        env = os.environ.copy()
        env["PYTHONPATH"] = paths
        subprocess.check_call((vp, "setup.py", "clean"), env=env)
        subprocess.check_call((vp, "setup.py", "install"), env=env)
        subprocess.check_call((vp, str(python_f)), env=env)
    finally:
        os.chdir(olddir)
Beispiel #10
0
 def setup_method(self, meth):
     self.executable = os.path.abspath(sys.executable)
     self.rootdir = os.path.abspath(os.path.dirname(os.path.dirname(cffi.__file__)))
     self.udir = udir.join(meth.__name__)
     os.mkdir(str(self.udir))
     if meth.chdir_to_tmp:
         self.saved_cwd = os.getcwd()
         os.chdir(str(self.udir))
Beispiel #11
0
def test_invalid_dotdotdot_in_macro():
    ffi = FFI()
    ffi.cdef("#define FOO ...")
    target = udir.join('test_invalid_dotdotdot_in_macro.py')
    e = py.test.raises(VerificationError, make_py_source, ffi,
                       'test_invalid_dotdotdot_in_macro', str(target))
    assert str(e.value) == ("macro FOO: cannot use the syntax '...' in "
                            "'#define FOO ...' when using the ABI mode")
 def setup_method(self, meth):
     self.executable = os.path.abspath(sys.executable)
     self.rootdir = os.path.abspath(
         os.path.dirname(os.path.dirname(cffi.__file__)))
     self.udir = udir.join(meth.__name__)
     os.mkdir(str(self.udir))
     if meth.chdir_to_tmp:
         self.saved_cwd = os.getcwd()
         os.chdir(str(self.udir))
Beispiel #13
0
def test_no_cross_include():
    baseffi = FFI()
    baseffi.set_source('test_no_cross_include_base', "..source..")
    #
    ffi = FFI()
    ffi.include(baseffi)
    target = udir.join('test_no_cross_include.py')
    py.test.raises(VerificationError, make_py_source,
                   ffi, 'test_no_cross_include', str(target))
Beispiel #14
0
 def setup_class(cls):
     cls.module = None
     from pypy.module.test_lib_pypy.cffi_tests.udir import udir
     udir.join('testownlib.c').write(SOURCE)
     if sys.platform == 'win32':
         import os
         # did we already build it?
         if cls.Backend is CTypesBackend:
             dll_path = str(
                 udir
             ) + '\\testownlib1.dll'  # only ascii for the ctypes backend
         else:
             dll_path = str(udir) + '\\' + (u + 'testownlib\u03be.dll'
                                            )  # non-ascii char
         if os.path.exists(dll_path):
             cls.module = dll_path
             return
         # try (not too hard) to find the version used to compile this python
         # no mingw
         from distutils.msvc9compiler import get_build_version
         version = get_build_version()
         toolskey = "VS%0.f0COMNTOOLS" % version
         toolsdir = os.environ.get(toolskey, None)
         if toolsdir is None:
             return
         productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
         productdir = os.path.abspath(productdir)
         vcvarsall = os.path.join(productdir, "vcvarsall.bat")
         # 64?
         arch = 'x86'
         if sys.maxsize > 2**32:
             arch = 'amd64'
         if os.path.isfile(vcvarsall):
             cmd = '"%s" %s' % (vcvarsall, arch) + ' & cl.exe testownlib.c ' \
                     ' /LD /Fetestownlib.dll'
             subprocess.check_call(cmd, cwd=str(udir), shell=True)
             os.rename(str(udir) + '\\testownlib.dll', dll_path)
             cls.module = dll_path
     else:
         subprocess.check_call(
             'cc testownlib.c -shared -fPIC -o testownlib.so',
             cwd=str(udir),
             shell=True)
         cls.module = str(udir.join('testownlib.so'))
Beispiel #15
0
def setup_module(mod):
    SRC = """
    #include <string.h>
    #define FOOBAR (-42)
    static const int FOOBAZ = -43;
    #define BIGPOS 420000000000L
    #define BIGNEG -420000000000L
    int add42(int x) { return x + 42; }
    int add43(int x, ...) { return x; }
    int globalvar42 = 1234;
    const int globalconst42 = 4321;
    const char *const globalconsthello = "hello";
    struct foo_s;
    typedef struct bar_s { int x; signed char a[]; } bar_t;
    enum foo_e { AA, BB, CC };

    void init_test_re_python(void) { }      /* windows hack */
    void PyInit__test_re_python(void) { }   /* windows hack */
    """
    tmpdir = udir.join('test_re_python')
    tmpdir.ensure(dir=1)
    c_file = tmpdir.join('_test_re_python.c')
    c_file.write(SRC)
    ext = ffiplatform.get_extension(str(c_file),
                                    '_test_re_python',
                                    export_symbols=[
                                        'add42', 'add43', 'globalvar42',
                                        'globalconst42', 'globalconsthello'
                                    ])
    outputfilename = ffiplatform.compile(str(tmpdir), ext)
    mod.extmod = outputfilename
    mod.tmpdir = tmpdir
    #
    ffi = FFI()
    ffi.cdef("""
    #define FOOBAR -42
    static const int FOOBAZ = -43;
    #define BIGPOS 420000000000L
    #define BIGNEG -420000000000L
    int add42(int);
    int add43(int, ...);
    int globalvar42;
    const int globalconst42;
    const char *const globalconsthello = "hello";
    int no_such_function(int);
    int no_such_globalvar;
    struct foo_s;
    typedef struct bar_s { int x; signed char a[]; } bar_t;
    enum foo_e { AA, BB, CC };
    int strlen(const char *);
    """)
    ffi.set_source('re_python_pysrc', None)
    ffi.emit_python_code(str(tmpdir.join('re_python_pysrc.py')))
    mod.original_ffi = ffi
    #
    sys.path.insert(0, str(tmpdir))
Beispiel #16
0
def setup_module(mod):
    SRC = """
    #include <string.h>
    #define FOOBAR (-42)
    static const int FOOBAZ = -43;
    #define BIGPOS 420000000000L
    #define BIGNEG -420000000000L
    int add42(int x) { return x + 42; }
    int add43(int x, ...) { return x; }
    int globalvar42 = 1234;
    const int globalconst42 = 4321;
    const char *const globalconsthello = "hello";
    struct foo_s;
    typedef struct bar_s { int x; signed char a[]; } bar_t;
    enum foo_e { AA, BB, CC };

    void init_test_re_python(void) { }      /* windows hack */
    void PyInit__test_re_python(void) { }   /* windows hack */
    """
    tmpdir = udir.join('test_re_python')
    tmpdir.ensure(dir=1)
    c_file = tmpdir.join('_test_re_python.c')
    c_file.write(SRC)
    ext = ffiplatform.get_extension(
        str(c_file),
        '_test_re_python',
        export_symbols=['add42', 'add43', 'globalvar42',
                        'globalconst42', 'globalconsthello']
    )
    outputfilename = ffiplatform.compile(str(tmpdir), ext)
    mod.extmod = outputfilename
    mod.tmpdir = tmpdir
    #
    ffi = FFI()
    ffi.cdef("""
    #define FOOBAR -42
    static const int FOOBAZ = -43;
    #define BIGPOS 420000000000L
    #define BIGNEG -420000000000L
    int add42(int);
    int add43(int, ...);
    int globalvar42;
    const int globalconst42;
    const char *const globalconsthello = "hello";
    int no_such_function(int);
    int no_such_globalvar;
    struct foo_s;
    typedef struct bar_s { int x; signed char a[]; } bar_t;
    enum foo_e { AA, BB, CC };
    int strlen(const char *);
    """)
    ffi.set_source('re_python_pysrc', None)
    ffi.emit_python_code(str(tmpdir.join('re_python_pysrc.py')))
    mod.original_ffi = ffi
    #
    sys.path.insert(0, str(tmpdir))
Beispiel #17
0
def test_constant_of_value_unknown_to_the_compiler():
    extra_c_source = udir.join(
        'extra_test_constant_of_value_unknown_to_the_compiler.c')
    extra_c_source.write('const int external_foo = 42;\n')
    ffi = FFI()
    ffi.cdef("const int external_foo;")
    lib = verify(ffi, 'test_constant_of_value_unknown_to_the_compiler', """
        extern const int external_foo;
    """, sources=[str(extra_c_source)])
    assert lib.external_foo == 42
Beispiel #18
0
def test_constant_of_value_unknown_to_the_compiler():
    extra_c_source = udir.join(
        'extra_test_constant_of_value_unknown_to_the_compiler.c')
    extra_c_source.write('const int external_foo = 42;\n')
    ffi = FFI()
    ffi.cdef("const int external_foo;")
    lib = verify(ffi, 'test_constant_of_value_unknown_to_the_compiler', """
        extern const int external_foo;
    """, sources=[str(extra_c_source)])
    assert lib.external_foo == 42
Beispiel #19
0
def test_struct_included():
    baseffi = FFI()
    baseffi.cdef("struct foo_s { int x; };")
    baseffi.set_source('test_struct_included_base', None)
    #
    ffi = FFI()
    ffi.include(baseffi)
    target = udir.join('test_struct_included.py')
    make_py_source(ffi, 'test_struct_included', str(target))
    assert target.read() == r"""# auto-generated file
Beispiel #20
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)
     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
Beispiel #21
0
def test_include():
    ffi = FFI()
    ffi.cdef("#define ABC 123")
    ffi.set_source('test_include', None)
    target = udir.join('test_include.py')
    make_py_source(ffi, 'test_include', str(target))
    assert target.read() == r"""# auto-generated file
import _cffi_backend

ffi = _cffi_backend.FFI('test_include',
    _version = 0x2601,
    _types = b'',
    _globals = (b'\xFF\xFF\xFF\x1FABC',123,),
)
"""
    #
    ffi2 = FFI()
    ffi2.include(ffi)
    target2 = udir.join('test2_include.py')
    make_py_source(ffi2, 'test2_include', str(target2))
    assert target2.read() == r"""# auto-generated file
Beispiel #22
0
 def setup_class(cls):
     cls.module = None
     from pypy.module.test_lib_pypy.cffi_tests.udir import udir
     udir.join('testownlib.c').write(SOURCE)
     if sys.platform == 'win32':
         import os
         # did we already build it?
         if os.path.exists(str(udir.join('testownlib.dll'))):
             cls.module = str(udir.join('testownlib.dll'))
             return
         # try (not too hard) to find the version used to compile this python
         # no mingw
         from distutils.msvc9compiler import get_build_version
         version = get_build_version()
         toolskey = "VS%0.f0COMNTOOLS" % version
         toolsdir = os.environ.get(toolskey, None)
         if toolsdir is None:
             return
         productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
         productdir = os.path.abspath(productdir)
         vcvarsall = os.path.join(productdir, "vcvarsall.bat")
         # 64?
         arch = 'x86'
         if sys.maxsize > 2**32:
             arch = 'amd64'
         if os.path.isfile(vcvarsall):
             cmd = '"%s" %s' % (vcvarsall, arch) + ' & cl.exe testownlib.c ' \
                     ' /LD /Fetestownlib.dll'
             subprocess.check_call(cmd, cwd = str(udir), shell=True)    
             cls.module = str(udir.join('testownlib.dll'))
     else:
         subprocess.check_call(
             'cc testownlib.c -shared -fPIC -o testownlib.so',
             cwd=str(udir), shell=True)
         cls.module = str(udir.join('testownlib.so'))
Beispiel #23
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')
Beispiel #24
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
Beispiel #25
0
def create_venv(name):
    tmpdir = udir.join(name)
    try:
        subprocess.check_call(['virtualenv', '--distribute',
                               '-p', os.path.abspath(sys.executable),
                               str(tmpdir)])
    except OSError as e:
        py.test.skip("Cannot execute virtualenv: %s" % (e,))

    try:
        deepcopy = os.symlink
    except:
        import shutil, errno
        def deepcopy(src, dst):
            try:
                shutil.copytree(src, dst)
            except OSError as e:
                if e.errno in (errno.ENOTDIR, errno.EINVAL):
                    shutil.copy(src, dst)
                else:
                    print('got errno')
                    print(e.errno)
                    print('not')
                    print(errno.ENOTDIR)
                    raise

    site_packages = None
    for dirpath, dirnames, filenames in os.walk(str(tmpdir)):
        if os.path.basename(dirpath) == 'site-packages':
            site_packages = dirpath
            break
    if site_packages:
        try:
            from cffi import _pycparser
            modules = ('cffi', '_cffi_backend')
        except ImportError:
            modules = ('cffi', '_cffi_backend', 'pycparser')
            try:
                import ply
            except ImportError:
                pass
            else:
                modules += ('ply',)   # needed for older versions of pycparser
        for module in modules:
            target = imp.find_module(module)[1]
            deepcopy(target, os.path.join(site_packages,
                                            os.path.basename(target)))
    return tmpdir
Beispiel #26
0
    def test_install_and_reload_module(self, targetpackage='', ext_package=''):
        KEY = repr(self)
        if not hasattr(os, 'fork'):
            py.test.skip("test requires os.fork()")

        if targetpackage:
            udir.ensure(targetpackage, dir=1).ensure('__init__.py')
        sys.path.insert(0, str(udir))

        def make_ffi(**verifier_args):
            ffi = FFI()
            ffi.cdef("/* %s, %s, %s */" % (KEY, targetpackage, ext_package))
            ffi.cdef("double test1iarm(double x);")
            csrc = "double test1iarm(double x) { return x * 42.0; }"
            lib = ffi.verify(csrc,
                             force_generic_engine=self.generic,
                             ext_package=ext_package,
                             **verifier_args)
            return ffi, lib

        childpid = os.fork()
        if childpid == 0:
            # in the child
            ffi, lib = make_ffi()
            assert lib.test1iarm(1.5) == 63.0
            # "install" the module by moving it into udir (/targetpackage)
            if targetpackage:
                target = udir.join(targetpackage)
            else:
                target = udir
            shutil.move(ffi.verifier.modulefilename, str(target))
            os._exit(0)
        # in the parent
        _, status = os.waitpid(childpid, 0)
        if not (os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0):
            raise AssertionError  # see error above in subprocess

        from cffi import ffiplatform
        prev_compile = ffiplatform.compile
        try:
            if targetpackage == ext_package:
                ffiplatform.compile = lambda *args: dont_call_me_any_more
            # won't find it in tmpdir, but should find it correctly
            # installed in udir
            ffi, lib = make_ffi()
            assert lib.test1iarm(0.5) == 21.0
        finally:
            ffiplatform.compile = prev_compile
Beispiel #27
0
 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]'
Beispiel #28
0
 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]'
Beispiel #29
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,
                  libraries=[self.lib_m])
     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')
Beispiel #30
0
    def test_install_and_reload_module(self, targetpackage='', ext_package=''):
        KEY = repr(self)
        if not hasattr(os, 'fork'):
            py.test.skip("test requires os.fork()")

        if targetpackage:
            udir.ensure(targetpackage, dir=1).ensure('__init__.py')
        sys.path.insert(0, str(udir))

        def make_ffi(**verifier_args):
            ffi = FFI()
            ffi.cdef("/* %s, %s, %s */" % (KEY, targetpackage, ext_package))
            ffi.cdef("double test1iarm(double x);")
            csrc = "double test1iarm(double x) { return x * 42.0; }"
            lib = ffi.verify(csrc, force_generic_engine=self.generic,
                             ext_package=ext_package,
                             **verifier_args)
            return ffi, lib

        childpid = os.fork()
        if childpid == 0:
            # in the child
            ffi, lib = make_ffi()
            assert lib.test1iarm(1.5) == 63.0
            # "install" the module by moving it into udir (/targetpackage)
            if targetpackage:
                target = udir.join(targetpackage)
            else:
                target = udir
            shutil.move(ffi.verifier.modulefilename, str(target))
            os._exit(0)
        # in the parent
        _, status = os.waitpid(childpid, 0)
        if not (os.WIFEXITED(status) and os.WEXITSTATUS(status) == 0):
            raise AssertionError   # see error above in subprocess

        from cffi import ffiplatform
        prev_compile = ffiplatform.compile
        try:
            if targetpackage == ext_package:
                ffiplatform.compile = lambda *args: dont_call_me_any_more
            # won't find it in tmpdir, but should find it correctly
            # installed in udir
            ffi, lib = make_ffi()
            assert lib.test1iarm(0.5) == 21.0
        finally:
            ffiplatform.compile = prev_compile
Beispiel #31
0
def create_venv(name):
    tmpdir = udir.join(name)
    try:
        subprocess.check_call([
            'virtualenv',
            #'--never-download', <= could be added, but causes failures
            # in random cases on random machines
            '-p',
            os.path.abspath(sys.executable),
            str(tmpdir)
        ])
    except OSError as e:
        py.test.skip("Cannot execute virtualenv: %s" % (e, ))

    site_packages = None
    for dirpath, dirnames, filenames in os.walk(str(tmpdir)):
        if os.path.basename(dirpath) == 'site-packages':
            site_packages = dirpath
            break
    paths = ""
    if site_packages:
        try:
            from cffi import _pycparser
            modules = ('cffi', '_cffi_backend')
        except ImportError:
            modules = ('cffi', '_cffi_backend', 'pycparser')
            try:
                import ply
            except ImportError:
                pass
            else:
                modules += ('ply', )  # needed for older versions of pycparser
        paths = []
        for module in modules:
            target = __import__(module, None, None, [])
            if not hasattr(target, '__file__'):  # for _cffi_backend on pypy
                continue
            src = os.path.abspath(target.__file__)
            for end in ['__init__.pyc', '__init__.pyo', '__init__.py']:
                if src.lower().endswith(end):
                    src = src[:-len(end) - 1]
                    break
            paths.append(os.path.dirname(src))
        paths = os.pathsep.join(paths)
    return tmpdir, paths
Beispiel #32
0
 def test_extension_object_extra_sources(self):
     ffi = FFI()
     ffi.cdef("double test1eoes(double x);")
     extra_source = str(udir.join('extension_extra_sources.c'))
     with open(extra_source, 'w') as f:
         f.write('double test1eoes(double x) { return x * 6.0; }\n')
     csrc = '/*9%s*/' % self + '''
     double test1eoes(double x);   /* or #include "extra_sources.h" */
     '''
     lib = ffi.verify(csrc, sources=[extra_source],
                      force_generic_engine=self.generic)
     assert lib.test1eoes(7.0) == 42.0
     v = ffi.verifier
     ext = v.get_extension()
     assert 'distutils.extension.Extension' in str(ext.__class__)
     assert ext.sources == [maybe_relative_path(v.sourcefilename),
                            extra_source]
     assert ext.name == v.get_module_name()
Beispiel #33
0
def really_run_setup_and_program(dirname, venv_dir, python_snippet):
    def remove(dir):
        dir = str(SNIPPET_DIR.join(dirname, dir))
        shutil.rmtree(dir, ignore_errors=True)
    remove('build')
    remove('__pycache__')
    for basedir in os.listdir(str(SNIPPET_DIR.join(dirname))):
        remove(os.path.join(basedir, '__pycache__'))
    olddir = os.getcwd()
    python_f = udir.join('x.py')
    python_f.write(py.code.Source(python_snippet))
    try:
        os.chdir(str(SNIPPET_DIR.join(dirname)))
        vp = str(venv_dir.join('bin/python'))
        subprocess.check_call((vp, 'setup.py', 'clean'))
        subprocess.check_call((vp, 'setup.py', 'install'))
        subprocess.check_call((vp, str(python_f)))
    finally:
        os.chdir(olddir)
Beispiel #34
0
def create_venv(name):
    tmpdir = udir.join(name)
    try:
        subprocess.check_call(['virtualenv', 
            #'--never-download', <= could be added, but causes failures
            # in random cases on random machines
                               '-p', os.path.abspath(sys.executable),
                               str(tmpdir)])
    except OSError as e:
        py.test.skip("Cannot execute virtualenv: %s" % (e,))

    site_packages = None
    for dirpath, dirnames, filenames in os.walk(str(tmpdir)):
        if os.path.basename(dirpath) == 'site-packages':
            site_packages = dirpath
            break
    paths = ""
    if site_packages:
        try:
            from cffi import _pycparser
            modules = ('cffi', '_cffi_backend')
        except ImportError:
            modules = ('cffi', '_cffi_backend', 'pycparser')
            try:
                import ply
            except ImportError:
                pass
            else:
                modules += ('ply',)   # needed for older versions of pycparser
        paths = []
        for module in modules:
            target = __import__(module, None, None, [])
            if not hasattr(target, '__file__'):   # for _cffi_backend on pypy
                continue
            src = os.path.abspath(target.__file__)
            for end in ['__init__.pyc', '__init__.pyo', '__init__.py']:
                if src.lower().endswith(end):
                    src = src[:-len(end)-1]
                    break
            paths.append(os.path.dirname(src))
        paths = os.pathsep.join(paths)
    return tmpdir, paths
Beispiel #35
0
 def test_extension_object_extra_sources(self):
     ffi = FFI()
     ffi.cdef("double test1eoes(double x);")
     extra_source = str(udir.join('extension_extra_sources.c'))
     with open(extra_source, 'w') as f:
         f.write('double test1eoes(double x) { return x * 6.0; }\n')
     csrc = '/*9%s*/' % self + '''
     double test1eoes(double x);   /* or #include "extra_sources.h" */
     '''
     lib = ffi.verify(csrc,
                      sources=[extra_source],
                      force_generic_engine=self.generic)
     assert lib.test1eoes(7.0) == 42.0
     v = ffi.verifier
     ext = v.get_extension()
     assert 'distutils.extension.Extension' in str(ext.__class__)
     assert ext.sources == [
         maybe_relative_path(v.sourcefilename), extra_source
     ]
     assert ext.name == v.get_module_name()
Beispiel #36
0
def really_run_setup_and_program(dirname, venv_dir, python_snippet):
    def remove(dir):
        dir = str(SNIPPET_DIR.join(dirname, dir))
        shutil.rmtree(dir, ignore_errors=True)

    remove('build')
    remove('__pycache__')
    for basedir in os.listdir(str(SNIPPET_DIR.join(dirname))):
        remove(os.path.join(basedir, '__pycache__'))
    olddir = os.getcwd()
    python_f = udir.join('x.py')
    python_f.write(py.code.Source(python_snippet))
    try:
        os.chdir(str(SNIPPET_DIR.join(dirname)))
        vp = str(venv_dir.join('bin/python'))
        subprocess.check_call((vp, 'setup.py', 'clean'))
        subprocess.check_call((vp, 'setup.py', 'install'))
        subprocess.check_call((vp, str(python_f)))
    finally:
        os.chdir(olddir)
Beispiel #37
0
def create_venv(name):
    tmpdir = udir.join(name)
    try:
        subprocess.check_call(["virtualenv", "--distribute", "-p", os.path.abspath(sys.executable), str(tmpdir)])
    except OSError as e:
        py.test.skip("Cannot execute virtualenv: %s" % (e,))

    site_packages = None
    for dirpath, dirnames, filenames in os.walk(str(tmpdir)):
        if os.path.basename(dirpath) == "site-packages":
            site_packages = dirpath
            break
    paths = ""
    if site_packages:
        try:
            from cffi import _pycparser

            modules = ("cffi", "_cffi_backend")
        except ImportError:
            modules = ("cffi", "_cffi_backend", "pycparser")
            try:
                import ply
            except ImportError:
                pass
            else:
                modules += ("ply",)  # needed for older versions of pycparser
        paths = []
        for module in modules:
            target = __import__(module, None, None, [])
            if not hasattr(target, "__file__"):  # for _cffi_backend on pypy
                continue
            src = os.path.abspath(target.__file__)
            for end in ["__init__.pyc", "__init__.pyo", "__init__.py"]:
                if src.lower().endswith(end):
                    src = src[: -len(end) - 1]
                    break
            paths.append(os.path.dirname(src))
        paths = os.pathsep.join(paths)
    return tmpdir, paths
Beispiel #38
0
def test_module_name_in_package():
    ffi = FFI()
    ffi.cdef("int foo(int);")
    recompiler.recompile(ffi, "test_module_name_in_package.mymod",
                         "int foo(int x) { return x + 32; }",
                         tmpdir=str(udir))
    old_sys_path = sys.path[:]
    try:
        package_dir = udir.join('test_module_name_in_package')
        for name in os.listdir(str(udir)):
            assert not name.startswith('test_module_name_in_package.')
        assert os.path.isdir(str(package_dir))
        assert len(os.listdir(str(package_dir))) > 0
        assert os.path.exists(str(package_dir.join('mymod.c')))
        package_dir.join('__init__.py').write('')
        #
        sys.path.insert(0, str(udir))
        import test_module_name_in_package.mymod
        assert test_module_name_in_package.mymod.lib.foo(10) == 42
        assert test_module_name_in_package.mymod.__name__ == (
            'test_module_name_in_package.mymod')
    finally:
        sys.path[:] = old_sys_path
Beispiel #39
0
def test_module_name_in_package():
    ffi = FFI()
    ffi.cdef("int foo(int);")
    recompiler.recompile(ffi, "test_module_name_in_package.mymod",
                         "int foo(int x) { return x + 32; }",
                         tmpdir=str(udir))
    old_sys_path = sys.path[:]
    try:
        package_dir = udir.join('test_module_name_in_package')
        for name in os.listdir(str(udir)):
            assert not name.startswith('test_module_name_in_package.')
        assert os.path.isdir(str(package_dir))
        assert len(os.listdir(str(package_dir))) > 0
        assert os.path.exists(str(package_dir.join('mymod.c')))
        package_dir.join('__init__.py').write('')
        #
        sys.path.insert(0, str(udir))
        import test_module_name_in_package.mymod
        assert test_module_name_in_package.mymod.lib.foo(10) == 42
        assert test_module_name_in_package.mymod.__name__ == (
            'test_module_name_in_package.mymod')
    finally:
        sys.path[:] = old_sys_path
Beispiel #40
0
def test_typename():
    ffi = FFI()
    ffi.cdef("typedef int foobar_t;")
    target = udir.join('test_typename.py')
    make_py_source(ffi, 'test_typename', str(target))
    assert target.read() == r"""# auto-generated file
Beispiel #41
0
def test_simple():
    ffi = FFI()
    ffi.cdef("int close(int); static const int BB = 42; int somevar;")
    target = udir.join('test_simple.py')
    make_py_source(ffi, 'test_simple', str(target))
    assert target.read() == r"""# auto-generated file
Beispiel #42
0
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
Beispiel #43
0
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
Beispiel #44
0
def test_global_var():
    ffi = FFI()
    ffi.cdef("int myglob;")
    target = udir.join('test_global_var.py')
    make_py_source(ffi, 'test_global_var', str(target))
    assert target.read() == r"""# auto-generated file
Beispiel #45
0
def test_bitfield():
    ffi = FFI()
    ffi.cdef("struct foo_s { int y:10; short x:5; };")
    target = udir.join('test_bitfield.py')
    make_py_source(ffi, 'test_bitfield', str(target))
    assert target.read() == r"""# auto-generated file
Beispiel #46
0
def test_global_constant():
    ffi = FFI()
    ffi.cdef("static const long BB; static const float BF = 12;")
    target = udir.join('test_valid_global_constant.py')
    make_py_source(ffi, 'test_valid_global_constant', str(target))
    assert target.read() == r"""# auto-generated file
Beispiel #47
0
def setup_module(mod):
    SRC = """
    #include <string.h>
    #define FOOBAR (-42)
    static const int FOOBAZ = -43;
    #define BIGPOS 420000000000L
    #define BIGNEG -420000000000L
    int add42(int x) { return x + 42; }
    int add43(int x, ...) { return x; }
    int globalvar42 = 1234;
    const int globalconst42 = 4321;
    const char *const globalconsthello = "hello";
    struct foo_s;
    typedef struct bar_s { int x; signed char a[]; } bar_t;
    enum foo_e { AA, BB, CC };

    void init_test_re_python(void) { }      /* windows hack */
    void PyInit__test_re_python(void) { }   /* windows hack */
    """
    tmpdir = udir.join('test_re_python')
    tmpdir.ensure(dir=1)
    c_file = tmpdir.join('_test_re_python.c')
    c_file.write(SRC)
    ext = ffiplatform.get_extension(str(c_file),
                                    '_test_re_python',
                                    export_symbols=[
                                        'add42', 'add43', 'globalvar42',
                                        'globalconst42', 'globalconsthello'
                                    ])
    outputfilename = ffiplatform.compile(str(tmpdir), ext)
    if sys.platform == "win32":
        # test with a non-ascii char
        outputfn1 = outputfilename
        ofn, oext = os.path.splitext(outputfn1)
        outputfilename = ofn + (u + '\u03be') + oext
        #print(repr(outputfn1) + ' ==> ' + repr(outputfilename))
        os.rename(outputfn1, outputfilename)
    mod.extmod = outputfilename
    mod.tmpdir = tmpdir
    #
    ffi = FFI()
    ffi.cdef("""
    #define FOOBAR -42
    static const int FOOBAZ = -43;
    #define BIGPOS 420000000000L
    #define BIGNEG -420000000000L
    int add42(int);
    int add43(int, ...);
    int globalvar42;
    const int globalconst42;
    const char *const globalconsthello = "hello";
    int no_such_function(int);
    int no_such_globalvar;
    struct foo_s;
    typedef struct bar_s { int x; signed char a[]; } bar_t;
    enum foo_e { AA, BB, CC };
    int strlen(const char *);
    struct with_union { union { int a; char b; }; };
    union with_struct { struct { int a; char b; }; };
    struct NVGcolor { union { float rgba[4]; struct { float r,g,b,a; }; }; };
    """)
    ffi.set_source('re_python_pysrc', None)
    ffi.emit_python_code(str(tmpdir.join('re_python_pysrc.py')))
    mod.original_ffi = ffi
    #
    sys.path.insert(0, str(tmpdir))
Beispiel #48
0
 def setup_method(self, meth):
     check_lib_python_found(str(udir.ensure('embedding', dir=1)))
     self._path = udir.join('embedding', meth.__name__)
     if sys.platform == "win32" or sys.platform == "darwin":
         self._compiled_modules.clear()   # workaround
Beispiel #49
0
 def setup_method(self, meth):
     check_lib_python_found(str(udir.ensure('embedding', dir=1)))
     self._path = udir.join('embedding', meth.__name__)
     if sys.platform == "win32" or sys.platform == "darwin":
         self._compiled_modules.clear()  # workaround