我们从Python开源项目中,提取了以下40个代码示例,用于说明如何使用_ast.Store()。
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,))
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)
def STORE_SLICE_3(self, instr): 'obj[lower:upper] = expr' upper = self.ast_stack.pop() lower = self.ast_stack.pop() value = self.ast_stack.pop() expr = self.ast_stack.pop() kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=lower, step=None, upper=upper, **kw) subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Store(), **kw) if isinstance(expr, _ast.AugAssign): assign = expr result = cmp_ast(expr.target, subscr) assert result else: assign = _ast.Assign(targets=[subscr], value=expr, **kw) self.ast_stack.append(assign)
def STORE_SUBSCR(self, instr): index = self.ast_stack.pop() value = self.ast_stack.pop() expr = self.ast_stack.pop() expr = self.process_ifexpr(expr) if isinstance(expr, _ast.AugAssign): self.ast_stack.append(expr) else: kw = dict(lineno=instr.lineno, col_offset=0) index = self.format_slice(index, kw) subscr = _ast.Subscript(value=value, slice=index, ctx=_ast.Store(), **kw) assign = _ast.Assign(targets=[subscr], value=expr, **kw) self.ast_stack.append(assign)
def visitName(self, node): if isinstance(node.ctx, _ast.Store): self.modified.add(node.id) elif isinstance(node.ctx, _ast.Load): self.used.update(node.id) if not self.graph.has_node(node.id): self.graph.add_node(node.id) if isinstance(node.ctx, _ast.Load): self.undefined.add(node.id) for ctx_var in self.context_names: if not self.graph.has_edge(node.id, ctx_var): self.graph.add_edge(node.id, ctx_var) return {node.id}
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)
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)
def pop_assignment(stmnts, name): for i in range(len(stmnts)): stmnt = stmnts[i] if isinstance(stmnt, _ast.Assign) and len(stmnt.targets) == 1 \ and isinstance(stmnt.targets[0], _ast.Name) \ and isinstance(stmnt.targets[0].ctx, _ast.Store): if stmnt.targets[0].id == name: stmnts.pop(i) return stmnt.value return None
def INPLACE_(OP): def INPLACE_OP(self, instr): right = self.ast_stack.pop() left = self.ast_stack.pop() left.ctx = _ast.Store() aug_assign = _ast.AugAssign(target=left, op=OP(), value=right, lineno=instr.lineno, col_offset=0) self.ast_stack.append(aug_assign) return INPLACE_OP
def STORE_ATTR(self, instr): attrname = instr.arg node = self.ast_stack.pop() expr = self.ast_stack.pop() expr = self.process_ifexpr(expr) assattr = _ast.Attribute(value=node, attr=attrname, ctx=_ast.Store(), lineno=instr.lineno, col_offset=0) set_attr = _ast.Assign(targets=[assattr], value=expr, lineno=instr.lineno, col_offset=0) self.ast_stack.append(set_attr)
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 STORE_SLICE_0(self, instr): 'obj[:] = expr' value = self.ast_stack.pop() expr = self.ast_stack.pop() kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=None, step=None, upper=None, **kw) subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Store(), **kw) assign = _ast.Assign(targets=[subscr], value=expr, **kw) self.ast_stack.append(assign)
def STORE_SLICE_1(self, instr): 'obj[lower:] = expr' lower = self.ast_stack.pop() value = self.ast_stack.pop() expr = self.ast_stack.pop() kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=lower, step=None, upper=None, **kw) subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Store(), **kw) assign = _ast.Assign(targets=[subscr], value=expr, **kw) self.ast_stack.append(assign)
def STORE_SLICE_2(self, instr): 'obj[:upper] = expr' upper = self.ast_stack.pop() value = self.ast_stack.pop() expr = self.ast_stack.pop() kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=None, step=None, upper=upper, **kw) subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Store(), **kw) assign = _ast.Assign(targets=[subscr], value=expr, **kw) self.ast_stack.append(assign)
def handle_generators(self, generators): defined = set() required = set() for generator in generators: get_symbols(generator, _ast.Load) required.update(get_symbols(generator, _ast.Load) - defined) defined.update(get_symbols(generator, _ast.Store)) return defined, required
def visitAssign(self, node): nodes = self.visit(node.value) tsymols = get_symbols(node, _ast.Store) re_defined = tsymols.intersection(set(self.graph.nodes())) if re_defined: add_edges(self.graph, re_defined, re_defined) targets = set() for target in node.targets: targets.update(self.visit(target)) add_edges(self.graph, targets, nodes) return targets | nodes
def _native_repr_tree(self, node, indent, _done=None): """recursive method for the native tree representation""" from _ast import Load as _Load, Store as _Store, Del as _Del from _ast import AST as Node if _done is None: _done = set() if node in _done: self._string += '\nloop in tree: %r (%s)' % ( node, getattr(node, 'lineno', None)) return _done.add(node) self._string += '\n' + indent + '<%s>' % node.__class__.__name__ indent += self.indent if not hasattr(node, '__dict__'): self._string += '\n' + self.indent + " ** node has no __dict__ " + str(node) return node_dict = node.__dict__ if hasattr(node, '_attributes'): for a in node._attributes: attr = node_dict[a] if attr is None: continue if a in ("lineno", "col_offset") and not self.lineno: continue self._string += '\n' + indent + a + " = " + repr(attr) for field in node._fields or (): attr = node_dict[field] if attr is None: continue if isinstance(attr, list): if not attr: continue self._string += '\n' + indent + field + ' = [' for elt in attr: self._native_repr_tree(elt, indent, _done) self._string += '\n' + indent + ']' continue if isinstance(attr, (_Load, _Store, _Del)): continue if isinstance(attr, Node): self._string += '\n' + indent + field + " = " self._native_repr_tree(attr, indent, _done) else: self._string += '\n' + indent + field + " = " + repr(attr)
def STORE_NAME(self, instr): value = self.ast_stack.pop() value = self.process_ifexpr(value) if isinstance(value, _ast.Import): if value.from_: assert isinstance(self.ast_stack[-1], _ast.ImportFrom) from_ = self.ast_stack.pop() as_name = instr.arg name = from_.names[0].name if as_name != name: from_.names[0].asname = as_name self.ast_stack.append(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.ast_stack.append(value) elif isinstance(value, (_ast.Attribute)) and isinstance(value.value, (_ast.Import)): asname = instr.arg value = value.value value.names[0].asname = asname self.ast_stack.append(value) elif isinstance(value, (_ast.ClassDef, _ast.FunctionDef)): as_name = instr.arg value.name = as_name self.ast_stack.append(value) elif isinstance(value, _ast.AugAssign): self.ast_stack.append(value) elif isinstance(value, _ast.Assign): _ = self.ast_stack.pop() assname = _ast.Name(instr.arg, _ast.Store(), lineno=instr.lineno, col_offset=0) value.targets.append(assname) self.ast_stack.append(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.ast_stack.append(assign)