Esempio n. 1
0
 def test_mutate_elision(self):
     class ConstRemover(BaseVisitor):
         def visitConst(self, const):
             return None
     listast = source2ast("[1, 2]", 'exec')
     listast = listast.mutate(ConstRemover())
     listnode = listast.node.nodes[0].expr
     assert isinstance(listnode, ast.List)
     assert len(listnode.nodes) == 0
Esempio n. 2
0
 def test_mutate_elision(self):
     from parser import source2ast, ASTConst, ASTList
     class ConstRemover(self.BaseVisitor):
         def visitConst(self, const):
             return None
     listast = source2ast("[1, 2]")
     listast = listast.mutate(ConstRemover())
     listnode = listast.node.nodes[0].expr
     assert isinstance(listnode, ASTList)
     assert len(listnode.nodes) == 0
Esempio n. 3
0
    def test_mutate_strange_cases(self):
        src = '''
if a:
    b
        '''
        ast = source2ast(src, 'exec')
        ast.mutate(BaseVisitor())
        src = '''
try:
    b
except Exception:
    pass
        '''
        ast = source2ast(src, 'exec')
        ast.mutate(BaseVisitor())
        src = '{1:2}'
        ast = source2ast(src, 'exec')
        ast.mutate(BaseVisitor())
        src = '1 > 3'
        ast = source2ast(src, 'exec')
        ast.mutate(BaseVisitor())
Esempio n. 4
0
def compile_with_astcompiler(expr, mode, space):
    ast = source2ast(expr, mode, space)
    misc.set_filename("<testing>", ast)
    ast = opt.optimize_ast_tree(space, ast)
    if mode == "exec":
        Generator = pycodegen.ModuleCodeGenerator
    elif mode == "single":
        Generator = pycodegen.InteractiveCodeGenerator
    elif mode == "eval":
        Generator = pycodegen.ExpressionCodeGenerator
    codegen = Generator(space, ast)
    rcode = codegen.getCode()
    assert isinstance(rcode, PyCode)
    assert rcode.co_filename == "<testing>"
    return rcode