def make_list_comp(self, get_iter, for_iter):

        block = self.make_block(for_iter.to, inclusive=False, raise_=False)

        jump_abs = block.pop()

        assert jump_abs.opname == 'JUMP_ABSOLUTE', jump_abs.opname
        jump_map = {for_iter.i:for_iter.to}

        stmnts = self.decompile_block(block, stack_items=[None], jump_map=jump_map).stmnt()

        if len(stmnts) > 1:

            assign = stmnts.pop(0)
    
            assert len(stmnts) == 1
    
            assert isinstance(assign, _ast.Assign)
    
    
            list_expr = self.pop_ast_item()
    
            # empty ast.List object
            list_ = self.pop_ast_item()
    
            ifs = []
            elt = refactor_ifs(stmnts[0], ifs)
    
            assert len(assign.targets) == 1
            generators = [_ast.comprehension(target=assign.targets[0], iter=list_expr, ifs=ifs, lineno=get_iter.lineno, col_offset=0)]
    
            if isinstance(list_, _ast.Assign):
                
                comp = _ast.comprehension(target=list_.targets[0], iter=None, ifs=ifs, lineno=get_iter.lineno, col_offset=0)
                generators.insert(0, comp)
            
            list_comp = _ast.ListComp(elt=elt, generators=generators, lineno=get_iter.lineno, col_offset=0)
        else:
            list_expr = self.pop_ast_item()
            list_comp = stmnts[0]
            
            generators = list_comp.generators
            
            # empty ast.List object
            list_ = self.pop_ast_item()
            if not isinstance(list_, _ast.Assign):
                comp = _ast.comprehension(target=list_.targets[0], iter=None, ifs=[], lineno=get_iter.lineno, col_offset=0)
                generators.insert(0, comp)
                
            generators[0].iter = list_expr

        self.push_ast_item(list_comp)
    def make_list_comp(self, get_iter, for_iter):

        block = self.make_block(for_iter.to, inclusive=False, raise_=False)

        jump_abs = block.pop()

        assert jump_abs.opname == 'JUMP_ABSOLUTE', jump_abs.opname
        jump_map = {for_iter.i:for_iter.to}

        stmnts = self.decompile_block(block, stack_items=[None], jump_map=jump_map).stmnt()

        if len(stmnts) > 1:

            assign = stmnts.pop(0)
    
            assert len(stmnts) == 1
    
            assert isinstance(assign, _ast.Assign)
    
            list_expr = self.ast_stack.pop()
    
            # empty ast.List object
            list_ = self.ast_stack.pop()
    
            ifs = []
            elt = refactor_ifs(stmnts[0], ifs)
    
            assert len(assign.targets) == 1
            generators = [_ast.comprehension(target=assign.targets[0], iter=list_expr, ifs=ifs, lineno=get_iter.lineno, col_offset=0)]
    
            if isinstance(list_, _ast.Assign):
                
                comp = _ast.comprehension(target=list_.targets[0], iter=None, ifs=ifs, lineno=get_iter.lineno, col_offset=0)
                generators.insert(0, comp)
            
            list_comp = _ast.ListComp(elt=elt, generators=generators, lineno=get_iter.lineno, col_offset=0)
        else:
            list_expr = self.ast_stack.pop()
            list_comp = stmnts[0]
            
            generators = list_comp.generators
            
            # empty ast.List object
            list_ = self.ast_stack.pop()
            if not isinstance(list_, _ast.Assign):
                comp = _ast.comprehension(target=list_.targets[0], iter=None, ifs=[], lineno=get_iter.lineno, col_offset=0)
                generators.insert(0, comp)
                
            generators[0].iter = list_expr

        self.ast_stack.append(list_comp)
Example #3
0
def comprehension(for_part, in_part, *ifs):
    """Create an _ast.comprehension node, used in _ast.ListComprehension.

  Args:
    for_part: The part after "for "
    in_part: The part after "for [for_part] in "
    *ifs: {_ast.Compare}

  Returns:
    {_ast.comprehension}
  """
    for_part = _WrapWithName(for_part, ctx_type=CtxEnum.STORE)
    in_part = _WrapWithName(in_part, ctx_type=CtxEnum.LOAD)
    return _ast.comprehension(target=for_part, iter=in_part, ifs=list(ifs))
Example #4
0
 def test_bug_null_in_objspace_type(self):
     import ast
     code = ast.Expression(
         lineno=1,
         col_offset=1,
         body=ast.ListComp(
             lineno=1,
             col_offset=1,
             elt=ast.Call(lineno=1,
                          col_offset=1,
                          func=ast.Name(lineno=1,
                                        col_offset=1,
                                        id='str',
                                        ctx=ast.Load(lineno=1,
                                                     col_offset=1)),
                          args=[
                              ast.Name(lineno=1,
                                       col_offset=1,
                                       id='x',
                                       ctx=ast.Load(lineno=1, col_offset=1))
                          ],
                          keywords=[]),
             generators=[
                 ast.comprehension(
                     lineno=1,
                     col_offset=1,
                     target=ast.Name(lineno=1,
                                     col_offset=1,
                                     id='x',
                                     ctx=ast.Store(lineno=1, col_offset=1)),
                     iter=ast.List(
                         lineno=1,
                         col_offset=1,
                         elts=[ast.Num(lineno=1, col_offset=1, n=23)],
                         ctx=ast.Load(
                             lineno=1,
                             col_offset=1,
                         )),
                     ifs=[])
             ]))
     compile(code, '<template>', 'eval')
Example #5
0
 def test_bug_null_in_objspace_type(self):
     import ast
     code = ast.Expression(lineno=1, col_offset=1, body=ast.ListComp(lineno=1, col_offset=1, elt=ast.Call(lineno=1, col_offset=1, func=ast.Name(lineno=1, col_offset=1, id='str', ctx=ast.Load(lineno=1, col_offset=1)), args=[ast.Name(lineno=1, col_offset=1, id='x', ctx=ast.Load(lineno=1, col_offset=1))], keywords=[]), generators=[ast.comprehension(lineno=1, col_offset=1, target=ast.Name(lineno=1, col_offset=1, id='x', ctx=ast.Store(lineno=1, col_offset=1)), iter=ast.List(lineno=1, col_offset=1, elts=[ast.Num(lineno=1, col_offset=1, n=23)], ctx=ast.Load(lineno=1, col_offset=1, )), ifs=[])]))
     compile(code, '<template>', 'eval')