我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用compiler.walk()。
def _visitFuncOrLambda(self, node, isLambda=0): if not isLambda and node.decorators: for decorator in node.decorators.nodes: self.visit(decorator) ndecorators = len(node.decorators.nodes) else: ndecorators = 0 gen = self.FunctionGen(node, self.scopes, isLambda, self.class_name, self.get_module()) walk(node.code, gen) gen.finish() self.set_lineno(node) for default in node.defaults: self.visit(default) self._makeClosure(gen, len(node.defaults)) for i in range(ndecorators): self.emit('CALL_FUNCTION', 1)
def visitClass(self, node): gen = self.ClassGen(node, self.scopes, self.get_module()) walk(node.code, gen) gen.finish() self.set_lineno(node) self.emit('LOAD_CONST', node.name) for base in node.bases: self.visit(base) self.emit('BUILD_TUPLE', len(node.bases)) self._makeClosure(gen, 0) self.emit('CALL_FUNCTION', 0) self.emit('BUILD_CLASS') self.storeName(node.name) # The rest are standard visitor methods # The next few implement control-flow statements
def parseSymbols(self, tree): s = symbols.SymbolVisitor() walk(tree, s) return s.scopes
def visitModule(self, node): self.scopes = self.parseSymbols(node) self.scope = self.scopes[node] self.emit('SET_LINENO', 0) if node.doc: self.emit('LOAD_CONST', node.doc) self.storeName('__doc__') lnf = walk(node.node, self.NameFinder(), verbose=0) self.locals.push(lnf.getLocals()) self.visit(node.node) self.emit('LOAD_CONST', None) self.emit('RETURN_VALUE')
def visitGenExpr(self, node): gen = GenExprCodeGenerator(node, self.scopes, self.class_name, self.get_module()) walk(node.code, gen) gen.finish() self.set_lineno(node) self._makeClosure(gen, 0) # precomputation of outmost iterable self.visit(node.code.quals[0].iter) self.emit('GET_ITER') self.emit('CALL_FUNCTION', 1)
def __init__(self, tree): self.graph = pyassem.PyFlowGraph("<expression>", tree.filename) self.__super_init() walk(tree, self)
def __init__(self, tree): self.graph = pyassem.PyFlowGraph("<interactive>", tree.filename) self.__super_init() self.set_lineno(tree) walk(tree, self) self.emit('RETURN_VALUE')
def __init__(self, func, scopes, isLambda, class_name, mod): self.class_name = class_name self.module = mod if isLambda: klass = FunctionCodeGenerator name = "<lambda.%d>" % klass.lambdaCount klass.lambdaCount = klass.lambdaCount + 1 else: name = func.name args, hasTupleArg = generateArgList(func.argnames) self.graph = pyassem.PyFlowGraph(name, func.filename, args, optimized=1) self.isLambda = isLambda self.super_init() if not isLambda and func.doc: self.setDocstring(func.doc) lnf = walk(func.code, self.NameFinder(args), verbose=0) self.locals.push(lnf.getLocals()) if func.varargs: self.graph.setFlag(CO_VARARGS) if func.kwargs: self.graph.setFlag(CO_VARKEYWORDS) self.set_lineno(func) if hasTupleArg: self.generateArgUnpack(func.argnames)
def __init__(self, klass, scopes, module): self.class_name = klass.name self.module = module self.graph = pyassem.PyFlowGraph(klass.name, klass.filename, optimized=0, klass=1) self.super_init() lnf = walk(klass.code, self.NameFinder(), verbose=0) self.locals.push(lnf.getLocals()) self.graph.setFlag(CO_NEWLOCALS) if klass.doc: self.setDocstring(klass.doc)
def findOp(node): """Find the op (DELETE, LOAD, STORE) in an AssTuple tree""" v = OpFinder() walk(node, v, verbose=0) return v.op
def main(files): mf = MethodFinder() for file in files: f = open(file) buf = f.read() f.close() ast = compiler.parse(buf) compiler.walk(ast, mf)
def __init__(self, tree): self.graph = pyassem.PyFlowGraph("<module>", tree.filename) self.futures = future.find_futures(tree) self.__super_init() walk(tree, self)