<s> import os , sys , string , random , tempfile , unittest <EOL> from test . test_support import run_unittest <EOL> class TestImport ( unittest . TestCase ) : <EOL> def __init__ ( self , * args , ** kw ) : <EOL> self . package_name = '<STR_LIT>' <EOL> while self . package_name in sys . modules : <EOL> self . package_name += random . choose ( string . letters ) <EOL> self . module_name = self . package_name + '<STR_LIT>' <EOL> unittest . TestCase . __init__ ( self , * args , ** kw ) <EOL> def remove_modules ( self ) : <EOL> for module_name in ( self . package_name , self . module_name ) : <EOL> if module_name in sys . modules : <EOL> del sys . modules [ module_name ] <EOL> def setUp ( self ) : <EOL> self . test_dir = tempfile . mkdtemp ( ) <EOL> sys . path . append ( self . test_dir ) <EOL> self . package_dir = os . path . join ( self . test_dir , <EOL> self . package_name ) <EOL> os . mkdir ( self . package_dir ) <EOL> open ( os . path . join ( <EOL> self . package_dir , '<STR_LIT>' + os . extsep + '<STR_LIT>' ) , '<STR_LIT:w>' ) . close ( ) <EOL> self . module_path = os . path . join ( self . package_dir , '<STR_LIT:foo>' + os . extsep + '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> for file in os . listdir ( self . package_dir ) : <EOL> os . remove ( os . path . join ( self . package_dir , file ) ) <EOL> os . rmdir ( self . package_dir ) <EOL> os . rmdir ( self . test_dir ) <EOL> self . assertNotEqual ( sys . path . count ( self . test_dir ) , <NUM_LIT:0> ) <EOL> sys . path . remove ( self . test_dir ) <EOL> self . remove_modules ( ) <EOL> def rewrite_file ( self , contents ) : <EOL> for extension in "<STR_LIT>" : <EOL> compiled_path = self . module_path + extension <EOL> if os . path . exists ( compiled_path ) : <EOL> os . remove ( compiled_path ) <EOL> f = open ( self . module_path , '<STR_LIT:w>' ) <EOL> f . write ( contents ) <EOL> f . close ( ) <EOL> def test_package_import__semantics ( self ) : <EOL> self . rewrite_file ( '<STR_LIT>' ) <EOL> try : __import__ ( self . module_name ) <EOL> except SyntaxError : pass <EOL> else : raise RuntimeError , '<STR_LIT>' <EOL> self . assertNotIn ( self . module_name , sys . modules ) <EOL> self . assertFalse ( hasattr ( sys . modules [ self . package_name ] , '<STR_LIT:foo>' ) ) <EOL> var = '<STR_LIT:a>' <EOL> while var in dir ( __builtins__ ) : <EOL> var += random . choose ( string . letters ) <EOL> self . rewrite_file ( var ) <EOL> try : __import__ ( self . module_name ) <EOL> except NameError : pass <EOL> else : raise RuntimeError , '<STR_LIT>' <EOL> self . rewrite_file ( '<STR_LIT>' % var ) <EOL> module = __import__ ( self . module_name ) . foo <EOL> self . assertEqual ( getattr ( module , var ) , <NUM_LIT:1> ) <EOL> def test_main ( ) : <EOL> run_unittest ( TestImport ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
