<s> import unittest <EOL> import os <EOL> import os . path <EOL> import sys <EOL> import tempfile <EOL> from test . test_support import verbose , run_unittest <EOL> from runpy import _run_module_code , run_module <EOL> class RunModuleCodeTest ( unittest . TestCase ) : <EOL> expected_result = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> test_source = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_run_module_code ( self ) : <EOL> initial = object ( ) <EOL> name = "<STR_LIT>" <EOL> file = "<STR_LIT>" <EOL> loader = "<STR_LIT>" <EOL> d1 = dict ( initial = initial ) <EOL> saved_argv0 = sys . argv [ <NUM_LIT:0> ] <EOL> d2 = _run_module_code ( self . test_source , <EOL> d1 , <EOL> name , <EOL> file , <EOL> loader , <EOL> True ) <EOL> self . failUnless ( "<STR_LIT:result>" not in d1 ) <EOL> self . failUnless ( d2 [ "<STR_LIT>" ] is initial ) <EOL> self . failUnless ( d2 [ "<STR_LIT:result>" ] == self . expected_result ) <EOL> self . failUnless ( d2 [ "<STR_LIT>" ] [ "<STR_LIT:x>" ] == <NUM_LIT:1> ) <EOL> self . failUnless ( d2 [ "<STR_LIT>" ] is name ) <EOL> self . failUnless ( d2 [ "<STR_LIT>" ] is name ) <EOL> self . failUnless ( d2 [ "<STR_LIT>" ] is file ) <EOL> self . failUnless ( d2 [ "<STR_LIT>" ] is file ) <EOL> self . failUnless ( d2 [ "<STR_LIT>" ] is loader ) <EOL> self . failUnless ( sys . argv [ <NUM_LIT:0> ] is saved_argv0 ) <EOL> self . failUnless ( name not in sys . modules ) <EOL> def test_run_module_code_defaults ( self ) : <EOL> saved_argv0 = sys . argv [ <NUM_LIT:0> ] <EOL> d = _run_module_code ( self . test_source ) <EOL> self . failUnless ( d [ "<STR_LIT:result>" ] == self . expected_result ) <EOL> self . failUnless ( d [ "<STR_LIT>" ] is None ) <EOL> self . failUnless ( d [ "<STR_LIT>" ] is None ) <EOL> self . failUnless ( d [ "<STR_LIT>" ] is None ) <EOL> self . failUnless ( d [ "<STR_LIT>" ] is saved_argv0 ) <EOL> self . failUnless ( "<STR_LIT>" not in d ) <EOL> self . failUnless ( sys . argv [ <NUM_LIT:0> ] is saved_argv0 ) <EOL> class RunModuleTest ( unittest . TestCase ) : <EOL> def expect_import_error ( self , mod_name ) : <EOL> try : <EOL> run_module ( mod_name ) <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" + mod_name ) <EOL> def test_invalid_names ( self ) : <EOL> self . expect_import_error ( "<STR_LIT>" ) <EOL> self . expect_import_error ( "<STR_LIT>" ) <EOL> self . expect_import_error ( "<STR_LIT>" ) <EOL> self . expect_import_error ( "<STR_LIT>" ) <EOL> self . expect_import_error ( "<STR_LIT>" ) <EOL> self . expect_import_error ( "<STR_LIT>" ) <EOL> def test_library_module ( self ) : <EOL> run_module ( "<STR_LIT>" ) <EOL> def _make_pkg ( self , source , depth ) : <EOL> pkg_name = "<STR_LIT>" <EOL> init_fname = "<STR_LIT>" + os . extsep + "<STR_LIT>" <EOL> test_fname = "<STR_LIT>" + os . extsep + "<STR_LIT>" <EOL> pkg_dir = sub_dir = tempfile . mkdtemp ( ) <EOL> if verbose : print "<STR_LIT>" , sub_dir <EOL> sys . path . insert ( <NUM_LIT:0> , pkg_dir ) <EOL> if verbose : print "<STR_LIT>" , sys . path [ <NUM_LIT:0> ] <EOL> for i in range ( depth ) : <EOL> sub_dir = os . path . join ( sub_dir , pkg_name ) <EOL> os . mkdir ( sub_dir ) <EOL> if verbose : print "<STR_LIT>" , sub_dir <EOL> pkg_fname = os . path . join ( sub_dir , init_fname ) <EOL> pkg_file = open ( pkg_fname , "<STR_LIT:w>" ) <EOL> pkg_file . close ( ) <EOL> if verbose : print "<STR_LIT>" , pkg_fname <EOL> mod_fname = os . path . join ( sub_dir , test_fname ) <EOL> mod_file = open ( mod_fname , "<STR_LIT:w>" ) <EOL> mod_file . write ( source ) <EOL> mod_file . close ( ) <EOL> if verbose : print "<STR_LIT>" , mod_fname <EOL> mod_name = ( pkg_name + "<STR_LIT:.>" ) * depth + "<STR_LIT>" <EOL> return pkg_dir , mod_fname , mod_name <EOL> def _del_pkg ( self , top , depth , mod_name ) : <EOL> for i in range ( depth + <NUM_LIT:1> ) : <EOL> parts = mod_name . rsplit ( "<STR_LIT:.>" , i ) <EOL> entry = parts [ <NUM_LIT:0> ] <EOL> try : <EOL> del sys . modules [ entry ] <EOL> except KeyError , ex : <EOL> if verbose : print ex <EOL> if verbose : print "<STR_LIT>" <EOL> del sys . path [ <NUM_LIT:0> ] <EOL> if verbose : print "<STR_LIT>" <EOL> for root , dirs , files in os . walk ( top , topdown = False ) : <EOL> for name in files : <EOL> try : <EOL> os . remove ( os . path . join ( root , name ) ) <EOL> except OSError , ex : <EOL> if verbose : print ex <EOL> for name in dirs : <EOL> fullname = os . path . join ( root , name ) <EOL> try : <EOL> os . rmdir ( fullname ) <EOL> except OSError , ex : <EOL> if verbose : print ex <EOL> try : <EOL> os . rmdir ( top ) <EOL> if verbose : print "<STR_LIT>" <EOL> except OSError , ex : <EOL> if verbose : print ex <EOL> def _check_module ( self , depth ) : <EOL> pkg_dir , mod_fname , mod_name = ( <EOL> self . _make_pkg ( "<STR_LIT>" , depth ) ) <EOL> try : <EOL> if verbose : print "<STR_LIT>" , mod_name <EOL> d1 = run_module ( mod_name ) <EOL> self . failUnless ( d1 [ "<STR_LIT:x>" ] == <NUM_LIT:1> ) <EOL> del d1 <EOL> __import__ ( mod_name ) <EOL> os . remove ( mod_fname ) <EOL> if verbose : print "<STR_LIT>" , mod_name <EOL> d2 = run_module ( mod_name ) <EOL> self . failUnless ( d2 [ "<STR_LIT:x>" ] == <NUM_LIT:1> ) <EOL> del d2 <EOL> finally : <EOL> self . _del_pkg ( pkg_dir , depth , mod_name ) <EOL> if verbose : print "<STR_LIT>" <EOL> def test_run_module ( self ) : <EOL> for depth in range ( <NUM_LIT:4> ) : <EOL> if verbose : print "<STR_LIT>" , depth <EOL> self . _check_module ( depth ) <EOL> def test_main ( ) : <EOL> run_unittest ( RunModuleCodeTest ) <EOL> run_unittest ( RunModuleTest ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
