Ejemplo n.º 1
0
def verify_source(source):
    tree = fix_tree(parse(source, 'exec'))
    code = pycodegen.ModuleCodeGenerator(tree).getCode()
    generator = ModuleSourceCodeGenerator(tree)
    generated = generator.getSourceCode()
    try:
        new = fix_tree(parse(generated, 'exec'))
    except SyntaxError:
        return generated

    old = code.co_code
    new = pycodegen.ModuleCodeGenerator(new).getCode().co_code

    if old != new:
        return generated
Ejemplo n.º 2
0
 def compile(self, code, filename="<string>"):
     tree = self.parser.parse(code)
     misc.set_filename(filename, tree)
     syntax.check(tree)
     gen = pycodegen.ModuleCodeGenerator(tree)
     code = gen.getCode()
     return code
Ejemplo n.º 3
0
 def run(self, frame):
     # fall-back for unknown statement nodes
     try:
         expr = ast.Module(None, ast.Stmt([self.__obj__]))
         expr.filename = '<run>'
         co = pycodegen.ModuleCodeGenerator(expr).getCode()
         frame.exec_(co)
     except passthroughex:
         raise
     except:
         raise Failure(self)
Ejemplo n.º 4
0
def compileCodeObjects(filename, codeobjs):
    if len(codeobjs) == 0:
        stmts = []
    else:
        stmts = [ast.For(ast.AssName('[--codeobj--]', 'OP_ASSIGN'),
                         ast.Const(codeobjs),
                         ast.Stmt([ast.Exec(ast.Name('[--codeobj--]'),
                                            None, None)]),
                         None),
                 ast.AssName('[--codeobj--]', 'OP_DELETE')]

    module = ast.Module(None, ast.Stmt(stmts))
    compiler.misc.set_filename(filename, module)
    return pycodegen.ModuleCodeGenerator(module).getCode()
Ejemplo n.º 5
0
 def compile(self, source, filename="<string>"):
     """
 Compile the template source into a code object suitable for execution
 with exec or eval.
 
 source - template (Python) source to compile.
 filename - the filename used in the compiled code.  This name will be
            used in tracebacks.
 """
     mod = compiler.parse(source)
     misc.set_filename(filename, mod)
     self.preorder(mod, self)
     generator = pycodegen.ModuleCodeGenerator(mod)
     code = generator.getCode()
     return code
Ejemplo n.º 6
0
 def run(self, frame):
     expr = Interpretable(self.expr)
     expr.eval(frame)
     self.result = expr.result
     self.explanation = '... = ' + expr.explanation
     # fall-back-run the rest of the assignment
     ass = ast.Assign(self.nodes, ast.Name('__exprinfo_expr'))
     mod = ast.Module(None, ast.Stmt([ass]))
     mod.filename = '<run>'
     co = pycodegen.ModuleCodeGenerator(mod).getCode()
     try:
         frame.exec_(co, __exprinfo_expr=expr.result)
     except passthroughex:
         raise
     except:
         raise Failure(self)
Ejemplo n.º 7
0
 def compile(self):
     ast = parse(self.source, self.filename)
     gen = pycodegen.ModuleCodeGenerator(self.filename)
     walk(ast, gen, 1)
     self.code = gen.getCode()
Ejemplo n.º 8
0
def codegen(tree):
    gen = pycodegen.ModuleCodeGenerator(tree)
    code = gen.getCode()
    return code