Exemplo n.º 1
0
 def test_constructor(self):
     ast = self.ast
     body = []
     mod = ast.Module(body)
     assert mod.body is body
     target = ast.Name("hi", ast.Store())
     expr = ast.Name("apples", ast.Load())
     otherwise = []
     fr = ast.For(target, expr, body, otherwise, lineno=0, col_offset=1)
     assert fr.target is target
     assert fr.iter is expr
     assert fr.orelse is otherwise
     assert fr.body is body
     assert fr.lineno == 0
     assert fr.col_offset == 1
     fr = ast.For(body=body,
                  target=target,
                  iter=expr,
                  col_offset=1,
                  lineno=0,
                  orelse=otherwise)
     assert fr.target is target
     assert fr.iter is expr
     assert fr.orelse is otherwise
     assert fr.body is body
     assert fr.lineno == 0
     assert fr.col_offset == 1
     exc = raises(TypeError, ast.Module, 1, 2).value
     msg = str(exc)
     assert msg == "Module constructor takes either 0 or 1 positional argument"
     ast.Module(nothing=23)
Exemplo n.º 2
0
        def parse_generator(nodes, ids):
            node = nodes[0]
            tempnode = _ast.For()
            tempnode.target = node.target
            tempnode.iter = _ast.Name(id=ids[0], ctx=_ast.Load())

            if len(nodes) == 1:
                yield_node = _ast.Expr(value=_ast.Yield(value=elt))
                body = [yield_node]
            else:
                body = [parse_generator(nodes[1:], ids[1:])]

            if len(node.ifs) == 1:
                ifnode = _ast.If(test=node.ifs[0], body=body, orelse=[])
                tempnode.body = [ifnode]

            elif len(node.ifs) > 1:
                ifnode = _ast.If(test=_ast.BoolOp(op=_ast.And(),
                                                  values=node.ifs),
                                 body=body,
                                 orelse=[])
                tempnode.body = [ifnode]

            else:
                tempnode.body = body

            tempnode.orelse = None
            return tempnode
Exemplo n.º 3
0
def build_for():
    config = menu("target", "iterable")
    target = _ast.Name(id=config["target"])
    iterable = ast.parse(config["iterable"]).body[0].value
    add_ast_node(_ast.For(
        target=target,
        iter=iterable,
        body=[],
        orelse=[],
    ))
Exemplo n.º 4
0
    def for_loop(self, loop_block):
        iter_block, _, body_else_block = split(loop_block, "GET_ITER")

        #        for_iter = body_else_block[0]
        for_iter = body_else_block.pop(0)

        assert for_iter.opname == "FOR_ITER"

        idx = find_index(
            body_else_block,
            lambda instr: instr.opname == "POP_BLOCK" and for_iter.to == instr.
            i,
        )

        assert idx is not False

        body_block = body_else_block[:idx]

        else_block = body_else_block[idx + 1:]

        jump_abs = body_block.pop()

        assert jump_abs.opname == "JUMP_ABSOLUTE" and jump_abs.to == for_iter.i

        iter_stmnt = self.decompile_block(iter_block).stmnt()

        assert len(iter_stmnt) == 1
        iter_stmnt = iter_stmnt[0]
        body_lst = self.decompile_block(body_block[:],
                                        stack_items=[None],
                                        jump_map={
                                            for_iter.i: for_iter.to
                                        }).stmnt()

        assign_ = body_lst.pop(0)
        body = body_lst

        if len(else_block) == 0:
            else_ = []
        else:
            else_ = self.decompile_block(else_block[:]).stmnt()

        assign = assign_.targets[0]
        for_ = _ast.For(
            target=assign,
            iter=iter_stmnt,
            body=body,
            orelse=else_,
            lineno=iter_stmnt.lineno,
            col_offset=0,
        )

        self.ast_stack.append(for_)