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(), parentscopeambiguity = self.scopeambiguity or self.parentscopeambiguity) walk(node.code, gen) gen.finish() self.set_lineno(node) for default in node.defaults: self.visit(default) frees = gen.scope.get_free_vars() if frees: for name in frees: self.emit('LOAD_CLOSURE', name) self.emit('LOAD_CONST', gen) self.emit('MAKE_CLOSURE', len(node.defaults)) else: self.emit('LOAD_CONST', gen) self.emit('MAKE_FUNCTION', len(node.defaults)) for i in range(ndecorators): self.emit('CALL_FUNCTION', 1)
def __init__(self, tree, futures = []): self.graph = pyassem.PyFlowGraph("<module>", tree.filename) self.futures = future.find_futures(tree) for f in futures: if f not in self.futures: self.futures.append(f) self.__super_init() walk(tree, self)
def __init__(self, tree, futures=[]): self.graph = pyassem.PyFlowGraph("<interactive>", tree.filename) self.futures = future.find_futures(tree) for f in futures: if f not in self.futures: self.futures.append(f) 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>" else: name = func.name args, hasTupleArg = generateArgList(func.argnames) self.graph = pyassem.PyFlowGraph(name, func.filename, args, optimized=self.localsfullyknown, newlocals=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 visitGenExpr(self, node): gen = GenExprCodeGenerator(node, self.scopes, self.class_name, self.get_module(), parentscopeambiguity=self.scopeambiguity or self.parentscopeambiguity) walk(node.code, gen) gen.finish() self.set_lineno(node) frees = gen.scope.get_free_vars() if frees: for name in frees: self.emit('LOAD_CLOSURE', name) self.emit('LOAD_CONST', gen) self.emit('MAKE_CLOSURE', 0) else: self.emit('LOAD_CONST', gen) self.emit('MAKE_FUNCTION', 0) # precomputation of outmost iterable self.visit(node.code.quals[0].iter) self.emit('GET_ITER') self.emit('CALL_FUNCTION', 1)
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 visitClass(self, node): gen = self.ClassGen(node, self.scopes, self.get_module(), parentscopeambiguity = self.scopeambiguity or self.parentscopeambiguity) 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)) frees = gen.scope.get_free_vars() for name in frees: self.emit('LOAD_CLOSURE', name) self.emit('LOAD_CONST', gen) if frees: self.emit('MAKE_CLOSURE', 0) else: self.emit('MAKE_FUNCTION', 0) self.emit('CALL_FUNCTION', 0) self.emit('BUILD_CLASS') self.storeName(node.name)
""" def visitFrom(self, node): if hasattr(node, 'valid_future'): return if node.modname != "__future__": return raise SyntaxError( "from __future__ imports must occur at the beginning of the file", (node.filename, node.lineno, 0, "")) def find_futures(node): p1 = FutureParser() p2 = BadFutureParser() walk(node, p1) walk(node, p2) return p1.get_features() if __name__ == "__main__": import sys from pypy.interpreter.stablecompiler import parseFile, walk for file in sys.argv[1:]: print file tree = parseFile(file) v = FutureParser() walk(tree, v) print v.found print
def find_futures(node): p1 = FutureParser() p2 = BadFutureParser() walk(node, p1) walk(node, p2) return p1.get_features()
def visitFrom(self, node): if hasattr(node, "valid_future"): return if node.modname != "__future__": return raise SyntaxError( "from __future__ imports must occur at the beginning of the file", (node.filename, node.lineno, 0, "") ) def find_futures(node): p1 = FutureParser() p2 = BadFutureParser() walk(node, p1) walk(node, p2) return p1.get_features() if __name__ == "__main__": import sys from pypy.interpreter.stablecompiler import parseFile, walk for file in sys.argv[1:]: print file tree = parseFile(file) v = FutureParser() walk(tree, v) print v.found print
def check(tree, multi=None): v = SyntaxErrorChecker(multi) walk(tree, v) return v.errors
import symtable def get_names(syms): return [s for s in [s.get_name() for s in syms.get_symbols()] if not (s.startswith('_[') or s.startswith('.'))] for file in sys.argv[1:]: print file f = open(file) buf = f.read() f.close() syms = symtable.symtable(buf, file, "exec") mod_names = get_names(syms) tree = parseFile(file) s = SymbolVisitor() walk(tree, s) # compare module-level symbols names2 = s.scopes[tree].get_names() if not list_eq(mod_names, names2): print print "oops", file print sort(mod_names) print sort(names2) sys.exit(-1) d = {} d.update(s.scopes) del d[tree] scopes = d.values()
def get_names(syms): return [ s for s in [s.get_name() for s in syms.get_symbols()] if not (s.startswith('_[') or s.startswith('.')) ] for file in sys.argv[1:]: print file f = open(file) buf = f.read() f.close() syms = symtable.symtable(buf, file, "exec") mod_names = get_names(syms) tree = parseFile(file) s = SymbolVisitor() walk(tree, s) # compare module-level symbols names2 = s.scopes[tree].get_names() if not list_eq(mod_names, names2): print print "oops", file print sort(mod_names) print sort(names2) sys.exit(-1) d = {} d.update(s.scopes) del d[tree] scopes = d.values()
def findOp(node): """Find the op (DELETE, LOAD, STORE) in an AssTuple tree""" v = OpFinder() walk(node, v, verbose=0) return v.op
def __init__(self, tree, futures=[]): self.graph = pyassem.PyFlowGraph("<expression>", tree.filename) self.futures = futures[:] self.__super_init() walk(tree, self)
def parseSymbols(self, tree): s = symbols.SymbolVisitor() walk(tree, s) return s.scopes