def decorator(fn): def convert_numpy_array(ll_tp, arg): return ffi.cast("void *", id(arg)) lines = inspect.getsource(fn).splitlines() for i, line in enumerate(lines): if line.strip(' ').startswith('@'): continue lines[i] = line.strip(' ') break source = "\n".join(lines[i:]) name = fn.__name__ handle = ptr.basic_register(source.encode(), name.encode(), ll_tp.encode(), ll_arrays.encode()) if not handle: raise Exception("basic_register failed") ll_handle = ffi.cast(ll_tp, handle) argspec = ", ".join(["arg%d" % i for i in range(len(argtypes))]) conversions = [] for i, arg in enumerate(argtypes): if arg == 'array': conversions.append( " arg%d = ffi.cast('void *', id(arg%d))" % (i, i)) conversions = "\n".join(conversions) source = py.code.Source(""" def func(%(args)s): %(conversions)s res = ll_handle(%(args)s) if not res and ptr.last_exception: exception_repr = ffi.string(ptr.last_exception) ptr.last_exception = ffi.NULL raise JitPyException(exception_repr) return res """ % { "args": argspec, 'conversions': conversions }) namespace = { 'ffi': ffi, 'll_handle': ll_handle, 'JitPyException': JitPyException, 'ptr': ptr } execute(source, namespace) res = namespace['func'] res.__name__ = fn.__name__ return res
def decorator(fn): def convert_numpy_array(ll_tp, arg): return ffi.cast("void *", id(arg)) lines = inspect.getsource(fn).splitlines() for i, line in enumerate(lines): if line.strip(' ').startswith('@'): continue lines[i] = line.strip(' ') break source = "\n".join(lines[i:]) name = fn.__name__ handle = ptr.basic_register(source.encode(), name.encode(), ll_tp.encode(), ll_arrays.encode()) if not handle: raise Exception("basic_register failed") ll_handle = ffi.cast(ll_tp, handle) argspec = ", ".join(["arg%d" % i for i in range(len(argtypes))]) conversions = [] for i, arg in enumerate(argtypes): if arg == 'array': conversions.append(" arg%d = ffi.cast('void *', id(arg%d))" % (i, i)) conversions = "\n".join(conversions) source = py.code.Source(""" def func(%(args)s): %(conversions)s res = ll_handle(%(args)s) if not res and ptr.last_exception: exception_repr = ffi.string(ptr.last_exception) ptr.last_exception = ffi.NULL raise JitPyException(exception_repr) return res """ % {"args": argspec, 'conversions': conversions}) namespace = {'ffi': ffi, 'll_handle': ll_handle, 'JitPyException':JitPyException, 'ptr': ptr} execute(source, namespace) res = namespace['func'] res.__name__ = fn.__name__ return res
def convert_numpy_array(ll_tp, arg): return ffi.cast("void *", id(arg))