def cache_file_collision_tester(q, tempdir, modname_bar1, modname_bar2): sys.path.insert(0, tempdir) bar1 = import_dynamic(modname_bar1).bar bar2 = import_dynamic(modname_bar2).bar with capture_cache_log() as buf: r1 = bar1() q.put(buf.getvalue()) q.put(r1) with capture_cache_log() as buf: r2 = bar2() q.put(buf.getvalue()) q.put(r2)
def check_c_ext(self, extdir, name): sys.path.append(extdir) try: lib = import_dynamic(name) yield lib finally: sys.path.remove(extdir) sys.modules.pop(name, None)
def compile_cffi_module(self, name, source, cdef): from cffi import FFI ffi = FFI() ffi.set_source(name, source, include_dirs=[include_path()]) ffi.cdef(cdef) tmpdir = temp_directory("cffi_test_{}".format(name)) ffi.compile(tmpdir=tmpdir) sys.path.append(tmpdir) try: mod = import_dynamic(name) finally: sys.path.remove(tmpdir) return ffi, mod
def import_module(self): # Import a fresh version of the test module. All jitted functions # in the test module will start anew and load overloads from # the on-disk cache if possible. old = sys.modules.pop(self.modname, None) if old is not None: # Make sure cached bytecode is removed cached = [old.__cached__] for fn in cached: try: os.unlink(fn) except FileNotFoundError: pass mod = import_dynamic(self.modname) self.assertEqual(mod.__file__.rstrip('co'), self.modfile) return mod
def load_ool_module(): """ Compile an out-of-line module, return the corresponding ffi and module objects. """ from cffi import FFI numba_complex = """ typedef struct _numba_complex { double real; double imag; } numba_complex; """ defs = numba_complex + """ double sin(double x); double cos(double x); int foo(int a, int b, int c); void vsSin(int n, float* x, float* y); void vdSin(int n, double* x, double* y); void vector_real(numba_complex *c, double *real, int n); void vector_imag(numba_complex *c, double *imag, int n); """ source = numba_complex + """ static int foo(int a, int b, int c) { return a + b * c; } void vsSin(int n, float* x, float* y) { int i; for (i=0; i<n; i++) y[i] = sin(x[i]); } void vdSin(int n, double* x, double* y) { int i; for (i=0; i<n; i++) y[i] = sin(x[i]); } static void vector_real(numba_complex *c, double *real, int n) { int i; for (i = 0; i < n; i++) real[i] = c[i].real; } static void vector_imag(numba_complex *c, double *imag, int n) { int i; for (i = 0; i < n; i++) imag[i] = c[i].imag; } """ ffi = FFI() ffi.set_source('cffi_usecases_ool', source) ffi.cdef(defs, override=True) tmpdir = temp_directory('test_cffi') ffi.compile(tmpdir=tmpdir) sys.path.append(tmpdir) try: mod = import_dynamic('cffi_usecases_ool') cffi_support.register_module(mod) cffi_support.register_type(mod.ffi.typeof('struct _numba_complex'), complex128) return mod.ffi, mod finally: sys.path.remove(tmpdir)
def import_bar2(self): return import_dynamic(self.modname_bar2).bar
def load_ool_module(): """ Compile an out-of-line module, return the corresponding ffi and module objects. """ from cffi import FFI numba_complex = """ typedef struct _numba_complex { double real; double imag; } numba_complex; """ bool_define = """ #ifdef _MSC_VER #define false 0 #define true 1 #define bool int #else #include <stdbool.h> #endif """ defs = (numba_complex + """ bool boolean(void); double sin(double x); double cos(double x); int foo(int a, int b, int c); void vsSin(int n, float* x, float* y); void vdSin(int n, double* x, double* y); void vector_real(numba_complex *c, double *real, int n); void vector_imag(numba_complex *c, double *imag, int n); """) source = (numba_complex + bool_define + """ static bool boolean(void) { return true; } static int foo(int a, int b, int c) { return a + b * c; } void vsSin(int n, float* x, float* y) { int i; for (i=0; i<n; i++) y[i] = sin(x[i]); } void vdSin(int n, double* x, double* y) { int i; for (i=0; i<n; i++) y[i] = sin(x[i]); } static void vector_real(numba_complex *c, double *real, int n) { int i; for (i = 0; i < n; i++) real[i] = c[i].real; } static void vector_imag(numba_complex *c, double *imag, int n) { int i; for (i = 0; i < n; i++) imag[i] = c[i].imag; } """) ffi = FFI() ffi.set_source("cffi_usecases_ool", source) ffi.cdef(defs, override=True) tmpdir = temp_directory("test_cffi") ffi.compile(tmpdir=tmpdir) sys.path.append(tmpdir) try: mod = import_dynamic("cffi_usecases_ool") cffi_support.register_module(mod) cffi_support.register_type(mod.ffi.typeof("struct _numba_complex"), complex128) return mod.ffi, mod finally: sys.path.remove(tmpdir)