Skip to content

Commit

Permalink
Fixing python 3 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
fcofdez committed Feb 26, 2015
1 parent 3bde03e commit 501a18d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
11 changes: 5 additions & 6 deletions jitpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import py
import cffi
import os
Expand Down Expand Up @@ -27,7 +26,7 @@ def setup(pypy_home=None):
except KeyError:
raise Exception('Neither PYPY_HOME specified nor passed to setup'
' as an argument')

ffi = cffi.FFI()
ffi.cdef("""
void rpython_startup_code(void);
Expand Down Expand Up @@ -62,7 +61,7 @@ def setup(pypy_home=None):
curdir = os.path.dirname(os.path.abspath(__file__))
ffi.cdef(pypy_defs)
lib.rpython_startup_code()
res = lib.pypy_setup_home(os.path.join(libdir, 'pypy-c'), 1)
res = lib.pypy_setup_home(os.path.join(libdir, 'pypy-c').encode(), 1)
pypy_side = os.path.join(curdir, 'pypy_side.py')
if res == 1:
raise Exception("cannot init pypy")
Expand All @@ -77,7 +76,7 @@ def setup(pypy_home=None):
traceback.print_tb(sys.exc_info()[2])
print "%%s:%%s" %% (e.__class__.__name__, e)
raise
""" % (curdir, pypy_side))), ptr)
""" % (curdir, pypy_side))).encode(), ptr)
if res:
raise Exception("error running pypy side")

Expand All @@ -93,7 +92,7 @@ def setup(pypy_home=None):
int type_num;
...;
} PyArray_Descr;
typedef struct {
char *data;
int nd;
Expand Down Expand Up @@ -126,4 +125,4 @@ def setup(pypy_home=None):
def extra_source(source):
if not ptr:
raise Exception("jitpy not initialized, call jitpy.setup(pypy_home)")
ptr.extra_source(source)
ptr.extra_source(source.encode())
14 changes: 10 additions & 4 deletions jitpy/wrapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import inspect
import py
from jitpy import ffi, ptr
Expand All @@ -12,6 +11,11 @@
'array': 'intptr_t*',
}


def execute(source, namespace):
exec(source.compile(), namespace)


def jittify(argtypes, restype=None):
""" Wrap function into a callable visible from CPython, but run on
underlaying PyPy.
Expand All @@ -33,7 +37,7 @@ def jittify(argtypes, restype=None):
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('@'):
Expand All @@ -42,7 +46,8 @@ def convert_numpy_array(ll_tp, arg):
break
source = "\n".join(lines[i:])
name = fn.__name__
handle = ptr.basic_register(source, name, ll_tp, ll_arrays)
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)
Expand All @@ -65,7 +70,8 @@ def func(%(args)s):
""" % {"args": argspec, 'conversions': conversions})
namespace = {'ffi': ffi, 'll_handle': ll_handle,
'JitPyException':JitPyException, 'ptr': ptr}
exec source.compile() in namespace

execute(source, namespace)
res = namespace['func']
res.__name__ = fn.__name__
return res
Expand Down

0 comments on commit 501a18d

Please sign in to comment.