<s> """<STR_LIT>""" <EOL> import _symtable <EOL> from _symtable import ( USE , DEF_GLOBAL , DEF_LOCAL , DEF_PARAM , <EOL> DEF_IMPORT , DEF_BOUND , OPT_IMPORT_STAR , OPT_EXEC , OPT_BARE_EXEC , <EOL> SCOPE_OFF , SCOPE_MASK , FREE , GLOBAL_IMPLICIT , GLOBAL_EXPLICIT ) <EOL> import warnings <EOL> import weakref <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def symtable ( code , filename , compile_type ) : <EOL> raw = _symtable . symtable ( code , filename , compile_type ) <EOL> for top in raw . itervalues ( ) : <EOL> if top . name == '<STR_LIT>' : <EOL> break <EOL> return _newSymbolTable ( top , filename ) <EOL> class SymbolTableFactory : <EOL> def __init__ ( self ) : <EOL> self . __memo = weakref . WeakValueDictionary ( ) <EOL> def new ( self , table , filename ) : <EOL> if table . type == _symtable . TYPE_FUNCTION : <EOL> return Function ( table , filename ) <EOL> if table . type == _symtable . TYPE_CLASS : <EOL> return Class ( table , filename ) <EOL> return SymbolTable ( table , filename ) <EOL> def __call__ ( self , table , filename ) : <EOL> key = table , filename <EOL> obj = self . __memo . get ( key , None ) <EOL> if obj is None : <EOL> obj = self . __memo [ key ] = self . new ( table , filename ) <EOL> return obj <EOL> _newSymbolTable = SymbolTableFactory ( ) <EOL> class SymbolTable ( object ) : <EOL> def __init__ ( self , raw_table , filename ) : <EOL> self . _table = raw_table <EOL> self . _filename = filename <EOL> self . _symbols = { } <EOL> def __repr__ ( self ) : <EOL> if self . __class__ == SymbolTable : <EOL> kind = "<STR_LIT>" <EOL> else : <EOL> kind = "<STR_LIT>" % self . __class__ . __name__ <EOL> if self . _table . name == "<STR_LIT>" : <EOL> return "<STR_LIT>" . format ( kind , self . _filename ) <EOL> else : <EOL> return "<STR_LIT>" . format ( kind , <EOL> self . _table . name , <EOL> self . _filename ) <EOL> def get_type ( self ) : <EOL> if self . _table . type == _symtable . TYPE_MODULE : <EOL> return "<STR_LIT>" <EOL> if self . _table . type == _symtable . TYPE_FUNCTION : <EOL> return "<STR_LIT>" <EOL> if self . _table . type == _symtable . TYPE_CLASS : <EOL> return "<STR_LIT:class>" <EOL> assert self . _table . type in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , "<STR_LIT>" . format ( self . _table . type ) <EOL> def get_id ( self ) : <EOL> return self . _table . id <EOL> def get_name ( self ) : <EOL> return self . _table . name <EOL> def get_lineno ( self ) : <EOL> return self . _table . lineno <EOL> def is_optimized ( self ) : <EOL> return bool ( self . _table . type == _symtable . TYPE_FUNCTION <EOL> and not self . _table . optimized ) <EOL> def is_nested ( self ) : <EOL> return bool ( self . _table . nested ) <EOL> def has_children ( self ) : <EOL> return bool ( self . _table . children ) <EOL> def has_exec ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( self . _table . optimized & ( OPT_EXEC | OPT_BARE_EXEC ) ) <EOL> def has_import_star ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( self . _table . optimized & OPT_IMPORT_STAR ) <EOL> def get_identifiers ( self ) : <EOL> return self . _table . symbols . keys ( ) <EOL> def lookup ( self , name ) : <EOL> sym = self . _symbols . get ( name ) <EOL> if sym is None : <EOL> flags = self . _table . symbols [ name ] <EOL> namespaces = self . __check_children ( name ) <EOL> sym = self . _symbols [ name ] = Symbol ( name , flags , namespaces ) <EOL> return sym <EOL> def get_symbols ( self ) : <EOL> return [ self . lookup ( ident ) for ident in self . get_identifiers ( ) ] <EOL> def __check_children ( self , name ) : <EOL> return [ _newSymbolTable ( st , self . _filename ) <EOL> for st in self . _table . children <EOL> if st . name == name ] <EOL> def get_children ( self ) : <EOL> return [ _newSymbolTable ( st , self . _filename ) <EOL> for st in self . _table . children ] <EOL> class Function ( SymbolTable ) : <EOL> __params = None <EOL> __locals = None <EOL> __frees = None <EOL> __globals = None <EOL> def __idents_matching ( self , test_func ) : <EOL> return tuple ( [ ident for ident in self . get_identifiers ( ) <EOL> if test_func ( self . _table . symbols [ ident ] ) ] ) <EOL> def get_parameters ( self ) : <EOL> if self . __params is None : <EOL> self . __params = self . __idents_matching ( lambda x : x & DEF_PARAM ) <EOL> return self . __params <EOL> def get_locals ( self ) : <EOL> if self . __locals is None : <EOL> self . __locals = self . __idents_matching ( lambda x : x & DEF_BOUND ) <EOL> return self . __locals <EOL> def get_globals ( self ) : <EOL> if self . __globals is None : <EOL> glob = ( GLOBAL_IMPLICIT , GLOBAL_EXPLICIT ) <EOL> test = lambda x : ( ( x >> SCOPE_OFF ) & SCOPE_MASK ) in glob <EOL> self . __globals = self . __idents_matching ( test ) <EOL> return self . __globals <EOL> def get_frees ( self ) : <EOL> if self . __frees is None : <EOL> is_free = lambda x : ( ( x >> SCOPE_OFF ) & SCOPE_MASK ) == FREE <EOL> self . __frees = self . __idents_matching ( is_free ) <EOL> return self . __frees <EOL> class Class ( SymbolTable ) : <EOL> __methods = None <EOL> def get_methods ( self ) : <EOL> if self . __methods is None : <EOL> d = { } <EOL> for st in self . _table . children : <EOL> d [ st . name ] = <NUM_LIT:1> <EOL> self . __methods = tuple ( d ) <EOL> return self . __methods <EOL> class Symbol ( object ) : <EOL> def __init__ ( self , name , flags , namespaces = None ) : <EOL> self . __name = name <EOL> self . __flags = flags <EOL> self . __scope = ( flags >> SCOPE_OFF ) & SCOPE_MASK <EOL> self . __namespaces = namespaces or ( ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . __name ) <EOL> def get_name ( self ) : <EOL> return self . __name <EOL> def is_referenced ( self ) : <EOL> return bool ( self . __flags & _symtable . USE ) <EOL> def is_parameter ( self ) : <EOL> return bool ( self . __flags & DEF_PARAM ) <EOL> def is_global ( self ) : <EOL> return bool ( self . __scope in ( GLOBAL_IMPLICIT , GLOBAL_EXPLICIT ) ) <EOL> def is_vararg ( self ) : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning , <NUM_LIT:2> ) <EOL> return False <EOL> def is_keywordarg ( self ) : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning , <NUM_LIT:2> ) <EOL> return False <EOL> def is_local ( self ) : <EOL> return bool ( self . __flags & DEF_BOUND ) <EOL> def is_free ( self ) : <EOL> return bool ( self . __scope == FREE ) <EOL> def is_imported ( self ) : <EOL> return bool ( self . __flags & DEF_IMPORT ) <EOL> def is_assigned ( self ) : <EOL> return bool ( self . __flags & DEF_LOCAL ) <EOL> def is_in_tuple ( self ) : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning , <NUM_LIT:2> ) <EOL> def is_namespace ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( self . __namespaces ) <EOL> def get_namespaces ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __namespaces <EOL> def get_namespace ( self ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . __namespaces ) != <NUM_LIT:1> : <EOL> raise ValueError , "<STR_LIT>" <EOL> return self . __namespaces [ <NUM_LIT:0> ] <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import os , sys <EOL> src = open ( sys . argv [ <NUM_LIT:0> ] ) . read ( ) <EOL> mod = symtable ( src , os . path . split ( sys . argv [ <NUM_LIT:0> ] ) [ <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> for ident in mod . get_identifiers ( ) : <EOL> info = mod . lookup ( ident ) <EOL> print info , info . is_local ( ) , info . is_namespace ( ) </s>
