Use meta to decompile python functions into ASTs
This commit is contained in:
parent
8a159b35f0
commit
b0137289b9
2 changed files with 11 additions and 19 deletions
|
|
@ -364,8 +364,12 @@ class CBuilder(object):
|
||||||
self.builder.ret(val.value)
|
self.builder.ret(val.value)
|
||||||
else:
|
else:
|
||||||
if retty != types.void:
|
if retty != types.void:
|
||||||
errmsg = "Cannot return void"
|
# errmsg = "Cannot return void"
|
||||||
raise TypeError(errmsg)
|
# raise TypeError(errmsg, retty)
|
||||||
|
bb = self.function.append_basic_block('unreachable_bb')
|
||||||
|
self.builder.position_at_end(bb)
|
||||||
|
self.builder.ret(self.builder.alloca(retty, name='undef'))
|
||||||
|
else:
|
||||||
self.builder.ret_void()
|
self.builder.ret_void()
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
# A handy translator that converts control flow into the appropriate
|
# A handy translator that converts control flow into the appropriate
|
||||||
# llvm_cbuilder constructs
|
# llvm_cbuilder constructs
|
||||||
|
from numba.functions import _get_ast
|
||||||
import inspect, functools, ast
|
import inspect, functools, ast
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
@ -8,22 +9,9 @@ logger = logging.getLogger(__name__)
|
||||||
def translate(func):
|
def translate(func):
|
||||||
# TODO use meta package
|
# TODO use meta package
|
||||||
wrapper = functools.wraps(func)
|
wrapper = functools.wraps(func)
|
||||||
source = inspect.getsource(func)
|
|
||||||
lines = list(source.splitlines())
|
|
||||||
|
|
||||||
# skip first two lines
|
|
||||||
# assume they are
|
|
||||||
# @translate
|
|
||||||
# def blahblah():
|
|
||||||
assert lines[0].lstrip().startswith('@')
|
|
||||||
assert lines[1].lstrip().startswith('def')
|
|
||||||
first_line_len = len(lines[2])
|
|
||||||
indent = first_line_len - len(lines[2].lstrip())
|
|
||||||
source = '\n'.join(line[indent:] for line in lines[2:])
|
|
||||||
|
|
||||||
caller_frame = inspect.currentframe().f_back
|
caller_frame = inspect.currentframe().f_back
|
||||||
|
tree = _get_ast(func)
|
||||||
tree = ast.parse(source)
|
tree = ast.copy_location(ast.Module(body=tree.body), tree)
|
||||||
tree = ExpandControlFlow().visit(tree)
|
tree = ExpandControlFlow().visit(tree)
|
||||||
tree = ast.fix_missing_locations(tree)
|
tree = ast.fix_missing_locations(tree)
|
||||||
|
|
||||||
|
|
@ -34,7 +22,7 @@ def translate(func):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return eval(compile(tree, '<string>', 'exec'))
|
return eval(compile(tree, '<string>', 'exec'))
|
||||||
except:
|
except Exception, e:
|
||||||
logger.debug(ast.dump(tree))
|
logger.debug(ast.dump(tree))
|
||||||
from ArminRonacher import codegen # uses Armin Ronacher's codegen to debug
|
from ArminRonacher import codegen # uses Armin Ronacher's codegen to debug
|
||||||
# http://dev.pocoo.org/hg/sandbox/file/852a1248c8eb/ast/codegen.py
|
# http://dev.pocoo.org/hg/sandbox/file/852a1248c8eb/ast/codegen.py
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue