<s> """<STR_LIT>""" <EOL> import sys <EOL> import imp <EOL> from pkgutil import read_code <EOL> try : <EOL> from imp import get_loader <EOL> except ImportError : <EOL> from pkgutil import get_loader <EOL> __all__ = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> ] <EOL> class _TempModule ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mod_name ) : <EOL> self . mod_name = mod_name <EOL> self . module = imp . new_module ( mod_name ) <EOL> self . _saved_module = [ ] <EOL> def __enter__ ( self ) : <EOL> mod_name = self . mod_name <EOL> try : <EOL> self . _saved_module . append ( sys . modules [ mod_name ] ) <EOL> except KeyError : <EOL> pass <EOL> sys . modules [ mod_name ] = self . module <EOL> return self <EOL> def __exit__ ( self , * args ) : <EOL> if self . _saved_module : <EOL> sys . modules [ self . mod_name ] = self . _saved_module [ <NUM_LIT:0> ] <EOL> else : <EOL> del sys . modules [ self . mod_name ] <EOL> self . _saved_module = [ ] <EOL> class _ModifiedArgv0 ( object ) : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> self . _saved_value = self . _sentinel = object ( ) <EOL> def __enter__ ( self ) : <EOL> if self . _saved_value is not self . _sentinel : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . _saved_value = sys . argv [ <NUM_LIT:0> ] <EOL> sys . argv [ <NUM_LIT:0> ] = self . value <EOL> def __exit__ ( self , * args ) : <EOL> self . value = self . _sentinel <EOL> sys . argv [ <NUM_LIT:0> ] = self . _saved_value <EOL> def _run_code ( code , run_globals , init_globals = None , <EOL> mod_name = None , mod_fname = None , <EOL> mod_loader = None , pkg_name = None ) : <EOL> """<STR_LIT>""" <EOL> if init_globals is not None : <EOL> run_globals . update ( init_globals ) <EOL> run_globals . update ( __name__ = mod_name , <EOL> __file__ = mod_fname , <EOL> __loader__ = mod_loader , <EOL> __package__ = pkg_name ) <EOL> exec code in run_globals <EOL> return run_globals <EOL> def _run_module_code ( code , init_globals = None , <EOL> mod_name = None , mod_fname = None , <EOL> mod_loader = None , pkg_name = None ) : <EOL> """<STR_LIT>""" <EOL> with _TempModule ( mod_name ) as temp_module , _ModifiedArgv0 ( mod_fname ) : <EOL> mod_globals = temp_module . module . __dict__ <EOL> _run_code ( code , mod_globals , init_globals , <EOL> mod_name , mod_fname , mod_loader , pkg_name ) <EOL> return mod_globals . copy ( ) <EOL> def _get_filename ( loader , mod_name ) : <EOL> for attr in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> meth = getattr ( loader , attr , None ) <EOL> if meth is not None : <EOL> return meth ( mod_name ) <EOL> return None <EOL> def _get_module_details ( mod_name ) : <EOL> loader = get_loader ( mod_name ) <EOL> if loader is None : <EOL> raise ImportError ( "<STR_LIT>" % mod_name ) <EOL> if loader . is_package ( mod_name ) : <EOL> if mod_name == "<STR_LIT:__main__>" or mod_name . endswith ( "<STR_LIT>" ) : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> try : <EOL> pkg_main_name = mod_name + "<STR_LIT>" <EOL> return _get_module_details ( pkg_main_name ) <EOL> except ImportError , e : <EOL> raise ImportError ( ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) % ( e , mod_name ) ) <EOL> code = loader . get_code ( mod_name ) <EOL> if code is None : <EOL> raise ImportError ( "<STR_LIT>" % mod_name ) <EOL> filename = _get_filename ( loader , mod_name ) <EOL> return mod_name , loader , code , filename <EOL> def _get_main_module_details ( ) : <EOL> main_name = "<STR_LIT:__main__>" <EOL> try : <EOL> return _get_module_details ( main_name ) <EOL> except ImportError as exc : <EOL> if main_name in str ( exc ) : <EOL> raise ImportError ( "<STR_LIT>" % <EOL> ( main_name , sys . path [ <NUM_LIT:0> ] ) ) <EOL> raise <EOL> def _run_module_as_main ( mod_name , alter_argv = True ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if alter_argv or mod_name != "<STR_LIT:__main__>" : <EOL> mod_name , loader , code , fname = _get_module_details ( mod_name ) <EOL> else : <EOL> mod_name , loader , code , fname = _get_main_module_details ( ) <EOL> except ImportError as exc : <EOL> msg = "<STR_LIT>" % ( sys . executable , str ( exc ) ) <EOL> sys . exit ( msg ) <EOL> pkg_name = mod_name . rpartition ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> main_globals = sys . modules [ "<STR_LIT:__main__>" ] . __dict__ <EOL> if alter_argv : <EOL> sys . argv [ <NUM_LIT:0> ] = fname <EOL> return _run_code ( code , main_globals , None , <EOL> "<STR_LIT:__main__>" , fname , loader , pkg_name ) <EOL> def run_module ( mod_name , init_globals = None , <EOL> run_name = None , alter_sys = False ) : <EOL> """<STR_LIT>""" <EOL> mod_name , loader , code , fname = _get_module_details ( mod_name ) <EOL> if run_name is None : <EOL> run_name = mod_name <EOL> pkg_name = mod_name . rpartition ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> if alter_sys : <EOL> return _run_module_code ( code , init_globals , run_name , <EOL> fname , loader , pkg_name ) <EOL> else : <EOL> return _run_code ( code , { } , init_globals , run_name , <EOL> fname , loader , pkg_name ) <EOL> def _get_importer ( path_name ) : <EOL> """<STR_LIT>""" <EOL> cache = sys . path_importer_cache <EOL> try : <EOL> importer = cache [ path_name ] <EOL> except KeyError : <EOL> cache [ path_name ] = None <EOL> for hook in sys . path_hooks : <EOL> try : <EOL> importer = hook ( path_name ) <EOL> break <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> try : <EOL> importer = imp . NullImporter ( path_name ) <EOL> except ImportError : <EOL> return None <EOL> cache [ path_name ] = importer <EOL> return importer <EOL> def _get_code_from_file ( fname ) : <EOL> with open ( fname , "<STR_LIT:rb>" ) as f : <EOL> code = read_code ( f ) <EOL> if code is None : <EOL> with open ( fname , "<STR_LIT>" ) as f : <EOL> code = compile ( f . read ( ) , fname , '<STR_LIT>' ) <EOL> return code <EOL> def run_path ( path_name , init_globals = None , run_name = None ) : <EOL> """<STR_LIT>""" <EOL> if run_name is None : <EOL> run_name = "<STR_LIT>" <EOL> importer = _get_importer ( path_name ) <EOL> if isinstance ( importer , imp . NullImporter ) : <EOL> code = _get_code_from_file ( path_name ) <EOL> return _run_module_code ( code , init_globals , run_name , path_name ) <EOL> else : <EOL> sys . path . insert ( <NUM_LIT:0> , path_name ) <EOL> try : <EOL> main_name = "<STR_LIT:__main__>" <EOL> saved_main = sys . modules [ main_name ] <EOL> del sys . modules [ main_name ] <EOL> try : <EOL> mod_name , loader , code , fname = _get_main_module_details ( ) <EOL> finally : <EOL> sys . modules [ main_name ] = saved_main <EOL> pkg_name = "<STR_LIT>" <EOL> with _TempModule ( run_name ) as temp_module , _ModifiedArgv0 ( path_name ) : <EOL> mod_globals = temp_module . module . __dict__ <EOL> return _run_code ( code , mod_globals , init_globals , <EOL> run_name , fname , loader , pkg_name ) . copy ( ) <EOL> finally : <EOL> try : <EOL> sys . path . remove ( path_name ) <EOL> except ValueError : <EOL> pass <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> else : <EOL> del sys . argv [ <NUM_LIT:0> ] <EOL> _run_module_as_main ( sys . argv [ <NUM_LIT:0> ] ) </s>
