Python _ast.AugAssign() Examples

The following are 6 code examples of _ast.AugAssign(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module _ast , or try the search function .
Example #1
Source File: types.py    From equip with Apache License 2.0 6 votes vote down vote up
def run(self, node, input_state):
    result_state = input_state.copy()
    block = node.data

    constraints = self.cfg.block_constraints

    for stmt in block.statements:
      native = stmt.native
      if isinstance(native, _ast.Assign) or isinstance(native, _ast.AugAssign):
        self.transfer_assign(result_state, native, stmt.start_bytecode_index)

      elif isinstance(native, _ast.Expr):
        value = native.value
        logger.debug("Stmt kind: %s", type(value))
        if isinstance(value, _ast.Call):
          self.transfer_call(result_state, native, stmt.start_bytecode_index)
      else:
        logger.error("Unknown stmt: %s", dump_native_ast(native))

    return result_state


  # Assign: a <- b 
Example #2
Source File: calc.py    From yui with GNU Affero General Public License v3.0 6 votes vote down vote up
def visit_augassign(self, node: _ast.AugAssign):  # target, op, value
        value = self._run(node.value)
        target = node.target
        target_cls = target.__class__
        op_cls = node.op.__class__

        if target_cls == _ast.Name:
            target_id = target.id  # type: ignore
            self.symbol_table[target_id] = BINOP_TABLE[op_cls](
                self.symbol_table[target_id], value,
            )
        elif target_cls == _ast.Subscript:
            sym = self._run(target.value)  # type: ignore
            xslice = self._run(target.slice)  # type: ignore
            if isinstance(target.slice, _ast.Index):  # type: ignore
                sym[xslice] = BINOP_TABLE[op_cls](sym[xslice], value,)
            else:
                raise BadSyntax('This assign method is not allowed')
        else:
            raise BadSyntax('This assign method is not allowed')
        return 
Example #3
Source File: defs.py    From equip with Apache License 2.0 5 votes vote down vote up
def run(self, node, input_state):
    result_state = input_state.copy()
    block = node.data

    for stmt in block.statements:
      native = stmt.native
      if not native:
        continue
      if isinstance(native, _ast.Assign) or isinstance(native, _ast.AugAssign):
        self.transfer_assign(result_state, native, stmt.start_bytecode_index)
      else:
        self.transfer_load(result_state, native, stmt.start_bytecode_index)

    return result_state 
Example #4
Source File: checker.py    From blackmamba with MIT License 5 votes vote down vote up
def __init__(self, name, source, scope):
        if '__all__' in scope and isinstance(source, ast.AugAssign):
            self.names = list(scope['__all__'].names)
        else:
            self.names = []
        if isinstance(source.value, (ast.List, ast.Tuple)):
            for node in source.value.elts:
                if isinstance(node, ast.Str):
                    self.names.append(node.s)
        super(ExportBinding, self).__init__(name, source) 
Example #5
Source File: checker.py    From Transcrypt with Apache License 2.0 5 votes vote down vote up
def __init__(self, name, source, scope):
        if '__all__' in scope and isinstance(source, ast.AugAssign):
            self.names = list(scope['__all__'].names)
        else:
            self.names = []
        if isinstance(source.value, (ast.List, ast.Tuple)):
            for node in source.value.elts:
                if isinstance(node, ast.Str):
                    self.names.append(node.s)
        super(ExportBinding, self).__init__(name, source) 
Example #6
Source File: stmt.py    From equip with Apache License 2.0 4 votes vote down vote up
def make_assign(i, bytecode):
    op = bytecode[i][2]
    if op == STORE_SUBSCR:
      return Statement.make_subscript(i, bytecode)

    prev_op = bytecode[i - 1][2] if i > 0 else -1

    if prev_op in INPLACE_OPCODES:
      in_cls = Statement.INPLACE_OPERATORS[prev_op]
      i -= 1
      i, rhs = Statement.make_expr(i - 1, bytecode, context=_ast.AugStore())
      i, lhs = Statement.make_expr(i - 1, bytecode, context=_ast.AugLoad())
      return i, _ast.AugAssign(lhs, in_cls(), rhs)
    else:
      # We can either have multiple assignments: a = b = c = 1
      # or unpacked sequences: a, b = 1, foo()
      # the compiler does some optimization so that: a, b = c, d
      # does not rely on UNPACK_SEQUENCE, but a ROT_TWO (or ROT_THREE & ROT_TWO for 3 elements).
      # This happens for 2 or 3 elements to unpack
      targets = []
      value = None
      has_unpack, has_ROT_2_3, has_multiple = False, False, 0
      num_unpack = -1
      j = i
      while j >= 0:
        op = bytecode[j][2]
        if op == UNPACK_SEQUENCE:
          has_unpack = True
          num_unpack = bytecode[j][3]
          break
        elif op in (ROT_TWO, ROT_THREE):
          has_ROT_2_3 = True
          break
        if op == DUP_TOP:
          has_multiple += 1
        j -= 1

      if has_unpack:
        return Statement.make_assign_unpack(i, bytecode, unpack_num=num_unpack)
      elif has_ROT_2_3:
        return Statement.make_assign_opt_unpack(i, bytecode)
      elif has_multiple > 0:
        return Statement.make_assign_chained(i, bytecode)
      else:
        # A simple assignment
        i, store_expr = Statement.make_expr(i, bytecode)
        i, value_expr = Statement.make_expr(i - 1, bytecode)
        return i, _ast.Assign([store_expr], value_expr)
    return i, None


  # 2 cases here:
  #  (1) a, b, c = foo() <=> v = foo(), a = v[0], b = v[1], c = v[2]
  #    => AST: _ast.Assign(targets=[Tuple(a, b, c)], value=foo())
  #
  #  (2) a, b = foo(), bar() <=> a = foo(), b = bar()
  #    => AST: _ast.Assign(targets=[Tuple(a, b)], value=Tuple(baz(), bar()))