Example #1
0
    def _inlined_fn(self, node):
        ''' Return a list of nodes, representing inlined function call,
        and a node, repesenting the variable that stores result.
        '''
        is_known, fn = self._get_node_value_if_known(node.func)
        assert is_known
        fn_ast = fn_to_ast(fn).body[0]
        inliner = Inliner(self._var_count, get_locals(fn_ast))
        fn_ast = inliner.visit(fn_ast)
        self._var_count = inliner.get_var_count()

        inlined_body = []
        assert not node.kwargs and not node.starargs # TODO
        for callee_arg, fn_arg in zip(node.args, fn_ast.args.args):
            # setup mangled values before call
            # TODO - if callee_arg is "simple" - literal or name,
            # and is never assigned in inlined_body
            # then do not make an assignment, just use it in inlined_body
            inlined_body.append(ast.Assign(
                targets=[ast.Name(id=fn_arg.id, ctx=ast.Store())],
                value=callee_arg))
            is_known, value = self._get_node_value_if_known(callee_arg)
            if is_known:
                # TODO - check that mutations are detected
                self._constants[fn_arg.id] = value
        
        inlined_code = self._visit(fn_ast.body) # optimize inlined code

        if isinstance(inlined_code[-1], ast.Break): # single return
            inlined_body.extend(inlined_code[:-1])
        else: # multiple returns - wrap in "while"
            while_var = new_var_name(self)
            inlined_body.extend([
                    ast.Assign(
                        targets=[ast.Name(id=while_var, ctx=ast.Store())],
                        value=self._get_literal_node(True)),
                    ast.While(
                        test=ast.Name(id=while_var, ctx=ast.Load()),
                        body=[
                            ast.Assign(
                                targets=[ast.Name(id=while_var, ctx=ast.Store())],
                                value=self._get_literal_node(False))] + 
                            inlined_code,
                        orelse=[])
                    ])

        all_nodes = inlined_body + \
                [ast.Name(id=inliner.get_return_var(), ctx=ast.Load())]
        remove_assignments(all_nodes)

        return all_nodes[:-1], all_nodes[-1]
Example #2
0
def specialized_fn(fn, globals_, locals_, *args, **kwargs):
    ''' Return specialized version of fn, fixing given args and kwargs,
    just as functools.partial does, but specialized function should be faster
    '''
    assert isinstance(globals_, dict) and isinstance(locals_, dict)
    globals_ = dict(globals_)
    globals_.update(locals_)
    fn_ast = fn_to_ast(fn)
    specialized_tree, bindings = specialized_ast(
            fn_ast, globals_, *args, **kwargs)
    if globals_.get('PRINT_AST'): # for demo
        print ast_to_source(specialized_tree)
    globals_.update(bindings)
    return eval_ast(specialized_tree, globals_=globals_)