<s> """<STR_LIT>""" <EOL> import sys <EOL> import imp <EOL> try : <EOL> from imp import get_loader <EOL> except ImportError : <EOL> from pkgutil import get_loader <EOL> __all__ = [ <EOL> "<STR_LIT>" , <EOL> ] <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> temp_module = imp . new_module ( mod_name ) <EOL> mod_globals = temp_module . __dict__ <EOL> saved_argv0 = sys . argv [ <NUM_LIT:0> ] <EOL> restore_module = mod_name in sys . modules <EOL> if restore_module : <EOL> saved_module = sys . modules [ mod_name ] <EOL> sys . argv [ <NUM_LIT:0> ] = mod_fname <EOL> sys . modules [ mod_name ] = temp_module <EOL> try : <EOL> _run_code ( code , mod_globals , init_globals , <EOL> mod_name , mod_fname , <EOL> mod_loader , pkg_name ) <EOL> finally : <EOL> sys . argv [ <NUM_LIT:0> ] = saved_argv0 <EOL> if restore_module : <EOL> sys . modules [ mod_name ] = saved_module <EOL> else : <EOL> del sys . modules [ mod_name ] <EOL> return mod_globals . copy ( ) <EOL> def _get_filename ( loader , mod_name ) : <EOL> try : <EOL> get_filename = loader . get_filename <EOL> except AttributeError : <EOL> return None <EOL> else : <EOL> return get_filename ( mod_name ) <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> raise ImportError ( ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) % 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 loader , code , filename <EOL> def _run_module_as_main ( mod_name , set_argv0 = True ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> loader , code , fname = _get_module_details ( mod_name ) <EOL> except ImportError as exc : <EOL> if set_argv0 : <EOL> info = str ( exc ) <EOL> else : <EOL> info = "<STR_LIT>" % sys . argv [ <NUM_LIT:0> ] <EOL> msg = "<STR_LIT>" % ( sys . executable , info ) <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 set_argv0 : <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> 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> 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>
