Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pythran-based dummy JIT compiler
import pythran
import inspect
import hashlib
import itertools
import imp
import re
def typename(obj):
# FIXME: only works for some types
return type(obj).__name__
class jit(object):
def __init__(self, **flags):
self.flags = flags
def __call__(self, fun):
# FIXME: gather global dependencies using pythran.analysis.imported_ids
src = inspect.getsource(fun)
src = re.sub("@.*?\sdef\s","def ", src)
fname = fun.__name__
m = hashlib.md5()
m.update(src)
def type_collector(*args, **kwargs):
arg_types = [typename(arg) for arg in itertools.chain(args, kwargs.values())]
m.update("".join(arg_types))
header = "#pythran export {}({})\n".format(fname, ", ".join(arg_types))
# FIXME: implement a cache here
module_name = "pythranized_" + m.hexdigest()
# FIXME: force output in tmp dir
module_path = pythran.compile_pythrancode(module_name, header + src, **self.flags)
module = imp.load_dynamic(module_name, module_path)
return getattr(module, fun.__name__)(*args, **kwargs)
return type_collector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.