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))
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))
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))
def setup_class(cls): try: from cffi import FFI # <== the system one, which from cffi import recompiler # needs to be at least cffi 1.0.0 from cffi import ffiplatform except ImportError: py.test.skip("system cffi module not found or older than 1.0.0") space = cls.space SRC = """ #define FOOBAR (-42) static const int FOOBAZ = -43; #define BIGPOS 420000000000L #define BIGNEG -420000000000L int add42(int x) { return x + 42; } 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', 'globalvar42', 'globalconst42', 'globalconsthello' ]) outputfilename = ffiplatform.compile(str(tmpdir), ext) cls.w_extmod = space.wrap(outputfilename) if WIN32: unicode_name = u'load\u03betest.dll' else: unicode_name = u'load_caf\xe9' + os.path.splitext( outputfilename)[1] try: unicode_name.encode(sys.getfilesystemencoding()) except UnicodeEncodeError: unicode_name = None # skip test_dlopen_unicode if unicode_name is not None: outputfileUname = os.path.join(unicode(udir), unicode_name) shutil.copyfile(outputfilename, outputfileUname) cls.w_extmodU = space.wrap(outputfileUname) #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 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 }; """) ffi.set_source('re_python_pysrc', None) ffi.emit_python_code(str(tmpdir.join('re_python_pysrc.py'))) # sub_ffi = FFI() sub_ffi.cdef("static const int k2 = 121212;") sub_ffi.include(ffi) assert 'macro FOOBAR' in ffi._parser._declarations assert 'macro FOOBAZ' in ffi._parser._declarations sub_ffi.set_source('re_py_subsrc', None) sub_ffi.emit_python_code(str(tmpdir.join('re_py_subsrc.py'))) # cls.w_fix_path = space.appexec([space.wrap(str(tmpdir))], """(path): def fix_path(ignored=None): import _cffi_backend # force it to be initialized import sys if path not in sys.path: sys.path.insert(0, path) return fix_path """)
def setup_class(cls): try: from cffi import FFI # <== the system one, which from cffi import recompiler # needs to be at least cffi 1.0.0 from cffi import ffiplatform except ImportError: py.test.skip("system cffi module not found or older than 1.0.0") space = cls.space SRC = """ #define FOOBAR (-42) static const int FOOBAZ = -43; #define BIGPOS 420000000000L #define BIGNEG -420000000000L int add42(int x) { return x + 42; } 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', 'globalvar42', 'globalconst42', 'globalconsthello']) outputfilename = ffiplatform.compile(str(tmpdir), ext) cls.w_extmod = space.wrap(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 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 }; """) ffi.set_source('re_python_pysrc', None) ffi.emit_python_code(str(tmpdir.join('re_python_pysrc.py'))) # sub_ffi = FFI() sub_ffi.cdef("static const int k2 = 121212;") sub_ffi.include(ffi) assert 'macro FOOBAR' in ffi._parser._declarations assert 'macro FOOBAZ' in ffi._parser._declarations sub_ffi.set_source('re_py_subsrc', None) sub_ffi.emit_python_code(str(tmpdir.join('re_py_subsrc.py'))) # space.appexec([space.wrap(str(tmpdir))], """(path): import _cffi_backend # force it to be initialized import sys sys.path.insert(0, path) """)
def prepare(space, cdef, module_name, source, w_includes=None, w_extra_source=None): try: import cffi from cffi import FFI # <== the system one, which from cffi import recompiler # needs to be at least cffi 1.0.4 from cffi import ffiplatform except ImportError: py.test.skip("system cffi module not found or older than 1.0.0") if cffi.__version_info__ < (1, 2, 0): py.test.skip("system cffi module needs to be at least 1.2.0") space.appexec([], """(): import _cffi_backend # force it to be initialized """) includes = [] if w_includes: includes += space.unpackiterable(w_includes) assert module_name.startswith('test_') module_name = '_CFFI_' + module_name rdir = udir.ensure('recompiler', dir=1) rdir.join('Python.h').write( '#define PYPY_VERSION XX\n' '#define PyMODINIT_FUNC /*exported*/ void\n' ) path = module_name.replace('.', os.sep) if '.' in module_name: subrdir = rdir.join(module_name[:module_name.index('.')]) os.mkdir(str(subrdir)) else: subrdir = rdir c_file = str(rdir.join('%s.c' % path)) ffi = FFI() for include_ffi_object in includes: ffi.include(include_ffi_object._test_recompiler_source_ffi) ffi.cdef(cdef) ffi.set_source(module_name, source) ffi.emit_c_code(c_file) base_module_name = module_name.split('.')[-1] sources = [] if w_extra_source is not None: sources.append(space.str_w(w_extra_source)) ext = ffiplatform.get_extension(c_file, module_name, include_dirs=[str(rdir)], export_symbols=['_cffi_pypyinit_' + base_module_name], sources=sources) ffiplatform.compile(str(rdir), ext) for extension in ['so', 'pyd', 'dylib']: so_file = str(rdir.join('%s.%s' % (path, extension))) if os.path.exists(so_file): break else: raise Exception("could not find the compiled extension module?") args_w = [space.wrap(module_name), space.wrap(so_file)] w_res = space.appexec(args_w, """(modulename, filename): import imp mod = imp.load_dynamic(modulename, filename) assert mod.__name__ == modulename return (mod.ffi, mod.lib) """) ffiobject = space.getitem(w_res, space.wrap(0)) ffiobject._test_recompiler_source_ffi = ffi if not hasattr(space, '_cleanup_ffi'): space._cleanup_ffi = [] space._cleanup_ffi.append(ffiobject) return w_res
def prepare(space, cdef, module_name, source, w_includes=None, w_extra_source=None): try: import cffi from cffi import FFI # <== the system one, which from cffi import recompiler # needs to be at least cffi 1.0.4 from cffi import ffiplatform except ImportError: py.test.skip("system cffi module not found or older than 1.0.0") if cffi.__version_info__ < (1, 2, 0): py.test.skip("system cffi module needs to be at least 1.2.0") space.appexec([], """(): import _cffi_backend # force it to be initialized """) includes = [] if w_includes: includes += space.unpackiterable(w_includes) assert module_name.startswith('test_') module_name = '_CFFI_' + module_name rdir = udir.ensure('recompiler', dir=1) rdir.join('Python.h').write('#define PYPY_VERSION XX\n' '#define PyMODINIT_FUNC /*exported*/ void\n') path = module_name.replace('.', os.sep) if '.' in module_name: subrdir = rdir.join(module_name[:module_name.index('.')]) os.mkdir(str(subrdir)) else: subrdir = rdir c_file = str(rdir.join('%s.c' % path)) ffi = FFI() for include_ffi_object in includes: ffi.include(include_ffi_object._test_recompiler_source_ffi) ffi.cdef(cdef) ffi.set_source(module_name, source) ffi.emit_c_code(c_file) base_module_name = module_name.split('.')[-1] sources = [] if w_extra_source is not None: sources.append(space.str_w(w_extra_source)) ext = ffiplatform.get_extension( c_file, module_name, include_dirs=[str(rdir)], export_symbols=['_cffi_pypyinit_' + base_module_name], sources=sources) ffiplatform.compile(str(rdir), ext) for extension in ['so', 'pyd', 'dylib']: so_file = str(rdir.join('%s.%s' % (path, extension))) if os.path.exists(so_file): break else: raise Exception("could not find the compiled extension module?") args_w = [space.wrap(module_name), space.wrap(so_file)] w_res = space.appexec( args_w, """(modulename, filename): import imp mod = imp.load_dynamic(modulename, filename) assert mod.__name__ == modulename return (mod.ffi, mod.lib) """) ffiobject = space.getitem(w_res, space.wrap(0)) ffiobject._test_recompiler_source_ffi = ffi if not hasattr(space, '_cleanup_ffi'): space._cleanup_ffi = [] space._cleanup_ffi.append(ffiobject) return w_res