Python _ast.Store() Examples

The following are 29 code examples of _ast.Store(). 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: rebuilder.py    From linter-pylama with MIT License 6 votes vote down vote up
def visit_attribute(self, node, parent):
        """visit an Attribute node by returning a fresh instance of it"""
        context = _get_context(node)
        if context == astroid.Del:
            # FIXME : maybe we should reintroduce and visit_delattr ?
            # for instance, deactivating assign_ctx
            newnode = nodes.DelAttr(node.attr, node.lineno, node.col_offset,
                                    parent)
        elif context == astroid.Store:
            newnode = nodes.AssignAttr(node.attr, node.lineno, node.col_offset,
                                       parent)
            # Prohibit a local save if we are in an ExceptHandler.
            if not isinstance(parent, astroid.ExceptHandler):
                self._delayed_assattr.append(newnode)
        else:
            newnode = nodes.Attribute(node.attr, node.lineno, node.col_offset,
                                      parent)
        newnode.postinit(self.visit(node.value, newnode))
        return newnode 
Example #2
Source File: rebuilder.py    From linter-pylama with MIT License 6 votes vote down vote up
def visit_name(self, node, parent):
        """visit a Name node by returning a fresh instance of it"""
        context = _get_context(node)
        # True and False can be assigned to something in py2x, so we have to
        # check first the context.
        if context == astroid.Del:
            newnode = nodes.DelName(node.id, node.lineno, node.col_offset,
                                    parent)
        elif context == astroid.Store:
            newnode = nodes.AssignName(node.id, node.lineno, node.col_offset,
                                       parent)
        elif node.id in CONST_NAME_TRANSFORMS:
            newnode = nodes.Const(CONST_NAME_TRANSFORMS[node.id],
                                  getattr(node, 'lineno', None),
                                  getattr(node, 'col_offset', None), parent)
            return newnode
        else:
            newnode = nodes.Name(node.id, node.lineno, node.col_offset, parent)
        # XXX REMOVE me :
        if context in (astroid.Del, astroid.Store): # 'Aug' ??
            self._save_assignment(newnode)
        return newnode 
Example #3
Source File: stmt.py    From equip with Apache License 2.0 6 votes vote down vote up
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) 
Example #4
Source File: checker.py    From Transcrypt with Apache License 2.0 6 votes vote down vote up
def NAME(self, node):
        """
        Handle occurrence of Name (which can be a load/store/delete access.)
        """
        # Locate the name in locals / function / globals scopes.
        if isinstance(node.ctx, (ast.Load, ast.AugLoad)):
            self.handleNodeLoad(node)
            if (node.id == 'locals' and isinstance(self.scope, FunctionScope)
                    and isinstance(node.parent, ast.Call)):
                # we are doing locals() call in current scope
                self.scope.usesLocals = True
        elif isinstance(node.ctx, (ast.Store, ast.AugStore)):
            self.handleNodeStore(node)
        elif isinstance(node.ctx, ast.Del):
            self.handleNodeDelete(node)
        else:
            # must be a Param context -- this only happens for names in function
            # arguments, but these aren't dispatched through here
            raise RuntimeError("Got impossible expression context: %r" % (node.ctx,)) 
Example #5
Source File: checker.py    From blackmamba with MIT License 6 votes vote down vote up
def TUPLE(self, node):
        if not PY2 and isinstance(node.ctx, ast.Store):
            # Python 3 advanced tuple unpacking: a, *b, c = d.
            # Only one starred expression is allowed, and no more than 1<<8
            # assignments are allowed before a stared expression. There is
            # also a limit of 1<<24 expressions after the starred expression,
            # which is impossible to test due to memory restrictions, but we
            # add it here anyway
            has_starred = False
            star_loc = -1
            for i, n in enumerate(node.elts):
                if isinstance(n, ast.Starred):
                    if has_starred:
                        self.report(messages.TwoStarredExpressions, node)
                        # The SyntaxError doesn't distinguish two from more
                        # than two.
                        break
                    has_starred = True
                    star_loc = i
            if star_loc >= 1 << 8 or len(node.elts) - star_loc - 1 >= 1 << 24:
                self.report(messages.TooManyExpressionsInStarredAssignment, node)
        self.handleChildren(node) 
Example #6
Source File: checker.py    From blackmamba with MIT License 6 votes vote down vote up
def NAME(self, node):
        """
        Handle occurrence of Name (which can be a load/store/delete access.)
        """
        # Locate the name in locals / function / globals scopes.
        if isinstance(node.ctx, (ast.Load, ast.AugLoad)):
            self.handleNodeLoad(node)
            if (node.id == 'locals' and isinstance(self.scope, FunctionScope)
                    and isinstance(node.parent, ast.Call)):
                # we are doing locals() call in current scope
                self.scope.usesLocals = True
        elif isinstance(node.ctx, (ast.Store, ast.AugStore)):
            self.handleNodeStore(node)
        elif isinstance(node.ctx, ast.Del):
            self.handleNodeDelete(node)
        else:
            # must be a Param context -- this only happens for names in function
            # arguments, but these aren't dispatched through here
            raise RuntimeError("Got impossible expression context: %r" % (node.ctx,)) 
Example #7
Source File: ast_visitor.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_Attribute( self, node ):
    if isinstance( node.ctx, _ast.Store ):
      if node.attr == self.attr:
        try:
          src,funclineno = inspect.getsourcelines( self.func )
        except IOError:
          # Dynamically generated AST encountered
          # Return null source code and line number as they are not
          # applicable to dynamic AST.
          src, funclineno = '', 0
        lineno         = funclineno + node.lineno - 1
        raise PyMTLError(
          'Cannot write .{attr} in a {kind} block!\n\n'
          ' {lineno} {srccode}\n'
          ' File: {filename}\n'
          ' Function: {funcname}\n'
          ' Line: {lineno}\n'.format(
            attr     = self.attr,
            kind     = {'value':'@tick','next':'@combinational'}[ self.attr ],
            srccode  = src[node.lineno-1],
            filename = inspect.getfile( self.func ),
            funcname = self.func.func_name,
            lineno   = lineno,
          )
        )

#------------------------------------------------------------------------
# DetectMissingValueNext
#------------------------------------------------------------------------ 
Example #8
Source File: pyparser.py    From android_universal with MIT License 5 votes vote down vote up
def visit_Name(self, node):
        if isinstance(node.ctx, _ast.Store):
            # this is eqiuvalent to visit_AssName in
            # compiler
            self._add_declared(node.id)
        elif node.id not in reserved and node.id \
            not in self.listener.declared_identifiers and node.id \
                not in self.local_ident_stack:
            self.listener.undeclared_identifiers.add(node.id) 
Example #9
Source File: pyparser.py    From ansible-cmdb with GNU General Public License v3.0 5 votes vote down vote up
def visit_Name(self, node):
        if isinstance(node.ctx, _ast.Store):
            # this is eqiuvalent to visit_AssName in
            # compiler
            self._add_declared(node.id)
        elif node.id not in reserved and node.id \
            not in self.listener.declared_identifiers and node.id \
                not in self.local_ident_stack:
            self.listener.undeclared_identifiers.add(node.id) 
Example #10
Source File: pyparser.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def visit_Name(self, node):
        if isinstance(node.ctx, _ast.Store):
            # this is eqiuvalent to visit_AssName in
            # compiler
            self._add_declared(node.id)
        elif (
            node.id not in reserved
            and node.id not in self.listener.declared_identifiers
            and node.id not in self.local_ident_stack
        ):
            self.listener.undeclared_identifiers.add(node.id) 
Example #11
Source File: pyparser.py    From docassemble with MIT License 5 votes vote down vote up
def visit_Name(self, node):
        if isinstance(node.ctx, _ast.Store):
            # this is eqiuvalent to visit_AssName in
            # compiler
            self._add_declared(node.id)
        elif node.id not in reserved and node.id \
            not in self.listener.declared_identifiers and node.id \
                not in self.local_ident_stack:
            self.listener.undeclared_identifiers.add(node.id) 
Example #12
Source File: pyparser.py    From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def visit_Name(self, node):
        if isinstance(node.ctx, _ast.Store):
            # this is eqiuvalent to visit_AssName in
            # compiler
            self._add_declared(node.id)
        elif node.id not in reserved and node.id \
            not in self.listener.declared_identifiers and node.id \
                not in self.local_ident_stack:
            self.listener.undeclared_identifiers.add(node.id) 
Example #13
Source File: checker.py    From blackmamba with MIT License 5 votes vote down vote up
def ANNASSIGN(self, node):
        if node.value:
            # Only bind the *targets* if the assignment has a value.
            # Otherwise it's not really ast.Store and shouldn't silence
            # UndefinedLocal warnings.
            self.handleNode(node.target, node)
        self.handleNode(node.annotation, node)
        if node.value:
            # If the assignment has value, handle the *value* now.
            self.handleNode(node.value, node) 
Example #14
Source File: ast_visitor.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_Subscript( self, node ):
    if not self.assign: return
    if   isinstance( node.ctx, _ast.Load ):
      self.load  += [ GetVariableName( self ).visit( node ) ]
    elif isinstance( node.ctx, _ast.Store ):
      self.store += [ GetVariableName( self ).visit( node ) ]
    else:
      print( type( node.ctx ) )
      raise Exception( "Unsupported concurrent block code!" )

  # TODO: need this to detect writes to bit slices?
  #def visit_Subscript( self, node ):
  #  if not self.assign: return
  #  if   isinstance( node.ctx, _ast.Load ):
  #    self.load  += [ GetVariableName( self ).visit( node ) ]
  #  elif isinstance( node.ctx, _ast.Store ):
  #    self.store += [ GetVariableName( self ).visit( node ) ]
  #  else:
  #    print( type( node.ctx ) )
  #    raise Exception( "Unsupported concurrent block code!" )


#------------------------------------------------------------------------
# GetVariableName
#------------------------------------------------------------------------
# Converts an AST branch, beginning with the indicated node, into it's
# corresponding Python name.  Meant to be called as a utility visitor
# by another AST visitor, which should pass a reference to itself as
# a parameter. 
Example #15
Source File: ast_visitor.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_Name( self, node ):
    if not self.assign: return
    if   isinstance( node.ctx, _ast.Load ):
      self.load  += [ GetVariableName( self ).visit( node ) ]
    elif isinstance( node.ctx, _ast.Store ):
      self.store += [ GetVariableName( self ).visit( node ) ]
    else:
      print( type( node.ctx ) )
      raise Exception( "Unsupported concurrent block code!" ) 
Example #16
Source File: ast_visitor.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_Attribute( self, node ):
    if not self.assign: return
    if   isinstance( node.ctx, _ast.Load ):
      self.load  += [ GetVariableName( self ).visit( node ) ]
    elif isinstance( node.ctx, _ast.Store ):
      self.store += [ GetVariableName( self ).visit( node ) ]
    else:
      print( type( node.ctx ) )
      raise Exception( "Unsupported concurrent block code!" ) 
Example #17
Source File: ast_visitor.py    From pymtl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def visit_Attribute( self, node ):
    self.decorators.append( node.attr )

#------------------------------------------------------------------------
# DetectLoadsAndStores
#------------------------------------------------------------------------
# AST traversal class which detects loads and stores within a concurrent
# block.  Load detection is useful for generating the sensitivity list
# for @combinational blocks in the Simulator.  Store detection is useful
# for determining 'reg' type variables during Verilog translation. 
Example #18
Source File: pyparser.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def visit_Name(self, node):
        if isinstance(node.ctx, _ast.Store):
            # this is eqiuvalent to visit_AssName in
            # compiler
            self._add_declared(node.id)
        elif (
            node.id not in reserved
            and node.id not in self.listener.declared_identifiers
            and node.id not in self.local_ident_stack
        ):
            self.listener.undeclared_identifiers.add(node.id) 
Example #19
Source File: pyparser.py    From mako with MIT License 5 votes vote down vote up
def visit_Name(self, node):
        if isinstance(node.ctx, _ast.Store):
            # this is eqiuvalent to visit_AssName in
            # compiler
            self._add_declared(node.id)
        elif (
            node.id not in reserved
            and node.id not in self.listener.declared_identifiers
            and node.id not in self.local_ident_stack
        ):
            self.listener.undeclared_identifiers.add(node.id) 
Example #20
Source File: pyparser.py    From teleport with Apache License 2.0 5 votes vote down vote up
def visit_Name(self, node):
        if isinstance(node.ctx, _ast.Store):
            # this is eqiuvalent to visit_AssName in
            # compiler
            self._add_declared(node.id)
        elif (
            node.id not in reserved
            and node.id not in self.listener.declared_identifiers
            and node.id not in self.local_ident_stack
        ):
            self.listener.undeclared_identifiers.add(node.id) 
Example #21
Source File: pyparser.py    From teleport with Apache License 2.0 5 votes vote down vote up
def visit_Name(self, node):
        if isinstance(node.ctx, _ast.Store):
            # this is eqiuvalent to visit_AssName in
            # compiler
            self._add_declared(node.id)
        elif (
            node.id not in reserved
            and node.id not in self.listener.declared_identifiers
            and node.id not in self.local_ident_stack
        ):
            self.listener.undeclared_identifiers.add(node.id) 
Example #22
Source File: pyparser.py    From teleport with Apache License 2.0 5 votes vote down vote up
def visit_Name(self, node):
        if isinstance(node.ctx, _ast.Store):
            # this is eqiuvalent to visit_AssName in
            # compiler
            self._add_declared(node.id)
        elif node.id not in reserved and node.id \
            not in self.listener.declared_identifiers and node.id \
                not in self.local_ident_stack:
            self.listener.undeclared_identifiers.add(node.id) 
Example #23
Source File: stmt.py    From equip with Apache License 2.0 5 votes vote down vote up
def make_store_delete_slice(i, bytecode, context=None):
    op = bytecode[i][2]
    is_delete = op in DELETE_SLICE_OPCODES

    if context is None:
      context = _ast.Store() if not is_delete else _ast.Del()

    lhs_expr = None

    if op in (STORE_SLICE_0, DELETE_SLICE_0):
      i, lhs_expr = Statement.make_expr(i - 1, bytecode, context=context)
      lhs_expr = _ast.Subscript(lhs_expr,
                                _ast.Slice(None, None, None),
                                _ast.Store())
    elif op in (STORE_SLICE_1, STORE_SLICE_2, DELETE_SLICE_1, DELETE_SLICE_2):
      i, index_expr = Statement.make_expr(i - 1, bytecode)
      i, arr_expr = Statement.make_expr(i - 1, bytecode, context=context)

      args = [None] * 3
      index_index = 0 if op in (STORE_SLICE_1, DELETE_SLICE_1) else 1
      args[index_index] = index_expr
      lhs_expr = _ast.Subscript(arr_expr,
                                _ast.Slice(*args),
                                _ast.Store())
    else:
      i, end_index_expr = Statement.make_expr(i - 1, bytecode)
      i, start_index_expr = Statement.make_expr(i - 1, bytecode)
      i, arr_expr = Statement.make_expr(i - 1, bytecode, context=context)

      lhs_expr = _ast.Subscript(arr_expr,
                                _ast.Slice(start_index_expr, end_index_expr, None),
                                _ast.Store())

    if is_delete:
      return i, _ast.Delete([lhs_expr])
    else:
      i, rhs_expr = Statement.make_expr(i - 1, bytecode)
      return i, _ast.Assign([lhs_expr], rhs_expr) 
Example #24
Source File: stmt.py    From equip with Apache License 2.0 5 votes vote down vote up
def make_exception(i, bytecode):
    logger.error("Exception STMT not handled.")
    return i, None


  # Store slice has 4 opodes:
  #  (1) STORE_SLICE+0 => TOS[:] = TOS1
  #  (2) STORE_SLICE+1 => TOS1[TOS:] = TOS2
  #  (3) STORE_SLICE+2 => TOS1[:TOS] = TOS2
  #  (4) STORE_SLICE+3 => TOS2[TOS1:TOS] = TOS3 
Example #25
Source File: stmt.py    From equip with Apache License 2.0 5 votes vote down vote up
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)


  # Only one case here for:
  #   a = b = z.d.f = foo()
  #    => AST: _ast.Assign(targets=[Tuple(a, b, z.d.f)], value=foo()) 
Example #26
Source File: stmt.py    From equip with Apache License 2.0 5 votes vote down vote up
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) 
Example #27
Source File: pyparser.py    From jbox with MIT License 5 votes vote down vote up
def visit_Name(self, node):
        if isinstance(node.ctx, _ast.Store):
            # this is eqiuvalent to visit_AssName in
            # compiler
            self._add_declared(node.id)
        elif node.id not in reserved and node.id \
            not in self.listener.declared_identifiers and node.id \
                not in self.local_ident_stack:
            self.listener.undeclared_identifiers.add(node.id) 
Example #28
Source File: pyparser.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def visit_Name(self, node):
        if isinstance(node.ctx, _ast.Store):
            # this is eqiuvalent to visit_AssName in
            # compiler
            self._add_declared(node.id)
        elif (
            node.id not in reserved
            and node.id not in self.listener.declared_identifiers
            and node.id not in self.local_ident_stack
        ):
            self.listener.undeclared_identifiers.add(node.id) 
Example #29
Source File: cpp.py    From pymtl with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def visit_Num(self, node):
    node._object = int( node.n )
    print >> self.o, node.n,

#  #---------------------------------------------------------------------
#  # visit_Self
#  #---------------------------------------------------------------------
#  # TODO: handle names properly
#  #def visit_Self(self, node):
#  #  self.this_obj = ThisObject( '', self.model )
#  #  self.visit( node.value )
#  #  print >> self.o, self.this_obj.name,
#
#  #---------------------------------------------------------------------
#  # visit_Local
#  #---------------------------------------------------------------------
#  # TODO: handle names properly
#  #def visit_Local(self, node):
#  #  obj = getattr( self.model, node.attr )
#  #  self.this_obj = ThisObject( node.attr, obj )
#  #  self.visit( node.value )
#  #  print >> self.o, self.this_obj.name,
#
#  #---------------------------------------------------------------------
#  # visit_Attribute
#  #---------------------------------------------------------------------
#  # TODO: currently assuming all attributes are bits objects,
#  #       need to fix for PortBundles and BitStructs
#  def visit_Attribute(self, node):
#    if isinstance( node.ctx, _ast.Store ):
#      self.regs.add( node._object )
#
#    # TODO: hacky
#    if   isinstance( node._object, list ):
#      print >> self.o, node._object[0].name.split('[')[0],
#    # TODO:convert int attributes into contant nodes!
#    elif isinstance( node._object, int ):
#      print >> self.o, node._object,
#    else:
#      print >> self.o, signal_to_str( node._object, None, self.model ),
#
#  #---------------------------------------------------------------------
#  # visit_Temp
#  #---------------------------------------------------------------------
#  def visit_Temp(self, node):
#    self.temps.add( node.id )
#    print >> self.o, node.id,
#
  #---------------------------------------------------------------------
  # visit_For
  #---------------------------------------------------------------------
  # TODO: does this work?