def fromText(src, modname='<test>', system=None): if system is None: _system = System() else: _system = system processModuleAst(parse(src), modname, _system) if system is None: _system.finalStateComputations() return _system.rootobjects[0]
def testMultipleLHS(self): """ Test multiple targets on the left hand side. """ snippets = ['a, b = 1, 2', '(a, b) = 1, 2', '((a, b), c) = (1, 2), 3'] for s in snippets: a = transformer.parse(s) assert isinstance(a, ast.Module) child1 = a.getChildNodes()[0] assert isinstance(child1, ast.Stmt) child2 = child1.getChildNodes()[0] assert isinstance(child2, ast.Assign) # This actually tests the compiler, but it's a way to assure the ast # is correct c = compile(s, '<string>', 'single') vals = {} exec c in vals assert vals['a'] == 1 assert vals['b'] == 2
# Embedded file name: scripts/common/Lib/compiler/__init__.py """Package for parsing and compiling Python source code There are several functions defined at the top level that are imported from modules contained in the package. parse(buf, mode="exec") -> AST Converts a string containing Python source code to an abstract syntax tree (AST). The AST is defined in compiler.ast. parseFile(path) -> AST The same as parse(open(path)) walk(ast, visitor, verbose=None) Does a pre-order walk over the ast using the visitor instance. See compiler.visitor for details. compile(source, filename, mode, flags=None, dont_inherit=None) Returns a code object. A replacement for the builtin compile() function. compileFile(filename) Generates a .pyc file by compiling filename. """ import warnings warnings.warn('The compiler package is deprecated and removed in Python 3.x.', DeprecationWarning, stacklevel=2) from compiler.transformer import parse, parseFile from compiler.visitor import walk from compiler.pycodegen import compile, compileFile
"""Package for parsing and compiling Python source code There are several functions defined at the top level that are imported from modules contained in the package. parse(buf, mode="exec") -> AST Converts a string containing Python source code to an abstract syntax tree (AST). The AST is defined in compiler.ast. parseFile(path) -> AST The same as parse(open(path)) walk(ast, visitor, verbose=None) Does a pre-order walk over the ast using the visitor instance. See compiler.visitor for details. compile(source, filename, mode, flags=None, dont_inherit=None) Returns a code object. A replacement for the builtin compile() function. compileFile(filename) Generates a .pyc file by compiling filename. """ import warnings warnings.warn("The compiler package is deprecated and removed in Python 3.x.", DeprecationWarning, stacklevel=2) from compiler.transformer import parse, parseFile from compiler.visitor import walk
def testSymbolVisitor(): def PrintScopes(scopes): result = [] for i in sorted(scopes, key=lambda x:x.name): result.append('- %s' % i) for j in sorted(i.uses): result.append(' - %s' % j) return '\n'.join(result) source_code = Dedent( ''' from alpha import Alpha import coilib50 class Zulu(Alpha): """ Zulu class docs. """ def __init__(self, name): """ Zulu.__init__ docs. """ self._name = name alpha = bravo coilib50.Charlie() f = coilib50.Delta(echo, foxtrot) ''' ) from terraformer._visitor import ASTVisitor code = TerraFormer._Parse(source_code) visitor = ASTVisitor() visitor.Visit(code) assert visitor._module.AsString() == Dedent( ''' module (1, 0) module IMPORT-BLOCK (1, 0) import-block #0 IMPORT-FROM (0, 0) alpha IMPORT (1, 0) alpha.Alpha IMPORT (2, 0) coilib50 USE (3, 0) Alpha class (0, 0) Zulu def (8, 4) __init__ ARG (9, 17) self ARG (9, 23) name DEF (13, 8) self._name USE (13, 21) name DEF (14, 8) alpha USE (14, 16) bravo USE (15, 8) coilib50.Charlie DEF (16, 8) f USE (16, 12) coilib50.Delta USE (16, 27) echo USE (16, 33) foxtrot ''' ) # Compares the results with the one given by compiler.symbols.SymbolVisitor, our inspiration. from compiler.symbols import SymbolVisitor from compiler.transformer import parse from compiler.visitor import walk code = parse(source_code) symbol_visitor = SymbolVisitor() walk(code, symbol_visitor) assert PrintScopes(symbol_visitor.scopes.values()) == Dedent( ''' - <ClassScope: Zulu> - <FunctionScope: __init__> - bravo - coilib50 - echo - foxtrot - name - self - <ModuleScope: global> - Alpha ''' )
def pp_test(source): assert ast_pp.pp(parse(source)) == source
def testSymbolVisitor(): def PrintScopes(scopes): result = [] for i in sorted(scopes, key=lambda x:x.name): result.append('- %s' % i) for j in sorted(i.uses): result.append(' - %s' % j) return '\n'.join(result) source_code = dedent( ''' from alpha import Alpha import coilib50 class Zulu(Alpha): """ Zulu class docs. """ def __init__(self, name): """ Zulu.__init__ docs. """ self._name = name alpha = bravo coilib50.Charlie() f = coilib50.Delta(echo, foxtrot) ''' ) from zerotk.terraformer._visitor import ASTVisitor code = TerraFormer._Parse(source_code) visitor = ASTVisitor() visitor.Visit(code) assert visitor._module.AsString() == dedent( ''' module (1, 0) module IMPORT-BLOCK (1, 0) import-block #0 IMPORT-FROM (0, 0) alpha IMPORT (1, 0) alpha.Alpha IMPORT (2, 0) coilib50 USE (3, 0) Alpha class (0, 0) Zulu def (8, 4) __init__ ARG (9, 17) self ARG (9, 23) name DEF (13, 8) self._name USE (13, 21) name DEF (14, 8) alpha USE (14, 16) bravo USE (15, 8) coilib50.Charlie DEF (16, 8) f USE (16, 12) coilib50.Delta USE (16, 27) echo USE (16, 33) foxtrot ''' ) # Compares the results with the one given by compiler.symbols.SymbolVisitor, our inspiration. from compiler.symbols import SymbolVisitor from compiler.transformer import parse from compiler.visitor import walk code = parse(source_code) symbol_visitor = SymbolVisitor() walk(code, symbol_visitor) assert PrintScopes(symbol_visitor.scopes.values()) == dedent( ''' - <ClassScope: Zulu> - <FunctionScope: __init__> - bravo - coilib50 - echo - foxtrot - name - self - <ModuleScope: global> - Alpha ''' )