<s> import imp <EOL> import os <EOL> import marshal <EOL> import struct <EOL> import sys <EOL> from cStringIO import StringIO <EOL> is_jython = sys . platform . startswith ( '<STR_LIT>' ) <EOL> from compiler import ast , parse , walk , syntax <EOL> from compiler import misc , future , symbols <EOL> from compiler . consts import SC_LOCAL , SC_GLOBAL , SC_FREE , SC_CELL <EOL> from compiler . consts import ( CO_VARARGS , CO_VARKEYWORDS , CO_NEWLOCALS , <EOL> CO_NESTED , CO_GENERATOR , CO_FUTURE_DIVISION , <EOL> CO_FUTURE_ABSIMPORT , CO_FUTURE_WITH_STATEMENT ) <EOL> if not is_jython : <EOL> from compiler . pyassem import TupleArg <EOL> else : <EOL> TupleArg = None <EOL> try : <EOL> VERSION = sys . version_info [ <NUM_LIT:0> ] <EOL> except AttributeError : <EOL> VERSION = <NUM_LIT:1> <EOL> callfunc_opcode_info = { <EOL> ( <NUM_LIT:0> , <NUM_LIT:0> ) : "<STR_LIT>" , <EOL> ( <NUM_LIT:1> , <NUM_LIT:0> ) : "<STR_LIT>" , <EOL> ( <NUM_LIT:0> , <NUM_LIT:1> ) : "<STR_LIT>" , <EOL> ( <NUM_LIT:1> , <NUM_LIT:1> ) : "<STR_LIT>" , <EOL> } <EOL> LOOP = <NUM_LIT:1> <EOL> EXCEPT = <NUM_LIT:2> <EOL> TRY_FINALLY = <NUM_LIT:3> <EOL> END_FINALLY = <NUM_LIT:4> <EOL> def compileFile ( filename , display = <NUM_LIT:0> ) : <EOL> f = open ( filename , '<STR_LIT>' ) <EOL> buf = f . read ( ) <EOL> f . close ( ) <EOL> mod = Module ( buf , filename ) <EOL> try : <EOL> mod . compile ( display ) <EOL> except SyntaxError : <EOL> raise <EOL> else : <EOL> f = open ( filename + "<STR_LIT:c>" , "<STR_LIT:wb>" ) <EOL> mod . dump ( f ) <EOL> f . close ( ) <EOL> if is_jython : <EOL> compile = compile <EOL> else : <EOL> def compile ( source , filename , mode , flags = None , dont_inherit = None ) : <EOL> """<STR_LIT>""" <EOL> if flags is not None or dont_inherit is not None : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> if mode == "<STR_LIT>" : <EOL> gen = Interactive ( source , filename ) <EOL> elif mode == "<STR_LIT>" : <EOL> gen = Module ( source , filename ) <EOL> elif mode == "<STR_LIT>" : <EOL> gen = Expression ( source , filename ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> gen . compile ( ) <EOL> return gen . code <EOL> class AbstractCompileMode : <EOL> mode = None <EOL> def __init__ ( self , source , filename ) : <EOL> self . source = source <EOL> self . filename = filename <EOL> self . code = None <EOL> def _get_tree ( self ) : <EOL> tree = parse ( self . source , self . mode ) <EOL> misc . set_filename ( self . filename , tree ) <EOL> syntax . check ( tree ) <EOL> return tree <EOL> def compile ( self ) : <EOL> pass <EOL> def getCode ( self ) : <EOL> return self . code <EOL> class Expression ( AbstractCompileMode ) : <EOL> mode = "<STR_LIT>" <EOL> def compile ( self ) : <EOL> tree = self . _get_tree ( ) <EOL> gen = ExpressionCodeGenerator ( tree ) <EOL> self . code = gen . getCode ( ) <EOL> class Interactive ( AbstractCompileMode ) : <EOL> mode = "<STR_LIT>" <EOL> def compile ( self ) : <EOL> tree = self . _get_tree ( ) <EOL> gen = InteractiveCodeGenerator ( tree ) <EOL> self . code = gen . getCode ( ) <EOL> class Module ( AbstractCompileMode ) : <EOL> mode = "<STR_LIT>" <EOL> def compile ( self , display = <NUM_LIT:0> ) : <EOL> tree = self . _get_tree ( ) <EOL> gen = ModuleCodeGenerator ( tree ) <EOL> if display : <EOL> import pprint <EOL> print pprint . pprint ( tree ) <EOL> self . code = gen . getCode ( ) <EOL> def dump ( self , f ) : <EOL> f . write ( self . getPycHeader ( ) ) <EOL> marshal . dump ( self . code , f ) <EOL> MAGIC = None if is_jython else imp . get_magic ( ) <EOL> def getPycHeader ( self ) : <EOL> mtime = os . path . getmtime ( self . filename ) <EOL> mtime = struct . pack ( '<STR_LIT>' , mtime ) <EOL> return self . MAGIC + mtime <EOL> class LocalNameFinder : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , names = ( ) ) : <EOL> self . names = misc . Set ( ) <EOL> self . globals = misc . Set ( ) <EOL> for name in names : <EOL> self . names . add ( name ) <EOL> def getLocals ( self ) : <EOL> for elt in self . globals . elements ( ) : <EOL> if self . names . has_elt ( elt ) : <EOL> self . names . remove ( elt ) <EOL> return self . names <EOL> def visitDict ( self , node ) : <EOL> pass <EOL> def visitGlobal ( self , node ) : <EOL> for name in node . names : <EOL> self . globals . add ( name ) <EOL> def visitFunction ( self , node ) : <EOL> self . names . add ( node . name ) <EOL> def visitLambda ( self , node ) : <EOL> pass <EOL> def visitImport ( self , node ) : <EOL> for name , alias in node . names : <EOL> self . names . add ( alias or name ) <EOL> def visitFrom ( self , node ) : <EOL> for name , alias in node . names : <EOL> self . names . add ( alias or name ) <EOL> def visitClass ( self , node ) : <EOL> self . names . add ( node . name ) <EOL> def visitAssName ( self , node ) : <EOL> self . names . add ( node . name ) <EOL> def is_constant_false ( node ) : <EOL> if isinstance ( node , ast . Const ) : <EOL> if not node . value : <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> class CodeGenerator : <EOL> """<STR_LIT>""" <EOL> optimized = <NUM_LIT:0> <EOL> __initialized = None <EOL> class_name = None <EOL> def __init__ ( self ) : <EOL> if self . __initialized is None : <EOL> self . initClass ( ) <EOL> self . __class__ . __initialized = <NUM_LIT:1> <EOL> self . checkClass ( ) <EOL> self . locals = misc . Stack ( ) <EOL> self . setups = misc . Stack ( ) <EOL> self . last_lineno = None <EOL> self . _setupGraphDelegation ( ) <EOL> self . _div_op = "<STR_LIT>" <EOL> futures = self . get_module ( ) . futures <EOL> for feature in futures : <EOL> if feature == "<STR_LIT>" : <EOL> self . graph . setFlag ( CO_FUTURE_DIVISION ) <EOL> self . _div_op = "<STR_LIT>" <EOL> elif feature == "<STR_LIT>" : <EOL> self . graph . setFlag ( CO_FUTURE_ABSIMPORT ) <EOL> elif feature == "<STR_LIT>" : <EOL> self . graph . setFlag ( CO_FUTURE_WITH_STATEMENT ) <EOL> def initClass ( self ) : <EOL> """<STR_LIT>""" <EOL> def checkClass ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> assert hasattr ( self , '<STR_LIT>' ) <EOL> assert getattr ( self , '<STR_LIT>' ) <EOL> assert getattr ( self , '<STR_LIT>' ) <EOL> assert getattr ( self , '<STR_LIT>' ) <EOL> except AssertionError , msg : <EOL> intro = "<STR_LIT>" % self . __class__ . __name__ <EOL> raise AssertionError , intro <EOL> def _setupGraphDelegation ( self ) : <EOL> self . emit = self . graph . emit <EOL> self . newBlock = self . graph . newBlock <EOL> self . startBlock = self . graph . startBlock <EOL> self . nextBlock = self . graph . nextBlock <EOL> self . setDocstring = self . graph . setDocstring <EOL> def getCode ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . graph . getCode ( ) <EOL> def mangle ( self , name ) : <EOL> if self . class_name is not None : <EOL> return misc . mangle ( name , self . class_name ) <EOL> else : <EOL> return name <EOL> def parseSymbols ( self , tree ) : <EOL> s = symbols . SymbolVisitor ( ) <EOL> walk ( tree , s ) <EOL> return s . scopes <EOL> def get_module ( self ) : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> def isLocalName ( self , name ) : <EOL> return self . locals . top ( ) . has_elt ( name ) <EOL> def storeName ( self , name ) : <EOL> self . _nameOp ( '<STR_LIT>' , name ) <EOL> def loadName ( self , name ) : <EOL> self . _nameOp ( '<STR_LIT>' , name ) <EOL> def delName ( self , name ) : <EOL> self . _nameOp ( '<STR_LIT>' , name ) <EOL> def _nameOp ( self , prefix , name ) : <EOL> name = self . mangle ( name ) <EOL> scope = self . scope . check_name ( name ) <EOL> if scope == SC_LOCAL : <EOL> if not self . optimized : <EOL> self . emit ( prefix + '<STR_LIT>' , name ) <EOL> else : <EOL> self . emit ( prefix + '<STR_LIT>' , name ) <EOL> elif scope == SC_GLOBAL : <EOL> if not self . optimized : <EOL> self . emit ( prefix + '<STR_LIT>' , name ) <EOL> else : <EOL> self . emit ( prefix + '<STR_LIT>' , name ) <EOL> elif scope == SC_FREE or scope == SC_CELL : <EOL> self . emit ( prefix + '<STR_LIT>' , name ) <EOL> else : <EOL> raise RuntimeError , "<STR_LIT>" % ( name , scope ) <EOL> def _implicitNameOp ( self , prefix , name ) : <EOL> """<STR_LIT>""" <EOL> if self . optimized : <EOL> self . emit ( prefix + '<STR_LIT>' , name ) <EOL> else : <EOL> self . emit ( prefix + '<STR_LIT>' , name ) <EOL> def set_lineno ( self , node , force = False ) : <EOL> """<STR_LIT>""" <EOL> lineno = getattr ( node , '<STR_LIT>' , None ) <EOL> if lineno is not None and ( lineno != self . last_lineno <EOL> or force ) : <EOL> self . emit ( '<STR_LIT>' , lineno ) <EOL> self . last_lineno = lineno <EOL> return True <EOL> return False <EOL> NameFinder = LocalNameFinder <EOL> FunctionGen = None <EOL> ClassGen = None <EOL> def visitModule ( self , node ) : <EOL> self . scopes = self . parseSymbols ( node ) <EOL> self . scope = self . scopes [ node ] <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if node . doc : <EOL> self . emit ( '<STR_LIT>' , node . doc ) <EOL> self . storeName ( '<STR_LIT>' ) <EOL> lnf = walk ( node . node , self . NameFinder ( ) , verbose = <NUM_LIT:0> ) <EOL> self . locals . push ( lnf . getLocals ( ) ) <EOL> self . visit ( node . node ) <EOL> self . emit ( '<STR_LIT>' , None ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def visitExpression ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> self . scopes = self . parseSymbols ( node ) <EOL> self . scope = self . scopes [ node ] <EOL> self . visit ( node . node ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def visitFunction ( self , node ) : <EOL> self . _visitFuncOrLambda ( node , isLambda = <NUM_LIT:0> ) <EOL> if node . doc : <EOL> self . setDocstring ( node . doc ) <EOL> self . storeName ( node . name ) <EOL> def visitLambda ( self , node ) : <EOL> self . _visitFuncOrLambda ( node , isLambda = <NUM_LIT:1> ) <EOL> def _visitFuncOrLambda ( self , node , isLambda = <NUM_LIT:0> ) : <EOL> if not isLambda and node . decorators : <EOL> for decorator in node . decorators . nodes : <EOL> self . visit ( decorator ) <EOL> ndecorators = len ( node . decorators . nodes ) <EOL> else : <EOL> ndecorators = <NUM_LIT:0> <EOL> gen = self . FunctionGen ( node , self . scopes , isLambda , <EOL> self . class_name , self . get_module ( ) ) <EOL> walk ( node . code , gen ) <EOL> gen . finish ( ) <EOL> self . set_lineno ( node ) <EOL> for default in node . defaults : <EOL> self . visit ( default ) <EOL> self . _makeClosure ( gen , len ( node . defaults ) ) <EOL> for i in range ( ndecorators ) : <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> def visitClass ( self , node ) : <EOL> gen = self . ClassGen ( node , self . scopes , <EOL> self . get_module ( ) ) <EOL> walk ( node . code , gen ) <EOL> gen . finish ( ) <EOL> self . set_lineno ( node ) <EOL> self . emit ( '<STR_LIT>' , node . name ) <EOL> for base in node . bases : <EOL> self . visit ( base ) <EOL> self . emit ( '<STR_LIT>' , len ( node . bases ) ) <EOL> self . _makeClosure ( gen , <NUM_LIT:0> ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . storeName ( node . name ) <EOL> def visitIf ( self , node ) : <EOL> end = self . newBlock ( ) <EOL> numtests = len ( node . tests ) <EOL> for i in range ( numtests ) : <EOL> test , suite = node . tests [ i ] <EOL> if is_constant_false ( test ) : <EOL> continue <EOL> self . set_lineno ( test ) <EOL> self . visit ( test ) <EOL> nextTest = self . newBlock ( ) <EOL> self . emit ( '<STR_LIT>' , nextTest ) <EOL> self . nextBlock ( ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . visit ( suite ) <EOL> self . emit ( '<STR_LIT>' , end ) <EOL> self . startBlock ( nextTest ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> if node . else_ : <EOL> self . visit ( node . else_ ) <EOL> self . nextBlock ( end ) <EOL> def visitWhile ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> loop = self . newBlock ( ) <EOL> else_ = self . newBlock ( ) <EOL> after = self . newBlock ( ) <EOL> self . emit ( '<STR_LIT>' , after ) <EOL> self . nextBlock ( loop ) <EOL> self . setups . push ( ( LOOP , loop ) ) <EOL> self . set_lineno ( node , force = True ) <EOL> self . visit ( node . test ) <EOL> self . emit ( '<STR_LIT>' , else_ or after ) <EOL> self . nextBlock ( ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . visit ( node . body ) <EOL> self . emit ( '<STR_LIT>' , loop ) <EOL> self . startBlock ( else_ ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . setups . pop ( ) <EOL> if node . else_ : <EOL> self . visit ( node . else_ ) <EOL> self . nextBlock ( after ) <EOL> def visitFor ( self , node ) : <EOL> start = self . newBlock ( ) <EOL> anchor = self . newBlock ( ) <EOL> after = self . newBlock ( ) <EOL> self . setups . push ( ( LOOP , start ) ) <EOL> self . set_lineno ( node ) <EOL> self . emit ( '<STR_LIT>' , after ) <EOL> self . visit ( node . list ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . nextBlock ( start ) <EOL> self . set_lineno ( node , force = <NUM_LIT:1> ) <EOL> self . emit ( '<STR_LIT>' , anchor ) <EOL> self . visit ( node . assign ) <EOL> self . visit ( node . body ) <EOL> self . emit ( '<STR_LIT>' , start ) <EOL> self . nextBlock ( anchor ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . setups . pop ( ) <EOL> if node . else_ : <EOL> self . visit ( node . else_ ) <EOL> self . nextBlock ( after ) <EOL> def visitBreak ( self , node ) : <EOL> if not self . setups : <EOL> raise SyntaxError , "<STR_LIT>" % ( node . filename , node . lineno ) <EOL> self . set_lineno ( node ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def visitContinue ( self , node ) : <EOL> if not self . setups : <EOL> raise SyntaxError , "<STR_LIT>" % ( node . filename , node . lineno ) <EOL> kind , block = self . setups . top ( ) <EOL> if kind == LOOP : <EOL> self . set_lineno ( node ) <EOL> self . emit ( '<STR_LIT>' , block ) <EOL> self . nextBlock ( ) <EOL> elif kind == EXCEPT or kind == TRY_FINALLY : <EOL> self . set_lineno ( node ) <EOL> top = len ( self . setups ) <EOL> while top > <NUM_LIT:0> : <EOL> top = top - <NUM_LIT:1> <EOL> kind , loop_block = self . setups [ top ] <EOL> if kind == LOOP : <EOL> break <EOL> if kind != LOOP : <EOL> raise SyntaxError , "<STR_LIT>" % ( node . filename , node . lineno ) <EOL> self . emit ( '<STR_LIT>' , loop_block ) <EOL> self . nextBlock ( ) <EOL> elif kind == END_FINALLY : <EOL> msg = "<STR_LIT>" <EOL> raise SyntaxError , msg % ( node . filename , node . lineno ) <EOL> def visitTest ( self , node , jump ) : <EOL> end = self . newBlock ( ) <EOL> for child in node . nodes [ : - <NUM_LIT:1> ] : <EOL> self . visit ( child ) <EOL> self . emit ( jump , end ) <EOL> self . nextBlock ( ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . visit ( node . nodes [ - <NUM_LIT:1> ] ) <EOL> self . nextBlock ( end ) <EOL> def visitAnd ( self , node ) : <EOL> self . visitTest ( node , '<STR_LIT>' ) <EOL> def visitOr ( self , node ) : <EOL> self . visitTest ( node , '<STR_LIT>' ) <EOL> def visitIfExp ( self , node ) : <EOL> endblock = self . newBlock ( ) <EOL> elseblock = self . newBlock ( ) <EOL> self . visit ( node . test ) <EOL> self . emit ( '<STR_LIT>' , elseblock ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . visit ( node . then ) <EOL> self . emit ( '<STR_LIT>' , endblock ) <EOL> self . nextBlock ( elseblock ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . visit ( node . else_ ) <EOL> self . nextBlock ( endblock ) <EOL> def visitCompare ( self , node ) : <EOL> self . visit ( node . expr ) <EOL> cleanup = self . newBlock ( ) <EOL> for op , code in node . ops [ : - <NUM_LIT:1> ] : <EOL> self . visit ( code ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' , op ) <EOL> self . emit ( '<STR_LIT>' , cleanup ) <EOL> self . nextBlock ( ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> if node . ops : <EOL> op , code = node . ops [ - <NUM_LIT:1> ] <EOL> self . visit ( code ) <EOL> self . emit ( '<STR_LIT>' , op ) <EOL> if len ( node . ops ) > <NUM_LIT:1> : <EOL> end = self . newBlock ( ) <EOL> self . emit ( '<STR_LIT>' , end ) <EOL> self . startBlock ( cleanup ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . nextBlock ( end ) <EOL> __list_count = <NUM_LIT:0> <EOL> def visitListComp ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> append = "<STR_LIT>" % self . __list_count <EOL> self . __list_count = self . __list_count + <NUM_LIT:1> <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . _implicitNameOp ( '<STR_LIT>' , append ) <EOL> stack = [ ] <EOL> for i , for_ in zip ( range ( len ( node . quals ) ) , node . quals ) : <EOL> start , anchor = self . visit ( for_ ) <EOL> cont = None <EOL> for if_ in for_ . ifs : <EOL> if cont is None : <EOL> cont = self . newBlock ( ) <EOL> self . visit ( if_ , cont ) <EOL> stack . insert ( <NUM_LIT:0> , ( start , cont , anchor ) ) <EOL> self . _implicitNameOp ( '<STR_LIT>' , append ) <EOL> self . visit ( node . expr ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> for start , cont , anchor in stack : <EOL> if cont : <EOL> skip_one = self . newBlock ( ) <EOL> self . emit ( '<STR_LIT>' , skip_one ) <EOL> self . startBlock ( cont ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . nextBlock ( skip_one ) <EOL> self . emit ( '<STR_LIT>' , start ) <EOL> self . startBlock ( anchor ) <EOL> self . _implicitNameOp ( '<STR_LIT>' , append ) <EOL> self . __list_count = self . __list_count - <NUM_LIT:1> <EOL> def visitListCompFor ( self , node ) : <EOL> start = self . newBlock ( ) <EOL> anchor = self . newBlock ( ) <EOL> self . visit ( node . list ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . nextBlock ( start ) <EOL> self . set_lineno ( node , force = True ) <EOL> self . emit ( '<STR_LIT>' , anchor ) <EOL> self . nextBlock ( ) <EOL> self . visit ( node . assign ) <EOL> return start , anchor <EOL> def visitListCompIf ( self , node , branch ) : <EOL> self . set_lineno ( node , force = True ) <EOL> self . visit ( node . test ) <EOL> self . emit ( '<STR_LIT>' , branch ) <EOL> self . newBlock ( ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def _makeClosure ( self , gen , args ) : <EOL> frees = gen . scope . get_free_vars ( ) <EOL> if frees : <EOL> for name in frees : <EOL> self . emit ( '<STR_LIT>' , name ) <EOL> self . emit ( '<STR_LIT>' , len ( frees ) ) <EOL> self . emit ( '<STR_LIT>' , gen ) <EOL> self . emit ( '<STR_LIT>' , args ) <EOL> else : <EOL> self . emit ( '<STR_LIT>' , gen ) <EOL> self . emit ( '<STR_LIT>' , args ) <EOL> def visitGenExpr ( self , node ) : <EOL> gen = GenExprCodeGenerator ( node , self . scopes , self . class_name , <EOL> self . get_module ( ) ) <EOL> walk ( node . code , gen ) <EOL> gen . finish ( ) <EOL> self . set_lineno ( node ) <EOL> self . _makeClosure ( gen , <NUM_LIT:0> ) <EOL> self . visit ( node . code . quals [ <NUM_LIT:0> ] . iter ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> def visitGenExprInner ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> stack = [ ] <EOL> for i , for_ in zip ( range ( len ( node . quals ) ) , node . quals ) : <EOL> start , anchor , end = self . visit ( for_ ) <EOL> cont = None <EOL> for if_ in for_ . ifs : <EOL> if cont is None : <EOL> cont = self . newBlock ( ) <EOL> self . visit ( if_ , cont ) <EOL> stack . insert ( <NUM_LIT:0> , ( start , cont , anchor , end ) ) <EOL> self . visit ( node . expr ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> for start , cont , anchor , end in stack : <EOL> if cont : <EOL> skip_one = self . newBlock ( ) <EOL> self . emit ( '<STR_LIT>' , skip_one ) <EOL> self . startBlock ( cont ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . nextBlock ( skip_one ) <EOL> self . emit ( '<STR_LIT>' , start ) <EOL> self . startBlock ( anchor ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . setups . pop ( ) <EOL> self . startBlock ( end ) <EOL> self . emit ( '<STR_LIT>' , None ) <EOL> def visitGenExprFor ( self , node ) : <EOL> start = self . newBlock ( ) <EOL> anchor = self . newBlock ( ) <EOL> end = self . newBlock ( ) <EOL> self . setups . push ( ( LOOP , start ) ) <EOL> self . emit ( '<STR_LIT>' , end ) <EOL> if node . is_outmost : <EOL> self . loadName ( '<STR_LIT>' ) <EOL> else : <EOL> self . visit ( node . iter ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . nextBlock ( start ) <EOL> self . set_lineno ( node , force = True ) <EOL> self . emit ( '<STR_LIT>' , anchor ) <EOL> self . nextBlock ( ) <EOL> self . visit ( node . assign ) <EOL> return start , anchor , end <EOL> def visitGenExprIf ( self , node , branch ) : <EOL> self . set_lineno ( node , force = True ) <EOL> self . visit ( node . test ) <EOL> self . emit ( '<STR_LIT>' , branch ) <EOL> self . newBlock ( ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def visitAssert ( self , node ) : <EOL> if __debug__ : <EOL> end = self . newBlock ( ) <EOL> self . set_lineno ( node ) <EOL> self . nextBlock ( ) <EOL> self . visit ( node . test ) <EOL> self . emit ( '<STR_LIT>' , end ) <EOL> self . nextBlock ( ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if node . fail : <EOL> self . visit ( node . fail ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> else : <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . nextBlock ( end ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def visitRaise ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> n = <NUM_LIT:0> <EOL> if node . expr1 : <EOL> self . visit ( node . expr1 ) <EOL> n = n + <NUM_LIT:1> <EOL> if node . expr2 : <EOL> self . visit ( node . expr2 ) <EOL> n = n + <NUM_LIT:1> <EOL> if node . expr3 : <EOL> self . visit ( node . expr3 ) <EOL> n = n + <NUM_LIT:1> <EOL> self . emit ( '<STR_LIT>' , n ) <EOL> def visitTryExcept ( self , node ) : <EOL> body = self . newBlock ( ) <EOL> handlers = self . newBlock ( ) <EOL> end = self . newBlock ( ) <EOL> if node . else_ : <EOL> lElse = self . newBlock ( ) <EOL> else : <EOL> lElse = end <EOL> self . set_lineno ( node ) <EOL> self . emit ( '<STR_LIT>' , handlers ) <EOL> self . nextBlock ( body ) <EOL> self . setups . push ( ( EXCEPT , body ) ) <EOL> self . visit ( node . body ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . setups . pop ( ) <EOL> self . emit ( '<STR_LIT>' , lElse ) <EOL> self . startBlock ( handlers ) <EOL> last = len ( node . handlers ) - <NUM_LIT:1> <EOL> for i in range ( len ( node . handlers ) ) : <EOL> expr , target , body = node . handlers [ i ] <EOL> self . set_lineno ( expr ) <EOL> if expr : <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . visit ( expr ) <EOL> self . emit ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> next = self . newBlock ( ) <EOL> self . emit ( '<STR_LIT>' , next ) <EOL> self . nextBlock ( ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> if target : <EOL> self . visit ( target ) <EOL> else : <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . visit ( body ) <EOL> self . emit ( '<STR_LIT>' , end ) <EOL> if expr : <EOL> self . nextBlock ( next ) <EOL> else : <EOL> self . nextBlock ( ) <EOL> if expr : <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> if node . else_ : <EOL> self . nextBlock ( lElse ) <EOL> self . visit ( node . else_ ) <EOL> self . nextBlock ( end ) <EOL> def visitTryFinally ( self , node ) : <EOL> body = self . newBlock ( ) <EOL> final = self . newBlock ( ) <EOL> self . set_lineno ( node ) <EOL> self . emit ( '<STR_LIT>' , final ) <EOL> self . nextBlock ( body ) <EOL> self . setups . push ( ( TRY_FINALLY , body ) ) <EOL> self . visit ( node . body ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . setups . pop ( ) <EOL> self . emit ( '<STR_LIT>' , None ) <EOL> self . nextBlock ( final ) <EOL> self . setups . push ( ( END_FINALLY , final ) ) <EOL> self . visit ( node . final ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . setups . pop ( ) <EOL> __with_count = <NUM_LIT:0> <EOL> def visitWith ( self , node ) : <EOL> body = self . newBlock ( ) <EOL> final = self . newBlock ( ) <EOL> exitvar = "<STR_LIT>" % self . __with_count <EOL> valuevar = "<STR_LIT>" % self . __with_count <EOL> self . __with_count += <NUM_LIT:1> <EOL> self . set_lineno ( node ) <EOL> self . visit ( node . expr ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . _implicitNameOp ( '<STR_LIT>' , exitvar ) <EOL> self . emit ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if node . vars is None : <EOL> self . emit ( '<STR_LIT>' ) <EOL> else : <EOL> self . _implicitNameOp ( '<STR_LIT>' , valuevar ) <EOL> self . emit ( '<STR_LIT>' , final ) <EOL> self . nextBlock ( body ) <EOL> self . setups . push ( ( TRY_FINALLY , body ) ) <EOL> if node . vars is not None : <EOL> self . _implicitNameOp ( '<STR_LIT>' , valuevar ) <EOL> self . _implicitNameOp ( '<STR_LIT>' , valuevar ) <EOL> self . visit ( node . vars ) <EOL> self . visit ( node . body ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . setups . pop ( ) <EOL> self . emit ( '<STR_LIT>' , None ) <EOL> self . nextBlock ( final ) <EOL> self . setups . push ( ( END_FINALLY , final ) ) <EOL> self . _implicitNameOp ( '<STR_LIT>' , exitvar ) <EOL> self . _implicitNameOp ( '<STR_LIT>' , exitvar ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . setups . pop ( ) <EOL> self . __with_count -= <NUM_LIT:1> <EOL> def visitDiscard ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> self . visit ( node . expr ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def visitConst ( self , node ) : <EOL> self . emit ( '<STR_LIT>' , node . value ) <EOL> def visitKeyword ( self , node ) : <EOL> self . emit ( '<STR_LIT>' , node . name ) <EOL> self . visit ( node . expr ) <EOL> def visitGlobal ( self , node ) : <EOL> pass <EOL> def visitName ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> self . loadName ( node . name ) <EOL> def visitPass ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> def visitImport ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> level = <NUM_LIT:0> if self . graph . checkFlag ( CO_FUTURE_ABSIMPORT ) else - <NUM_LIT:1> <EOL> for name , alias in node . names : <EOL> if VERSION > <NUM_LIT:1> : <EOL> self . emit ( '<STR_LIT>' , level ) <EOL> self . emit ( '<STR_LIT>' , None ) <EOL> self . emit ( '<STR_LIT>' , name ) <EOL> mod = name . split ( "<STR_LIT:.>" ) [ <NUM_LIT:0> ] <EOL> if alias : <EOL> self . _resolveDots ( name ) <EOL> self . storeName ( alias ) <EOL> else : <EOL> self . storeName ( mod ) <EOL> def visitFrom ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> level = node . level <EOL> if level == <NUM_LIT:0> and not self . graph . checkFlag ( CO_FUTURE_ABSIMPORT ) : <EOL> level = - <NUM_LIT:1> <EOL> fromlist = map ( lambda ( name , alias ) : name , node . names ) <EOL> if VERSION > <NUM_LIT:1> : <EOL> self . emit ( '<STR_LIT>' , level ) <EOL> self . emit ( '<STR_LIT>' , tuple ( fromlist ) ) <EOL> self . emit ( '<STR_LIT>' , node . modname ) <EOL> for name , alias in node . names : <EOL> if VERSION > <NUM_LIT:1> : <EOL> if name == '<STR_LIT:*>' : <EOL> self . namespace = <NUM_LIT:0> <EOL> self . emit ( '<STR_LIT>' ) <EOL> assert len ( node . names ) == <NUM_LIT:1> <EOL> return <EOL> else : <EOL> self . emit ( '<STR_LIT>' , name ) <EOL> self . _resolveDots ( name ) <EOL> self . storeName ( alias or name ) <EOL> else : <EOL> self . emit ( '<STR_LIT>' , name ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def _resolveDots ( self , name ) : <EOL> elts = name . split ( "<STR_LIT:.>" ) <EOL> if len ( elts ) == <NUM_LIT:1> : <EOL> return <EOL> for elt in elts [ <NUM_LIT:1> : ] : <EOL> self . emit ( '<STR_LIT>' , elt ) <EOL> def visitGetattr ( self , node ) : <EOL> self . visit ( node . expr ) <EOL> self . emit ( '<STR_LIT>' , self . mangle ( node . attrname ) ) <EOL> def visitAssign ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> self . visit ( node . expr ) <EOL> dups = len ( node . nodes ) - <NUM_LIT:1> <EOL> for i in range ( len ( node . nodes ) ) : <EOL> elt = node . nodes [ i ] <EOL> if i < dups : <EOL> self . emit ( '<STR_LIT>' ) <EOL> if isinstance ( elt , ast . Node ) : <EOL> self . visit ( elt ) <EOL> def visitAssName ( self , node ) : <EOL> if node . flags == '<STR_LIT>' : <EOL> self . storeName ( node . name ) <EOL> elif node . flags == '<STR_LIT>' : <EOL> self . set_lineno ( node ) <EOL> self . delName ( node . name ) <EOL> else : <EOL> print "<STR_LIT>" , node . flags <EOL> def visitAssAttr ( self , node ) : <EOL> self . visit ( node . expr ) <EOL> if node . flags == '<STR_LIT>' : <EOL> self . emit ( '<STR_LIT>' , self . mangle ( node . attrname ) ) <EOL> elif node . flags == '<STR_LIT>' : <EOL> self . emit ( '<STR_LIT>' , self . mangle ( node . attrname ) ) <EOL> else : <EOL> print "<STR_LIT>" , node . flags <EOL> print node <EOL> def _visitAssSequence ( self , node , op = '<STR_LIT>' ) : <EOL> if findOp ( node ) != '<STR_LIT>' : <EOL> self . emit ( op , len ( node . nodes ) ) <EOL> for child in node . nodes : <EOL> self . visit ( child ) <EOL> if VERSION > <NUM_LIT:1> : <EOL> visitAssTuple = _visitAssSequence <EOL> visitAssList = _visitAssSequence <EOL> else : <EOL> def visitAssTuple ( self , node ) : <EOL> self . _visitAssSequence ( node , '<STR_LIT>' ) <EOL> def visitAssList ( self , node ) : <EOL> self . _visitAssSequence ( node , '<STR_LIT>' ) <EOL> def visitAugAssign ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> aug_node = wrap_aug ( node . node ) <EOL> self . visit ( aug_node , "<STR_LIT>" ) <EOL> self . visit ( node . expr ) <EOL> self . emit ( self . _augmented_opcode [ node . op ] ) <EOL> self . visit ( aug_node , "<STR_LIT:store>" ) <EOL> _augmented_opcode = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def visitAugName ( self , node , mode ) : <EOL> if mode == "<STR_LIT>" : <EOL> self . loadName ( node . name ) <EOL> elif mode == "<STR_LIT:store>" : <EOL> self . storeName ( node . name ) <EOL> def visitAugGetattr ( self , node , mode ) : <EOL> if mode == "<STR_LIT>" : <EOL> self . visit ( node . expr ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' , self . mangle ( node . attrname ) ) <EOL> elif mode == "<STR_LIT:store>" : <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' , self . mangle ( node . attrname ) ) <EOL> def visitAugSlice ( self , node , mode ) : <EOL> if mode == "<STR_LIT>" : <EOL> self . visitSlice ( node , <NUM_LIT:1> ) <EOL> elif mode == "<STR_LIT:store>" : <EOL> slice = <NUM_LIT:0> <EOL> if node . lower : <EOL> slice = slice | <NUM_LIT:1> <EOL> if node . upper : <EOL> slice = slice | <NUM_LIT:2> <EOL> if slice == <NUM_LIT:0> : <EOL> self . emit ( '<STR_LIT>' ) <EOL> elif slice == <NUM_LIT:3> : <EOL> self . emit ( '<STR_LIT>' ) <EOL> else : <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' % slice ) <EOL> def visitAugSubscript ( self , node , mode ) : <EOL> if mode == "<STR_LIT>" : <EOL> self . visitSubscript ( node , <NUM_LIT:1> ) <EOL> elif mode == "<STR_LIT:store>" : <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def visitExec ( self , node ) : <EOL> self . visit ( node . expr ) <EOL> if node . locals is None : <EOL> self . emit ( '<STR_LIT>' , None ) <EOL> else : <EOL> self . visit ( node . locals ) <EOL> if node . globals is None : <EOL> self . emit ( '<STR_LIT>' ) <EOL> else : <EOL> self . visit ( node . globals ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def visitCallFunc ( self , node ) : <EOL> pos = <NUM_LIT:0> <EOL> kw = <NUM_LIT:0> <EOL> self . set_lineno ( node ) <EOL> self . visit ( node . node ) <EOL> for arg in node . args : <EOL> self . visit ( arg ) <EOL> if isinstance ( arg , ast . Keyword ) : <EOL> kw = kw + <NUM_LIT:1> <EOL> else : <EOL> pos = pos + <NUM_LIT:1> <EOL> if node . star_args is not None : <EOL> self . visit ( node . star_args ) <EOL> if node . dstar_args is not None : <EOL> self . visit ( node . dstar_args ) <EOL> have_star = node . star_args is not None <EOL> have_dstar = node . dstar_args is not None <EOL> opcode = callfunc_opcode_info [ have_star , have_dstar ] <EOL> self . emit ( opcode , kw << <NUM_LIT:8> | pos ) <EOL> def visitPrint ( self , node , newline = <NUM_LIT:0> ) : <EOL> self . set_lineno ( node ) <EOL> if node . dest : <EOL> self . visit ( node . dest ) <EOL> for child in node . nodes : <EOL> if node . dest : <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . visit ( child ) <EOL> if node . dest : <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> else : <EOL> self . emit ( '<STR_LIT>' ) <EOL> if node . dest and not newline : <EOL> self . emit ( '<STR_LIT>' ) <EOL> def visitPrintnl ( self , node ) : <EOL> self . visitPrint ( node , newline = <NUM_LIT:1> ) <EOL> if node . dest : <EOL> self . emit ( '<STR_LIT>' ) <EOL> else : <EOL> self . emit ( '<STR_LIT>' ) <EOL> def visitReturn ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> self . visit ( node . value ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def visitYield ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> self . visit ( node . value ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def visitSlice ( self , node , aug_flag = None ) : <EOL> self . visit ( node . expr ) <EOL> slice = <NUM_LIT:0> <EOL> if node . lower : <EOL> self . visit ( node . lower ) <EOL> slice = slice | <NUM_LIT:1> <EOL> if node . upper : <EOL> self . visit ( node . upper ) <EOL> slice = slice | <NUM_LIT:2> <EOL> if aug_flag : <EOL> if slice == <NUM_LIT:0> : <EOL> self . emit ( '<STR_LIT>' ) <EOL> elif slice == <NUM_LIT:3> : <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> else : <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> if node . flags == '<STR_LIT>' : <EOL> self . emit ( '<STR_LIT>' % slice ) <EOL> elif node . flags == '<STR_LIT>' : <EOL> self . emit ( '<STR_LIT>' % slice ) <EOL> elif node . flags == '<STR_LIT>' : <EOL> self . emit ( '<STR_LIT>' % slice ) <EOL> else : <EOL> print "<STR_LIT>" , node . flags <EOL> raise <EOL> def visitSubscript ( self , node , aug_flag = None ) : <EOL> self . visit ( node . expr ) <EOL> for sub in node . subs : <EOL> self . visit ( sub ) <EOL> if len ( node . subs ) > <NUM_LIT:1> : <EOL> self . emit ( '<STR_LIT>' , len ( node . subs ) ) <EOL> if aug_flag : <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> if node . flags == '<STR_LIT>' : <EOL> self . emit ( '<STR_LIT>' ) <EOL> elif node . flags == '<STR_LIT>' : <EOL> self . emit ( '<STR_LIT>' ) <EOL> elif node . flags == '<STR_LIT>' : <EOL> self . emit ( '<STR_LIT>' ) <EOL> def binaryOp ( self , node , op ) : <EOL> self . visit ( node . left ) <EOL> self . visit ( node . right ) <EOL> self . emit ( op ) <EOL> def visitAdd ( self , node ) : <EOL> return self . binaryOp ( node , '<STR_LIT>' ) <EOL> def visitSub ( self , node ) : <EOL> return self . binaryOp ( node , '<STR_LIT>' ) <EOL> def visitMul ( self , node ) : <EOL> return self . binaryOp ( node , '<STR_LIT>' ) <EOL> def visitDiv ( self , node ) : <EOL> return self . binaryOp ( node , self . _div_op ) <EOL> def visitFloorDiv ( self , node ) : <EOL> return self . binaryOp ( node , '<STR_LIT>' ) <EOL> def visitMod ( self , node ) : <EOL> return self . binaryOp ( node , '<STR_LIT>' ) <EOL> def visitPower ( self , node ) : <EOL> return self . binaryOp ( node , '<STR_LIT>' ) <EOL> def visitLeftShift ( self , node ) : <EOL> return self . binaryOp ( node , '<STR_LIT>' ) <EOL> def visitRightShift ( self , node ) : <EOL> return self . binaryOp ( node , '<STR_LIT>' ) <EOL> def unaryOp ( self , node , op ) : <EOL> self . visit ( node . expr ) <EOL> self . emit ( op ) <EOL> def visitInvert ( self , node ) : <EOL> return self . unaryOp ( node , '<STR_LIT>' ) <EOL> def visitUnarySub ( self , node ) : <EOL> return self . unaryOp ( node , '<STR_LIT>' ) <EOL> def visitUnaryAdd ( self , node ) : <EOL> return self . unaryOp ( node , '<STR_LIT>' ) <EOL> def visitUnaryInvert ( self , node ) : <EOL> return self . unaryOp ( node , '<STR_LIT>' ) <EOL> def visitNot ( self , node ) : <EOL> return self . unaryOp ( node , '<STR_LIT>' ) <EOL> def visitBackquote ( self , node ) : <EOL> return self . unaryOp ( node , '<STR_LIT>' ) <EOL> def bitOp ( self , nodes , op ) : <EOL> self . visit ( nodes [ <NUM_LIT:0> ] ) <EOL> for node in nodes [ <NUM_LIT:1> : ] : <EOL> self . visit ( node ) <EOL> self . emit ( op ) <EOL> def visitBitand ( self , node ) : <EOL> return self . bitOp ( node . nodes , '<STR_LIT>' ) <EOL> def visitBitor ( self , node ) : <EOL> return self . bitOp ( node . nodes , '<STR_LIT>' ) <EOL> def visitBitxor ( self , node ) : <EOL> return self . bitOp ( node . nodes , '<STR_LIT>' ) <EOL> def visitEllipsis ( self , node ) : <EOL> self . emit ( '<STR_LIT>' , Ellipsis ) <EOL> def visitTuple ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> for elt in node . nodes : <EOL> self . visit ( elt ) <EOL> self . emit ( '<STR_LIT>' , len ( node . nodes ) ) <EOL> def visitList ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> for elt in node . nodes : <EOL> self . visit ( elt ) <EOL> self . emit ( '<STR_LIT>' , len ( node . nodes ) ) <EOL> def visitSliceobj ( self , node ) : <EOL> for child in node . nodes : <EOL> self . visit ( child ) <EOL> self . emit ( '<STR_LIT>' , len ( node . nodes ) ) <EOL> def visitDict ( self , node ) : <EOL> self . set_lineno ( node ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> for k , v in node . items : <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . visit ( k ) <EOL> self . visit ( v ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> class NestedScopeMixin : <EOL> """<STR_LIT>""" <EOL> def initClass ( self ) : <EOL> self . __class__ . NameFinder = LocalNameFinder <EOL> self . __class__ . FunctionGen = FunctionCodeGenerator <EOL> self . __class__ . ClassGen = ClassCodeGenerator <EOL> class ModuleCodeGenerator ( NestedScopeMixin , CodeGenerator ) : <EOL> __super_init = CodeGenerator . __init__ <EOL> scopes = None <EOL> def __init__ ( self , tree ) : <EOL> self . graph = pyassem . PyFlowGraph ( "<STR_LIT>" , tree . filename ) <EOL> self . futures = future . find_futures ( tree ) <EOL> self . __super_init ( ) <EOL> walk ( tree , self ) <EOL> def get_module ( self ) : <EOL> return self <EOL> class ExpressionCodeGenerator ( NestedScopeMixin , CodeGenerator ) : <EOL> __super_init = CodeGenerator . __init__ <EOL> scopes = None <EOL> futures = ( ) <EOL> def __init__ ( self , tree ) : <EOL> self . graph = pyassem . PyFlowGraph ( "<STR_LIT>" , tree . filename ) <EOL> self . __super_init ( ) <EOL> walk ( tree , self ) <EOL> def get_module ( self ) : <EOL> return self <EOL> class InteractiveCodeGenerator ( NestedScopeMixin , CodeGenerator ) : <EOL> __super_init = CodeGenerator . __init__ <EOL> scopes = None <EOL> futures = ( ) <EOL> def __init__ ( self , tree ) : <EOL> self . graph = pyassem . PyFlowGraph ( "<STR_LIT>" , tree . filename ) <EOL> self . __super_init ( ) <EOL> self . set_lineno ( tree ) <EOL> walk ( tree , self ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def get_module ( self ) : <EOL> return self <EOL> def visitDiscard ( self , node ) : <EOL> self . visit ( node . expr ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> class AbstractFunctionCode : <EOL> optimized = <NUM_LIT:1> <EOL> lambdaCount = <NUM_LIT:0> <EOL> def __init__ ( self , func , scopes , isLambda , class_name , mod ) : <EOL> self . class_name = class_name <EOL> self . module = mod <EOL> if isLambda : <EOL> klass = FunctionCodeGenerator <EOL> name = "<STR_LIT>" % klass . lambdaCount <EOL> klass . lambdaCount = klass . lambdaCount + <NUM_LIT:1> <EOL> else : <EOL> name = func . name <EOL> args , hasTupleArg = generateArgList ( func . argnames ) <EOL> self . graph = pyassem . PyFlowGraph ( name , func . filename , args , <EOL> optimized = <NUM_LIT:1> ) <EOL> self . isLambda = isLambda <EOL> self . super_init ( ) <EOL> if not isLambda and func . doc : <EOL> self . setDocstring ( func . doc ) <EOL> lnf = walk ( func . code , self . NameFinder ( args ) , verbose = <NUM_LIT:0> ) <EOL> self . locals . push ( lnf . getLocals ( ) ) <EOL> if func . varargs : <EOL> self . graph . setFlag ( CO_VARARGS ) <EOL> if func . kwargs : <EOL> self . graph . setFlag ( CO_VARKEYWORDS ) <EOL> self . set_lineno ( func ) <EOL> if hasTupleArg : <EOL> self . generateArgUnpack ( func . argnames ) <EOL> def get_module ( self ) : <EOL> return self . module <EOL> def finish ( self ) : <EOL> self . graph . startExitBlock ( ) <EOL> if not self . isLambda : <EOL> self . emit ( '<STR_LIT>' , None ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def generateArgUnpack ( self , args ) : <EOL> for i in range ( len ( args ) ) : <EOL> arg = args [ i ] <EOL> if isinstance ( arg , tuple ) : <EOL> self . emit ( '<STR_LIT>' , '<STR_LIT>' % ( i * <NUM_LIT:2> ) ) <EOL> self . unpackSequence ( arg ) <EOL> def unpackSequence ( self , tup ) : <EOL> if VERSION > <NUM_LIT:1> : <EOL> self . emit ( '<STR_LIT>' , len ( tup ) ) <EOL> else : <EOL> self . emit ( '<STR_LIT>' , len ( tup ) ) <EOL> for elt in tup : <EOL> if isinstance ( elt , tuple ) : <EOL> self . unpackSequence ( elt ) <EOL> else : <EOL> self . _nameOp ( '<STR_LIT>' , elt ) <EOL> unpackTuple = unpackSequence <EOL> class FunctionCodeGenerator ( NestedScopeMixin , AbstractFunctionCode , <EOL> CodeGenerator ) : <EOL> super_init = CodeGenerator . __init__ <EOL> scopes = None <EOL> __super_init = AbstractFunctionCode . __init__ <EOL> def __init__ ( self , func , scopes , isLambda , class_name , mod ) : <EOL> self . scopes = scopes <EOL> self . scope = scopes [ func ] <EOL> self . __super_init ( func , scopes , isLambda , class_name , mod ) <EOL> self . graph . setFreeVars ( self . scope . get_free_vars ( ) ) <EOL> self . graph . setCellVars ( self . scope . get_cell_vars ( ) ) <EOL> if self . scope . generator is not None : <EOL> self . graph . setFlag ( CO_GENERATOR ) <EOL> class GenExprCodeGenerator ( NestedScopeMixin , AbstractFunctionCode , <EOL> CodeGenerator ) : <EOL> super_init = CodeGenerator . __init__ <EOL> scopes = None <EOL> __super_init = AbstractFunctionCode . __init__ <EOL> def __init__ ( self , gexp , scopes , class_name , mod ) : <EOL> self . scopes = scopes <EOL> self . scope = scopes [ gexp ] <EOL> self . __super_init ( gexp , scopes , <NUM_LIT:1> , class_name , mod ) <EOL> self . graph . setFreeVars ( self . scope . get_free_vars ( ) ) <EOL> self . graph . setCellVars ( self . scope . get_cell_vars ( ) ) <EOL> self . graph . setFlag ( CO_GENERATOR ) <EOL> class AbstractClassCode : <EOL> def __init__ ( self , klass , scopes , module ) : <EOL> self . class_name = klass . name <EOL> self . module = module <EOL> self . graph = pyassem . PyFlowGraph ( klass . name , klass . filename , <EOL> optimized = <NUM_LIT:0> , klass = <NUM_LIT:1> ) <EOL> self . super_init ( ) <EOL> lnf = walk ( klass . code , self . NameFinder ( ) , verbose = <NUM_LIT:0> ) <EOL> self . locals . push ( lnf . getLocals ( ) ) <EOL> self . graph . setFlag ( CO_NEWLOCALS ) <EOL> if klass . doc : <EOL> self . setDocstring ( klass . doc ) <EOL> def get_module ( self ) : <EOL> return self . module <EOL> def finish ( self ) : <EOL> self . graph . startExitBlock ( ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> class ClassCodeGenerator ( NestedScopeMixin , AbstractClassCode , CodeGenerator ) : <EOL> super_init = CodeGenerator . __init__ <EOL> scopes = None <EOL> __super_init = AbstractClassCode . __init__ <EOL> def __init__ ( self , klass , scopes , module ) : <EOL> self . scopes = scopes <EOL> self . scope = scopes [ klass ] <EOL> self . __super_init ( klass , scopes , module ) <EOL> self . graph . setFreeVars ( self . scope . get_free_vars ( ) ) <EOL> self . graph . setCellVars ( self . scope . get_cell_vars ( ) ) <EOL> self . set_lineno ( klass ) <EOL> self . emit ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . storeName ( "<STR_LIT>" ) <EOL> if klass . doc : <EOL> self . emit ( "<STR_LIT>" , klass . doc ) <EOL> self . storeName ( '<STR_LIT>' ) <EOL> def generateArgList ( arglist ) : <EOL> """<STR_LIT>""" <EOL> args = [ ] <EOL> extra = [ ] <EOL> count = <NUM_LIT:0> <EOL> for i in range ( len ( arglist ) ) : <EOL> elt = arglist [ i ] <EOL> if isinstance ( elt , str ) : <EOL> args . append ( elt ) <EOL> elif isinstance ( elt , tuple ) : <EOL> args . append ( TupleArg ( i * <NUM_LIT:2> , elt ) ) <EOL> extra . extend ( misc . flatten ( elt ) ) <EOL> count = count + <NUM_LIT:1> <EOL> else : <EOL> raise ValueError , "<STR_LIT>" , elt <EOL> return args + extra , count <EOL> def findOp ( node ) : <EOL> """<STR_LIT>""" <EOL> v = OpFinder ( ) <EOL> walk ( node , v , verbose = <NUM_LIT:0> ) <EOL> return v . op <EOL> class OpFinder : <EOL> def __init__ ( self ) : <EOL> self . op = None <EOL> def visitAssName ( self , node ) : <EOL> if self . op is None : <EOL> self . op = node . flags <EOL> elif self . op != node . flags : <EOL> raise ValueError , "<STR_LIT>" <EOL> visitAssAttr = visitAssName <EOL> visitSubscript = visitAssName <EOL> class Delegator : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , obj ) : <EOL> self . obj = obj <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . obj , attr ) <EOL> class AugGetattr ( Delegator ) : <EOL> pass <EOL> class AugName ( Delegator ) : <EOL> pass <EOL> class AugSlice ( Delegator ) : <EOL> pass <EOL> class AugSubscript ( Delegator ) : <EOL> pass <EOL> wrapper = { <EOL> ast . Getattr : AugGetattr , <EOL> ast . Name : AugName , <EOL> ast . Slice : AugSlice , <EOL> ast . Subscript : AugSubscript , <EOL> } <EOL> def wrap_aug ( node ) : <EOL> return wrapper [ node . __class__ ] ( node ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> for file in sys . argv [ <NUM_LIT:1> : ] : <EOL> compileFile ( file ) </s>
