示例#1
0
  def make_assign_opt_unpack(i, bytecode):
    store_exprs = []
    value_exprs = []
    store_state, value_state = True, False

    while i >= 0:
      op, arg = bytecode[i][2], bytecode[i][3]
      if store_state:
        if op == ROT_TWO:
          prev_op = bytecode[i - 1][2] if i > 0 else -1
          if prev_op == ROT_THREE:
            i -= 1
          value_state = True
          store_state = False
        elif op in STORE_OPCODES:
          i, store_stmt = Statement.make_expr(i, bytecode, context=_ast.Store())
          store_exprs.insert(0, store_stmt)
      elif value_state:
        i, value_stmt = Statement.make_expr(i, bytecode)
        value_exprs.insert(0, value_stmt)
      i -= 1

    store_exprs = _ast.Tuple(store_exprs, _ast.Store())
    if not isinstance(value_exprs, _ast.AST):
      value_exprs = _ast.Tuple(value_exprs, _ast.Load())

    return i, _ast.Assign([store_exprs], value_exprs)
class ConstructorMutableAttribsLineNumberFixture:
    definition_is_none = None
    definition_is_not_a_function = _ast.Pass
    constructor_with_empty_body = _ast.FunctionDef(name='__init__', body=[])
    try:
        constructor_with_immutable = _ast.FunctionDef(
            name='__init__',
            body=[
                _ast.Assign(lineno=1, value=_ast.Tuple(elts=[1, 2, 3])),
                _ast.Assign(lineno=2, value=_ast.Str(s='a')),
            ],
        )
    except (AttributeError):
        constructor_with_immutable = _ast.FunctionDef(
            name='__init__',
            body=[
                _ast.Assign(lineno=1, value=_ast.Tuple(elts=[1, 2, 3])),
                _ast.Assign(lineno=3, value=_ast.JoinedStr(values=None)),
            ],
        )
    constructor_with_mutable = _ast.FunctionDef(
        name='__init__',
        body=[
            _ast.Assign(lineno=1, value=_ast.List(elts=[1, 2, 3])),
            _ast.Assign(lineno=2, value=_ast.Set(elts=[1, 2, 3])),
            _ast.Assign(lineno=3, value=_ast.Dict(keys=['a'])),
        ],
    )
示例#3
0
  def make_assign_unpack(i, bytecode, unpack_num=-1):
    if unpack_num < 1:
      logger.error("Could not find the number of unpacked items. ")
      return i, None

    store_exprs = []
    value_exprs = []
    store_state, value_state = True, False

    while i >= 0:
      op, arg = bytecode[i][2], bytecode[i][3]
      if store_state:
        if op == UNPACK_SEQUENCE:
          store_state = False
          prev_op = bytecode[i - 1][2] if i > 0 else -1
          if prev_op == BUILD_TUPLE:
            value_state = True
          else:
            i, value_exprs = Statement.make_expr(i - 1, bytecode)
            break
        elif op in STORE_OPCODES:
          i, store_stmt = Statement.make_expr(i, bytecode, context=_ast.Store())
          store_exprs.insert(0, store_stmt)
      elif value_state:
        i, value_stmt = Statement.make_expr(i, bytecode)
        value_exprs.insert(0, value_stmt)

      i -= 1

    store_exprs = _ast.Tuple(store_exprs, _ast.Store())
    if not isinstance(value_exprs, _ast.AST):
      value_exprs = _ast.Tuple(value_exprs, _ast.Load())

    return i, _ast.Assign([store_exprs], value_exprs)
    def ROT_TWO(self, instr):

        one = self.ast_stack.pop()
        two = self.ast_stack.pop()

        if self.ilst[0].opname == 'STORE_NAME':

            kw = dict(lineno=instr.lineno, col_offset=0)
            stores = []
            while self.ilst[0].opname == 'STORE_NAME':
                stores.append(self.ilst.pop(0))

            assert len(stores) <= 3, stores
            elts_load = [one, two]
            if len(stores) == 3:
                elts_load.insert(0, self.ast_stack.pop())

            tup_load = _ast.Tuple(elts=elts_load[::-1], ctx=_ast.Load(), **kw)

            elts_store = [
                _ast.Name(id=store.arg, ctx=_ast.Store(), **kw)
                for store in stores
            ]
            tup_store = _ast.Tuple(elts=elts_store, ctx=_ast.Store(), **kw)

            assgn = _ast.Assign(value=tup_load, targets=[tup_store], **kw)
            self.ast_stack.append(assgn)
#            self.ast_stack.append(tup_store)
        else:
            self.ast_stack.append(one)
            self.ast_stack.append(two)
    def UNPACK_SEQUENCE(self, instr):
        nargs = instr.oparg

        nodes = []
        ast_tuple = _ast.Tuple(elts=nodes,
                               ctx=_ast.Store(),
                               lineno=instr.lineno,
                               col_offset=0)
        for i in range(nargs):
            nex_instr = self.ilst.pop(0)
            self.ast_stack.append(None)
            self.visit(nex_instr)

            node = self.ast_stack.pop()
            nodes.append(node.targets[0])

        expr = self.ast_stack.pop()
        if isinstance(expr, _ast.Assign):
            assgn = expr
            assgn.targets.append(ast_tuple)

            value_dup = self.ast_stack.pop()

            assert cmp_ast(assgn.value, value_dup)

        else:
            assgn = _ast.Assign(targets=[ast_tuple],
                                value=expr,
                                lineno=instr.lineno,
                                col_offset=0)
        self.ast_stack.append(assgn)
示例#6
0
def Tuple(*items, **kwargs):
    """Creates an _ast.Tuple node.

  Automatically adjusts inner ctx attrs.

  Args:
    *items: The items in the list.
    **kwargs: Only recognized kwarg is 'ctx_type', which controls the
      ctx type of the list. See CtxEnum.

  Returns:
    An _ast.Tuple node.
  """
    ctx_type = kwargs.pop('ctx_type', CtxEnum.LOAD)

    new_items = []
    for item in items:
        if isinstance(item, str):
            new_items.append(_WrapWithName(item))
        else:
            new_items.append(item)

    for item in new_items:
        if isinstance(item, _ast.Name):
            item.ctx = GetCtx(ctx_type)
        elif isinstance(item, _ast.Attribute):
            name_node = _LeftmostNodeInDotVar(item)
            name_node.ctx = GetCtx(ctx_type)
    ctx = GetCtx(ctx_type)
    return _ast.Tuple(elts=new_items, ctx=ctx)
示例#7
0
 def visit_Tuple(self, node):
     if self.randomize():
         elements = [self.visit(node.elts[0])]
         for element in node.elts[1:]:
             if self.randomize():
                 elements.append(self.visit(element))
         if self.randomize():
             for element in node.elts:
                 if self.randomize():
                     elements.append(self.visit(element))
         return ast.copy_location(_ast.Tuple(elts=elements), node)
     else:
         return node
示例#8
0
    def BUILD_TUPLE(self, instr):

        nitems = instr.oparg

        nodes = []
        list_ = _ast.Tuple(elts=nodes, ctx=_ast.Load(), lineno=instr.lineno, col_offset=0)
        for i in range(nitems):
            nodes.insert(0, self.ast_stack.pop())

        if any([item == 'CLOSURE' for item in nodes]):
            assert all([item == 'CLOSURE' for item in nodes])
            return

        self.ast_stack.append(list_)
示例#9
0
 def make_const(i, bytecode):
   arg = bytecode[i][3]
   if isinstance(arg, basestring):
     return i, _ast.Str(arg)
   elif isinstance(arg, int) or isinstance(arg, float) or isinstance(arg, long):
     return i, _ast.Num(arg)
   elif isinstance(arg, dict):
     return i, _ast.Dict(arg.keys(), arg.values())
   elif isinstance(arg, set):
     return i, _ast.Dict(arg)
   elif isinstance(arg, tuple):
     return i, _ast.Tuple(arg, _ast.Load())
   elif isinstance(arg, list):
     return i, _ast.List(arg, _ast.Load())
   elif isinstance(arg, bytes):
     return i, _ast.Bytes(arg)
   return i, None
def make_const(arg, lineno=0, col_offset=0):
    kw = {'lineno': lineno, 'col_offset': col_offset}

    if isinstance(arg, str):
        const = _ast.Str(s=arg, **kw)
    elif isinstance(arg, (int, float, complex)):
        const = _ast.Num(n=arg, **kw)
    elif arg is None:
        const = _ast.Name(id='None', ctx=_ast.Load(), **kw)
    elif isinstance(arg, tuple):
        elts = []
        for item in arg:
            elts.append(make_const(item, **kw))
        const = _ast.Tuple(elts=elts, ctx=_ast.Load(), **kw)
    else:
        const = arg

    return const
示例#11
0
  def make_assign_chained(i, bytecode):
    store_exprs = []
    value_exprs = []
    store_state, value_state = True, False

    while i >= 0:
      op, arg = bytecode[i][2], bytecode[i][3]
      if store_state:
        if op == DUP_TOP:
          prev_op = bytecode[i - 1][2] if i > 0 else -1
          if prev_op not in STORE_OPCODES:
            value_state = True
            store_state = False
        elif op in STORE_OPCODES:
          i, store_stmt = Statement.make_expr(i, bytecode, context=_ast.Store())
          store_exprs.insert(0, store_stmt)
      elif value_state:
        i, value_exprs = Statement.make_expr(i, bytecode)
        break
      i -= 1

    store_exprs = _ast.Tuple(store_exprs, _ast.Store())
    return i, _ast.Assign([store_exprs], value_exprs)
示例#12
0
def tuple_expr(elts: List[_ast.expr], ctx: _ast.expr_context) -> _ast.Tuple:
    return _ast.Tuple(elts=elts, ctx=ctx)
示例#13
0
def test_return_value_is_filled_tuple():
    # when method return ('1')
    assert ReturnedExpression(
        _ast.Return(value=_ast.Tuple(elts=['1'], ctx=_ast.Load()),
                    lineno=1), ).value_not_none() is True
示例#14
0
def test_return_value_is_empty_tuple():
    # when method return ()
    assert ReturnedExpression(
        _ast.Return(value=_ast.Tuple(elts=[], ctx=_ast.Load()),
                    lineno=1), ).value_not_none() is False
    def STORE_NAME(self, instr):

        value = self.pop_ast_item()
        value = self.process_ifexpr(value)

        if isinstance(value, _ast.Import):

            if value.from_:
                assert isinstance(self._ast_stack[-1], _ast.ImportFrom)
                from_ = self.pop_ast_item()

                as_name = instr.arg
                name = from_.names[0].name
                if as_name != name:
                    from_.names[0].asname = as_name

                self.push_ast_item(from_)
            else:
                as_name = instr.arg
                if value.names[0].asname is None:
                    base_name = value.names[0].name.split('.')[0]
                    if base_name != as_name:
                        value.names[0].asname = as_name

            self.push_ast_item(value)

        elif isinstance(value, (_ast.Attribute)) and isinstance(
                value.value, (_ast.Import)):
            asname = instr.arg
            value = value.value
            value.names[0].asname = asname

            self.push_ast_item(value)

        elif isinstance(value, (_ast.ClassDef, _ast.FunctionDef)):
            as_name = instr.arg
            value.name = as_name
            self.push_ast_item(value)
        elif isinstance(value, _ast.AugAssign):
            self.push_ast_item(value)
        elif isinstance(value, _ast.Assign):
            _ = self.pop_ast_item()
            assname = _ast.Name(instr.arg,
                                _ast.Store(),
                                lineno=instr.lineno,
                                col_offset=0)
            if _ is value.value or isinstance(_, _ast.Assign):
                value.targets.append(assname)
            else:
                if not isinstance(value.targets, _ast.Tuple):
                    value.targets = [_ast.Tuple(value.targets, _ast.Store())]
                    value.value = _ast.Tuple([value.value], _ast.Load())
                    value.targets[0].lineno = value.targets[0].elts[0].lineno
                    value.targets[0].col_offset = value.targets[0].elts[
                        0].col_offset
                    value.value.lineno = value.value.elts[0].lineno
                    value.value.col_offset = value.value.elts[0].col_offset
                value.targets[0].elts.append(assname)
                value.value.elts.append(_)

            self.push_ast_item(value)
        else:

            assname = _ast.Name(instr.arg,
                                _ast.Store(),
                                lineno=instr.lineno,
                                col_offset=0)

            assign = _ast.Assign(targets=[assname],
                                 value=value,
                                 lineno=instr.lineno,
                                 col_offset=0)
            self.push_ast_item(assign)