<s> """<STR_LIT>""" <EOL> import sys <EOL> import __builtin__ <EOL> import os <EOL> import ihooks <EOL> import imp <EOL> __all__ = [ "<STR_LIT>" ] <EOL> class FileBase : <EOL> ok_file_methods = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> class FileWrapper ( FileBase ) : <EOL> def __init__ ( self , f ) : <EOL> for m in self . ok_file_methods : <EOL> if not hasattr ( self , m ) and hasattr ( f , m ) : <EOL> setattr ( self , m , getattr ( f , m ) ) <EOL> def close ( self ) : <EOL> self . flush ( ) <EOL> TEMPLATE = """<STR_LIT>""" <EOL> class FileDelegate ( FileBase ) : <EOL> def __init__ ( self , mod , name ) : <EOL> self . mod = mod <EOL> self . name = name <EOL> for m in FileBase . ok_file_methods + ( '<STR_LIT>' , ) : <EOL> exec TEMPLATE % ( m , m ) <EOL> class RHooks ( ihooks . Hooks ) : <EOL> def __init__ ( self , * args ) : <EOL> verbose = <NUM_LIT:0> <EOL> rexec = None <EOL> if args and type ( args [ - <NUM_LIT:1> ] ) == type ( <NUM_LIT:0> ) : <EOL> verbose = args [ - <NUM_LIT:1> ] <EOL> args = args [ : - <NUM_LIT:1> ] <EOL> if args and hasattr ( args [ <NUM_LIT:0> ] , '<STR_LIT>' ) : <EOL> rexec = args [ <NUM_LIT:0> ] <EOL> args = args [ <NUM_LIT:1> : ] <EOL> if args : <EOL> raise TypeError , "<STR_LIT>" <EOL> ihooks . Hooks . __init__ ( self , verbose ) <EOL> self . rexec = rexec <EOL> def set_rexec ( self , rexec ) : <EOL> self . rexec = rexec <EOL> def get_suffixes ( self ) : <EOL> return self . rexec . get_suffixes ( ) <EOL> def is_builtin ( self , name ) : <EOL> return self . rexec . is_builtin ( name ) <EOL> def init_builtin ( self , name ) : <EOL> m = __import__ ( name ) <EOL> return self . rexec . copy_except ( m , ( ) ) <EOL> def init_frozen ( self , name ) : raise SystemError , "<STR_LIT>" <EOL> def load_source ( self , * args ) : raise SystemError , "<STR_LIT>" <EOL> def load_compiled ( self , * args ) : raise SystemError , "<STR_LIT>" <EOL> def load_package ( self , * args ) : raise SystemError , "<STR_LIT>" <EOL> def load_dynamic ( self , name , filename , file ) : <EOL> return self . rexec . load_dynamic ( name , filename , file ) <EOL> def add_module ( self , name ) : <EOL> return self . rexec . add_module ( name ) <EOL> def modules_dict ( self ) : <EOL> return self . rexec . modules <EOL> def default_path ( self ) : <EOL> return self . rexec . modules [ '<STR_LIT>' ] . path <EOL> RModuleLoader = ihooks . FancyModuleLoader <EOL> RModuleImporter = ihooks . ModuleImporter <EOL> class RExec ( ihooks . _Verbose ) : <EOL> """<STR_LIT>""" <EOL> ok_path = tuple ( sys . path ) <EOL> ok_builtin_modules = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:time>' , <EOL> '<STR_LIT>' ) <EOL> ok_posix_names = ( '<STR_LIT:error>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> ok_sys_names = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:version>' , '<STR_LIT>' ) <EOL> nok_builtin_names = ( '<STR_LIT>' , '<STR_LIT:file>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> ok_file_types = ( imp . C_EXTENSION , imp . PY_SOURCE ) <EOL> def __init__ ( self , hooks = None , verbose = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError , "<STR_LIT>" <EOL> ihooks . _Verbose . __init__ ( self , verbose ) <EOL> self . hooks = hooks or RHooks ( verbose ) <EOL> self . hooks . set_rexec ( self ) <EOL> self . modules = { } <EOL> self . ok_dynamic_modules = self . ok_builtin_modules <EOL> list = [ ] <EOL> for mname in self . ok_builtin_modules : <EOL> if mname in sys . builtin_module_names : <EOL> list . append ( mname ) <EOL> self . ok_builtin_modules = tuple ( list ) <EOL> self . set_trusted_path ( ) <EOL> self . make_builtin ( ) <EOL> self . make_initial_modules ( ) <EOL> self . make_sys ( ) <EOL> self . loader = RModuleLoader ( self . hooks , verbose ) <EOL> self . importer = RModuleImporter ( self . loader , verbose ) <EOL> def set_trusted_path ( self ) : <EOL> self . trusted_path = filter ( os . path . isabs , sys . path ) <EOL> def load_dynamic ( self , name , filename , file ) : <EOL> if name not in self . ok_dynamic_modules : <EOL> raise ImportError , "<STR_LIT>" % name <EOL> if name in sys . modules : <EOL> src = sys . modules [ name ] <EOL> else : <EOL> src = imp . load_dynamic ( name , filename , file ) <EOL> dst = self . copy_except ( src , [ ] ) <EOL> return dst <EOL> def make_initial_modules ( self ) : <EOL> self . make_main ( ) <EOL> self . make_osname ( ) <EOL> def get_suffixes ( self ) : <EOL> return [ item <EOL> for item in imp . get_suffixes ( ) <EOL> if item [ <NUM_LIT:2> ] in self . ok_file_types ] <EOL> def is_builtin ( self , mname ) : <EOL> return mname in self . ok_builtin_modules <EOL> def make_builtin ( self ) : <EOL> m = self . copy_except ( __builtin__ , self . nok_builtin_names ) <EOL> m . __import__ = self . r_import <EOL> m . reload = self . r_reload <EOL> m . open = m . file = self . r_open <EOL> def make_main ( self ) : <EOL> m = self . add_module ( '<STR_LIT:__main__>' ) <EOL> def make_osname ( self ) : <EOL> osname = os . name <EOL> src = __import__ ( osname ) <EOL> dst = self . copy_only ( src , self . ok_posix_names ) <EOL> dst . environ = e = { } <EOL> for key , value in os . environ . items ( ) : <EOL> e [ key ] = value <EOL> def make_sys ( self ) : <EOL> m = self . copy_only ( sys , self . ok_sys_names ) <EOL> m . modules = self . modules <EOL> m . argv = [ '<STR_LIT>' ] <EOL> m . path = map ( None , self . ok_path ) <EOL> m . exc_info = self . r_exc_info <EOL> m = self . modules [ '<STR_LIT>' ] <EOL> l = self . modules . keys ( ) + list ( self . ok_builtin_modules ) <EOL> l . sort ( ) <EOL> m . builtin_module_names = tuple ( l ) <EOL> def copy_except ( self , src , exceptions ) : <EOL> dst = self . copy_none ( src ) <EOL> for name in dir ( src ) : <EOL> setattr ( dst , name , getattr ( src , name ) ) <EOL> for name in exceptions : <EOL> try : <EOL> delattr ( dst , name ) <EOL> except AttributeError : <EOL> pass <EOL> return dst <EOL> def copy_only ( self , src , names ) : <EOL> dst = self . copy_none ( src ) <EOL> for name in names : <EOL> try : <EOL> value = getattr ( src , name ) <EOL> except AttributeError : <EOL> continue <EOL> setattr ( dst , name , value ) <EOL> return dst <EOL> def copy_none ( self , src ) : <EOL> m = self . add_module ( src . __name__ ) <EOL> m . __doc__ = src . __doc__ <EOL> return m <EOL> def add_module ( self , mname ) : <EOL> m = self . modules . get ( mname ) <EOL> if m is None : <EOL> self . modules [ mname ] = m = self . hooks . new_module ( mname ) <EOL> m . __builtins__ = self . modules [ '<STR_LIT>' ] <EOL> return m <EOL> def r_exec ( self , code ) : <EOL> """<STR_LIT>""" <EOL> m = self . add_module ( '<STR_LIT:__main__>' ) <EOL> exec code in m . __dict__ <EOL> def r_eval ( self , code ) : <EOL> """<STR_LIT>""" <EOL> m = self . add_module ( '<STR_LIT:__main__>' ) <EOL> return eval ( code , m . __dict__ ) <EOL> def r_execfile ( self , file ) : <EOL> """<STR_LIT>""" <EOL> m = self . add_module ( '<STR_LIT:__main__>' ) <EOL> execfile ( file , m . __dict__ ) <EOL> def r_import ( self , mname , globals = { } , locals = { } , fromlist = [ ] ) : <EOL> """<STR_LIT>""" <EOL> return self . importer . import_module ( mname , globals , locals , fromlist ) <EOL> def r_reload ( self , m ) : <EOL> """<STR_LIT>""" <EOL> return self . importer . reload ( m ) <EOL> def r_unload ( self , m ) : <EOL> """<STR_LIT>""" <EOL> return self . importer . unload ( m ) <EOL> def make_delegate_files ( self ) : <EOL> s = self . modules [ '<STR_LIT>' ] <EOL> self . delegate_stdin = FileDelegate ( s , '<STR_LIT>' ) <EOL> self . delegate_stdout = FileDelegate ( s , '<STR_LIT>' ) <EOL> self . delegate_stderr = FileDelegate ( s , '<STR_LIT>' ) <EOL> self . restricted_stdin = FileWrapper ( sys . stdin ) <EOL> self . restricted_stdout = FileWrapper ( sys . stdout ) <EOL> self . restricted_stderr = FileWrapper ( sys . stderr ) <EOL> def set_files ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . save_files ( ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . make_delegate_files ( ) <EOL> s = self . modules [ '<STR_LIT>' ] <EOL> s . stdin = self . restricted_stdin <EOL> s . stdout = self . restricted_stdout <EOL> s . stderr = self . restricted_stderr <EOL> sys . stdin = self . delegate_stdin <EOL> sys . stdout = self . delegate_stdout <EOL> sys . stderr = self . delegate_stderr <EOL> def reset_files ( self ) : <EOL> self . restore_files ( ) <EOL> s = self . modules [ '<STR_LIT>' ] <EOL> self . restricted_stdin = s . stdin <EOL> self . restricted_stdout = s . stdout <EOL> self . restricted_stderr = s . stderr <EOL> def save_files ( self ) : <EOL> self . save_stdin = sys . stdin <EOL> self . save_stdout = sys . stdout <EOL> self . save_stderr = sys . stderr <EOL> def restore_files ( self ) : <EOL> sys . stdin = self . save_stdin <EOL> sys . stdout = self . save_stdout <EOL> sys . stderr = self . save_stderr <EOL> def s_apply ( self , func , args = ( ) , kw = { } ) : <EOL> self . save_files ( ) <EOL> try : <EOL> self . set_files ( ) <EOL> r = func ( * args , ** kw ) <EOL> finally : <EOL> self . restore_files ( ) <EOL> return r <EOL> def s_exec ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . s_apply ( self . r_exec , args ) <EOL> def s_eval ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . s_apply ( self . r_eval , args ) <EOL> def s_execfile ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . s_apply ( self . r_execfile , args ) <EOL> def s_import ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . s_apply ( self . r_import , args ) <EOL> def s_reload ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . s_apply ( self . r_reload , args ) <EOL> def s_unload ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . s_apply ( self . r_unload , args ) <EOL> def r_open ( self , file , mode = '<STR_LIT:r>' , buf = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> mode = str ( mode ) <EOL> if mode not in ( '<STR_LIT:r>' , '<STR_LIT:rb>' ) : <EOL> raise IOError , "<STR_LIT>" <EOL> return open ( file , mode , buf ) <EOL> def r_exc_info ( self ) : <EOL> ty , va , tr = sys . exc_info ( ) <EOL> tr = None <EOL> return ty , va , tr <EOL> def test ( ) : <EOL> import getopt , traceback <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' ) <EOL> verbose = <NUM_LIT:0> <EOL> trusted = [ ] <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : <EOL> verbose = verbose + <NUM_LIT:1> <EOL> if o == '<STR_LIT>' : <EOL> trusted . append ( a ) <EOL> r = RExec ( verbose = verbose ) <EOL> if trusted : <EOL> r . ok_builtin_modules = r . ok_builtin_modules + tuple ( trusted ) <EOL> if args : <EOL> r . modules [ '<STR_LIT>' ] . argv = args <EOL> r . modules [ '<STR_LIT>' ] . path . insert ( <NUM_LIT:0> , os . path . dirname ( args [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> r . modules [ '<STR_LIT>' ] . path . insert ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> fp = sys . stdin <EOL> if args and args [ <NUM_LIT:0> ] != '<STR_LIT:->' : <EOL> try : <EOL> fp = open ( args [ <NUM_LIT:0> ] ) <EOL> except IOError , msg : <EOL> print "<STR_LIT>" % ( sys . argv [ <NUM_LIT:0> ] , args [ <NUM_LIT:0> ] ) <EOL> return <NUM_LIT:1> <EOL> if fp . isatty ( ) : <EOL> try : <EOL> import readline <EOL> except ImportError : <EOL> pass <EOL> import code <EOL> class RestrictedConsole ( code . InteractiveConsole ) : <EOL> def runcode ( self , co ) : <EOL> self . locals [ '<STR_LIT>' ] = r . modules [ '<STR_LIT>' ] <EOL> r . s_apply ( code . InteractiveConsole . runcode , ( self , co ) ) <EOL> try : <EOL> RestrictedConsole ( r . modules [ '<STR_LIT:__main__>' ] . __dict__ ) . interact ( ) <EOL> except SystemExit , n : <EOL> return n <EOL> else : <EOL> text = fp . read ( ) <EOL> fp . close ( ) <EOL> c = compile ( text , fp . name , '<STR_LIT>' ) <EOL> try : <EOL> r . s_exec ( c ) <EOL> except SystemExit , n : <EOL> return n <EOL> except : <EOL> traceback . print_exc ( ) <EOL> return <NUM_LIT:1> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( test ( ) ) </s>
