Exemple #1
0
    def compile_to_pyfunc(self, ufunc_ast, globals=()):
        "Compile the ufunc ast to a function"
        # Build ufunc AST module
        module = ast.Module(body=[ufunc_ast])
        functions.fix_ast_lineno(module)

        # Create Python ufunc function
        d = dict(globals)
        exec(compile(module, '<ast>', 'exec'), d, d)
        d.pop('__builtins__')
        py_ufunc = d[ufunc_ast.name]

        assert isinstance(py_ufunc, types.FunctionType), py_ufunc

        return py_ufunc
Exemple #2
0
    def compile_to_pyfunc(self, ufunc_ast, globals=()):
        "Compile the ufunc ast to a function"
        # Build ufunc AST module
        module = ast.Module(body=[ufunc_ast])
        functions.fix_ast_lineno(module)

        # Create Python ufunc function
        d = dict(globals)
        exec(compile(module, '<ast>', 'exec'), d, d)
        d.pop('__builtins__')
        py_ufunc = d[ufunc_ast.name]

        assert isinstance(py_ufunc, types.FunctionType), py_ufunc

        return py_ufunc
Exemple #3
0
    def compile_to_pyfunc(self, ufunc_ast):
        "Compile the ufunc ast to a function"
        # Build ufunc AST module
        module = ast.Module(body=[ufunc_ast])
        functions.fix_ast_lineno(module)

        # Create Python ufunc function
        d = {}
        exec compile(module, "<ast>", "exec") in d, d
        d.pop("__builtins__")
        py_ufunc = d[ufunc_ast.name]

        assert isinstance(py_ufunc, types.FunctionType), py_ufunc

        return py_ufunc
Exemple #4
0
def translate(func):
    # TODO use meta package
    wrapper = functools.wraps(func)
    caller_frame = inspect.currentframe().f_back
    tree = _get_ast(func)
    tree = ast.Module(body=tree.body)
    tree = ExpandControlFlow().visit(tree)
    fix_ast_lineno(tree)

    # prepare locals for execution
    local_dict = locals()
    local_dict.update(caller_frame.f_locals)
    local_dict.update(caller_frame.f_globals)

    try:
        compiled = compile(tree, '<string>', 'exec')
        return eval(compiled)
    except Exception, e:
        logger.debug(ast.dump(tree))
        from ArminRonacher import codegen # uses Armin Ronacher's codegen to debug
        # http://dev.pocoo.org/hg/sandbox/file/852a1248c8eb/ast/codegen.py
        logger.debug(codegen.to_source(tree))
        raise
Exemple #5
0
def translate(func):
    # TODO use meta package
    wrapper = functools.wraps(func)
    caller_frame = inspect.currentframe().f_back
    tree = _get_ast(func)
    tree = ast.Module(body=tree.body)
    tree = ExpandControlFlow().visit(tree)
    fix_ast_lineno(tree)

    # prepare locals for execution
    local_dict = locals()
    local_dict.update(caller_frame.f_locals)
    local_dict.update(caller_frame.f_globals)

    try:
        compiled = compile(tree, '<string>', 'exec')
        return eval(compiled)
    except Exception as e:
        logger.debug(ast.dump(tree))
        from ArminRonacher import codegen # uses Armin Ronacher's codegen to debug
        # http://dev.pocoo.org/hg/sandbox/file/852a1248c8eb/ast/codegen.py
        logger.debug(codegen.to_source(tree))
        raise