Exemple #1
0
 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)
Exemple #2
0
 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)
Exemple #3
0
 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)
Exemple #4
0
 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)
Exemple #5
0
    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)
Exemple #6
0
    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)
Exemple #7
0
    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)
Exemple #8
0
    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)
Exemple #9
0
 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)
Exemple #10
0
 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')
Exemple #11
0
 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')
Exemple #12
0
 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)
Exemple #13
0
 def parseSymbols(self, tree):
     s = symbols.SymbolVisitor()
     walk(tree, s)
     return s.scopes
Exemple #14
0
def findOp(node):
    """Find the op (DELETE, LOAD, STORE) in an AssTuple tree"""
    v = OpFinder()
    walk(node, v, verbose=0)
    return v.op
Exemple #15
0
 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')
Exemple #16
0
 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')
Exemple #17
0
 def __init__(self, tree):
     self.graph = pyassem.PyFlowGraph("<expression>", tree.filename)
     self.__super_init()
     walk(tree, self)
Exemple #18
0
 def __init__(self, tree):
     self.graph = pyassem.PyFlowGraph("<expression>", tree.filename)
     self.__super_init()
     walk(tree, self)
Exemple #19
0
 def parseSymbols(self, tree):
     s = symbols.SymbolVisitor()
     walk(tree, s)
     return s.scopes
Exemple #20
0
def findOp(node):
    """Find the op (DELETE, LOAD, STORE) in an AssTuple tree"""
    v = OpFinder()
    walk(node, v, verbose=0)
    return v.op
def find_futures(node):
    p1 = FutureParser()
    p2 = BadFutureParser()
    walk(node, p1)
    walk(node, p2)
    return p1.get_features()
Exemple #22
0
 def __init__(self, tree):
     self.graph = pyassem.PyFlowGraph("<module>", tree.filename)
     self.futures = future.find_futures(tree)
     self.__super_init()
     walk(tree, self)
class BadFutureParser:
    """Check for invalid future statements"""
    def visitFrom(self, node):
        if hasattr(node, 'valid_future'):
            return
        if node.modname != "__future__":
            return
        raise SyntaxError, "invalid future statement " + repr(node)


def find_futures(node):
    p1 = FutureParser()
    p2 = BadFutureParser()
    walk(node, p1)
    walk(node, p2)
    return p1.get_features()


if __name__ == "__main__":
    import sys
    from compiler import parseFile, walk

    for file in sys.argv[1:]:
        print file
        tree = parseFile(file)
        v = FutureParser()
        walk(tree, v)
        print v.found
        print
Exemple #24
0
 def __init__(self, tree):
     self.graph = pyassem.PyFlowGraph("<module>", tree.filename)
     self.futures = future.find_futures(tree)
     self.__super_init()
     walk(tree, self)