<s> """<STR_LIT>""" <EOL> from lib2to3 . compiler import ast <EOL> from lib2to3 . compiler . consts import SC_LOCAL , SC_GLOBAL , SC_FREE , SC_CELL , SC_UNKNOWN <EOL> from lib2to3 . compiler . misc import mangle <EOL> import types <EOL> import sys <EOL> MANGLE_LEN = <NUM_LIT> <EOL> class Scope : <EOL> def __init__ ( self , name , module , klass = None ) : <EOL> self . name = name <EOL> self . module = module <EOL> self . defs = { } <EOL> self . uses = { } <EOL> self . globals = { } <EOL> self . params = { } <EOL> self . frees = { } <EOL> self . cells = { } <EOL> self . children = [ ] <EOL> self . nested = None <EOL> self . generator = None <EOL> self . klass = None <EOL> if klass is not None : <EOL> for i in range ( len ( klass ) ) : <EOL> if klass [ i ] != '<STR_LIT:_>' : <EOL> self . klass = klass [ i : ] <EOL> break <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . name ) <EOL> def mangle ( self , name ) : <EOL> if self . klass is None : <EOL> return name <EOL> return mangle ( name , self . klass ) <EOL> def add_def ( self , name ) : <EOL> self . defs [ self . mangle ( name ) ] = <NUM_LIT:1> <EOL> def add_use ( self , name ) : <EOL> self . uses [ self . mangle ( name ) ] = <NUM_LIT:1> <EOL> def add_global ( self , name ) : <EOL> name = self . mangle ( name ) <EOL> if name in self . uses or name in self . defs : <EOL> pass <EOL> if name in self . params : <EOL> raise SyntaxError , "<STR_LIT>" % ( name , self . name ) <EOL> self . globals [ name ] = <NUM_LIT:1> <EOL> self . module . add_def ( name ) <EOL> def add_param ( self , name ) : <EOL> name = self . mangle ( name ) <EOL> self . defs [ name ] = <NUM_LIT:1> <EOL> self . params [ name ] = <NUM_LIT:1> <EOL> def get_names ( self ) : <EOL> d = { } <EOL> d . update ( self . defs ) <EOL> d . update ( self . uses ) <EOL> d . update ( self . globals ) <EOL> return d . keys ( ) <EOL> def add_child ( self , child ) : <EOL> self . children . append ( child ) <EOL> def get_children ( self ) : <EOL> return self . children <EOL> def DEBUG ( self ) : <EOL> print >> sys . stderr , self . name , self . nested and "<STR_LIT>" or "<STR_LIT>" <EOL> print >> sys . stderr , "<STR_LIT>" , self . globals <EOL> print >> sys . stderr , "<STR_LIT>" , self . cells <EOL> print >> sys . stderr , "<STR_LIT>" , self . defs <EOL> print >> sys . stderr , "<STR_LIT>" , self . uses <EOL> print >> sys . stderr , "<STR_LIT>" , self . frees <EOL> def check_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name in self . globals : <EOL> return SC_GLOBAL <EOL> if name in self . cells : <EOL> return SC_CELL <EOL> if name in self . defs : <EOL> return SC_LOCAL <EOL> if self . nested and ( name in self . frees or name in self . uses ) : <EOL> return SC_FREE <EOL> if self . nested : <EOL> return SC_UNKNOWN <EOL> else : <EOL> return SC_GLOBAL <EOL> def get_free_vars ( self ) : <EOL> if not self . nested : <EOL> return ( ) <EOL> free = { } <EOL> free . update ( self . frees ) <EOL> for name in self . uses . keys ( ) : <EOL> if name not in self . defs and name not in self . globals : <EOL> free [ name ] = <NUM_LIT:1> <EOL> return free . keys ( ) <EOL> def handle_children ( self ) : <EOL> for child in self . children : <EOL> frees = child . get_free_vars ( ) <EOL> globals = self . add_frees ( frees ) <EOL> for name in globals : <EOL> child . force_global ( name ) <EOL> def force_global ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . globals [ name ] = <NUM_LIT:1> <EOL> if name in self . frees : <EOL> del self . frees [ name ] <EOL> for child in self . children : <EOL> if child . check_name ( name ) == SC_FREE : <EOL> child . force_global ( name ) <EOL> def add_frees ( self , names ) : <EOL> """<STR_LIT>""" <EOL> child_globals = [ ] <EOL> for name in names : <EOL> sc = self . check_name ( name ) <EOL> if self . nested : <EOL> if sc == SC_UNKNOWN or sc == SC_FREE or isinstance ( self , ClassScope ) : <EOL> self . frees [ name ] = <NUM_LIT:1> <EOL> elif sc == SC_GLOBAL : <EOL> child_globals . append ( name ) <EOL> elif isinstance ( self , FunctionScope ) and sc == SC_LOCAL : <EOL> self . cells [ name ] = <NUM_LIT:1> <EOL> elif sc != SC_CELL : <EOL> child_globals . append ( name ) <EOL> else : <EOL> if sc == SC_LOCAL : <EOL> self . cells [ name ] = <NUM_LIT:1> <EOL> elif sc != SC_CELL : <EOL> child_globals . append ( name ) <EOL> return child_globals <EOL> def get_cell_vars ( self ) : <EOL> return self . cells . keys ( ) <EOL> class ModuleScope ( Scope ) : <EOL> __super_init = Scope . __init__ <EOL> def __init__ ( self ) : <EOL> self . __super_init ( "<STR_LIT>" , self ) <EOL> class FunctionScope ( Scope ) : <EOL> pass <EOL> class GenExprScope ( Scope ) : <EOL> __super_init = Scope . __init__ <EOL> __counter = <NUM_LIT:1> <EOL> def __init__ ( self , module , klass = None ) : <EOL> i = self . __counter <EOL> self . __counter += <NUM_LIT:1> <EOL> self . __super_init ( "<STR_LIT>" % i , module , klass ) <EOL> self . add_param ( '<STR_LIT>' ) <EOL> def get_names ( self ) : <EOL> keys = Scope . get_names ( self ) <EOL> return keys <EOL> class LambdaScope ( FunctionScope ) : <EOL> __super_init = Scope . __init__ <EOL> __counter = <NUM_LIT:1> <EOL> def __init__ ( self , module , klass = None ) : <EOL> i = self . __counter <EOL> self . __counter += <NUM_LIT:1> <EOL> self . __super_init ( "<STR_LIT>" % i , module , klass ) <EOL> class ClassScope ( Scope ) : <EOL> __super_init = Scope . __init__ <EOL> def __init__ ( self , name , module ) : <EOL> self . __super_init ( name , module , name ) <EOL> class SymbolVisitor : <EOL> def __init__ ( self ) : <EOL> self . scopes = { } <EOL> self . klass = None <EOL> def visitModule ( self , node ) : <EOL> scope = self . module = self . scopes [ node ] = ModuleScope ( ) <EOL> self . visit ( node . node , scope ) <EOL> visitExpression = visitModule <EOL> def visitFunction ( self , node , parent ) : <EOL> if node . decorators : <EOL> self . visit ( node . decorators , parent ) <EOL> parent . add_def ( node . name ) <EOL> for n in node . defaults : <EOL> self . visit ( n , parent ) <EOL> scope = FunctionScope ( node . name , self . module , self . klass ) <EOL> if parent . nested or isinstance ( parent , FunctionScope ) : <EOL> scope . nested = <NUM_LIT:1> <EOL> self . scopes [ node ] = scope <EOL> self . _do_args ( scope , node . argnames ) <EOL> self . visit ( node . code , scope ) <EOL> self . handle_free_vars ( scope , parent ) <EOL> def visitGenExpr ( self , node , parent ) : <EOL> scope = GenExprScope ( self . module , self . klass ) ; <EOL> if parent . nested or isinstance ( parent , FunctionScope ) or isinstance ( parent , GenExprScope ) : <EOL> scope . nested = <NUM_LIT:1> <EOL> self . scopes [ node ] = scope <EOL> self . visit ( node . code , scope ) <EOL> self . handle_free_vars ( scope , parent ) <EOL> def visitGenExprInner ( self , node , scope ) : <EOL> for genfor in node . quals : <EOL> self . visit ( genfor , scope ) <EOL> self . visit ( node . expr , scope ) <EOL> def visitGenExprFor ( self , node , scope ) : <EOL> self . visit ( node . assign , scope , <NUM_LIT:1> ) <EOL> self . visit ( node . iter , scope ) <EOL> for if_ in node . ifs : <EOL> self . visit ( if_ , scope ) <EOL> def visitGenExprIf ( self , node , scope ) : <EOL> self . visit ( node . test , scope ) <EOL> def visitLambda ( self , node , parent , assign = <NUM_LIT:0> ) : <EOL> assert not assign <EOL> for n in node . defaults : <EOL> self . visit ( n , parent ) <EOL> scope = LambdaScope ( self . module , self . klass ) <EOL> if parent . nested or isinstance ( parent , FunctionScope ) : <EOL> scope . nested = <NUM_LIT:1> <EOL> self . scopes [ node ] = scope <EOL> self . _do_args ( scope , node . argnames ) <EOL> self . visit ( node . code , scope ) <EOL> self . handle_free_vars ( scope , parent ) <EOL> def _do_args ( self , scope , args ) : <EOL> for name in args : <EOL> if type ( name ) == types . TupleType : <EOL> self . _do_args ( scope , name ) <EOL> else : <EOL> scope . add_param ( name ) <EOL> def handle_free_vars ( self , scope , parent ) : <EOL> parent . add_child ( scope ) <EOL> scope . handle_children ( ) <EOL> def visitClass ( self , node , parent ) : <EOL> parent . add_def ( node . name ) <EOL> for n in node . bases : <EOL> self . visit ( n , parent ) <EOL> scope = ClassScope ( node . name , self . module ) <EOL> if parent . nested or isinstance ( parent , FunctionScope ) : <EOL> scope . nested = <NUM_LIT:1> <EOL> if node . doc is not None : <EOL> scope . add_def ( '<STR_LIT>' ) <EOL> scope . add_def ( '<STR_LIT>' ) <EOL> self . scopes [ node ] = scope <EOL> prev = self . klass <EOL> self . klass = node . name <EOL> self . visit ( node . code , scope ) <EOL> self . klass = prev <EOL> self . handle_free_vars ( scope , parent ) <EOL> def visitName ( self , node , scope , assign = <NUM_LIT:0> ) : <EOL> if assign : <EOL> scope . add_def ( node . name ) <EOL> else : <EOL> scope . add_use ( node . name ) <EOL> def visitFor ( self , node , scope ) : <EOL> self . visit ( node . assign , scope , <NUM_LIT:1> ) <EOL> self . visit ( node . list , scope ) <EOL> self . visit ( node . body , scope ) <EOL> if node . else_ : <EOL> self . visit ( node . else_ , scope ) <EOL> def visitFrom ( self , node , scope ) : <EOL> for name , asname in node . names : <EOL> if name == "<STR_LIT:*>" : <EOL> continue <EOL> scope . add_def ( asname or name ) <EOL> def visitImport ( self , node , scope ) : <EOL> for name , asname in node . names : <EOL> i = name . find ( "<STR_LIT:.>" ) <EOL> if i > - <NUM_LIT:1> : <EOL> name = name [ : i ] <EOL> scope . add_def ( asname or name ) <EOL> def visitGlobal ( self , node , scope ) : <EOL> for name in node . names : <EOL> scope . add_global ( name ) <EOL> def visitAssign ( self , node , scope ) : <EOL> """<STR_LIT>""" <EOL> for n in node . nodes : <EOL> self . visit ( n , scope , <NUM_LIT:1> ) <EOL> self . visit ( node . expr , scope ) <EOL> def visitAssName ( self , node , scope , assign = <NUM_LIT:1> ) : <EOL> scope . add_def ( node . name ) <EOL> def visitAssAttr ( self , node , scope , assign = <NUM_LIT:0> ) : <EOL> self . visit ( node . expr , scope , <NUM_LIT:0> ) <EOL> def visitSubscript ( self , node , scope , assign = <NUM_LIT:0> ) : <EOL> self . visit ( node . expr , scope , <NUM_LIT:0> ) <EOL> for n in node . subs : <EOL> self . visit ( n , scope , <NUM_LIT:0> ) <EOL> def visitSlice ( self , node , scope , assign = <NUM_LIT:0> ) : <EOL> self . visit ( node . expr , scope , <NUM_LIT:0> ) <EOL> if node . lower : <EOL> self . visit ( node . lower , scope , <NUM_LIT:0> ) <EOL> if node . upper : <EOL> self . visit ( node . upper , scope , <NUM_LIT:0> ) <EOL> def visitAugAssign ( self , node , scope ) : <EOL> self . visit ( node . node , scope ) <EOL> if isinstance ( node . node , ast . Name ) : <EOL> self . visit ( node . node , scope , <NUM_LIT:1> ) <EOL> self . visit ( node . expr , scope ) <EOL> _const_types = types . StringType , types . IntType , types . FloatType <EOL> def visitIf ( self , node , scope ) : <EOL> for test , body in node . tests : <EOL> if isinstance ( test , ast . Const ) : <EOL> if type ( test . value ) in self . _const_types : <EOL> if not test . value : <EOL> continue <EOL> self . visit ( test , scope ) <EOL> self . visit ( body , scope ) <EOL> if node . else_ : <EOL> self . visit ( node . else_ , scope ) <EOL> def visitYield ( self , node , scope ) : <EOL> scope . generator = <NUM_LIT:1> <EOL> self . visit ( node . value , scope ) <EOL> def list_eq ( l1 , l2 ) : <EOL> return sorted ( l1 ) == sorted ( l2 ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import sys <EOL> from compiler import parseFile , walk <EOL> import symtable <EOL> def get_names ( syms ) : <EOL> res = [ ] <EOL> for sym in syms . get_symbols ( ) : <EOL> s = sym . get_name ( ) <EOL> if not ( s . startswith ( '<STR_LIT>' ) or s . startswith ( '<STR_LIT:.>' ) ) : <EOL> res . append ( s ) <EOL> return res <EOL> for file in sys . argv [ <NUM_LIT:1> : ] : <EOL> print file <EOL> f = open ( file ) <EOL> buf = f . read ( ) <EOL> f . close ( ) <EOL> syms = symtable . symtable ( buf , file , "<STR_LIT>" ) <EOL> mod_names = get_names ( syms ) <EOL> tree = parseFile ( file ) <EOL> s = SymbolVisitor ( ) <EOL> walk ( tree , s ) <EOL> names2 = s . scopes [ tree ] . get_names ( ) <EOL> if not list_eq ( mod_names , names2 ) : <EOL> print <EOL> print "<STR_LIT>" , file <EOL> print sorted ( mod_names ) <EOL> print sorted ( names2 ) <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> d = { } <EOL> d . update ( s . scopes ) <EOL> del d [ tree ] <EOL> scopes = d . values ( ) <EOL> del d <EOL> for s in syms . get_symbols ( ) : <EOL> if s . is_namespace ( ) : <EOL> l = [ sc for sc in scopes <EOL> if sc . name == s . get_name ( ) ] <EOL> if len ( l ) > <NUM_LIT:1> : <EOL> print "<STR_LIT>" , s . get_name ( ) <EOL> else : <EOL> if not list_eq ( get_names ( s . get_namespace ( ) ) , <EOL> l [ <NUM_LIT:0> ] . get_names ( ) ) : <EOL> print s . get_name ( ) <EOL> print sorted ( get_names ( s . get_namespace ( ) ) ) <EOL> print sorted ( l [ <NUM_LIT:0> ] . get_names ( ) ) <EOL> sys . exit ( - <NUM_LIT:1> ) </s>
