<s> '''<STR_LIT>''' <EOL> import unittest <EOL> import exceptions <EOL> import imp <EOL> import __builtin__ <EOL> import sys <EOL> import types <EOL> origImport = __import__ <EOL> class TestImportStatementError ( exceptions . ImportError ) : <EOL> def __init__ ( self , args ) : <EOL> names = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . len = len ( args ) <EOL> for a in args : <EOL> n = names . pop ( <NUM_LIT:0> ) <EOL> setattr ( self , n , a ) <EOL> for n in names : <EOL> setattr ( self , n , None ) <EOL> class TestImportStatement ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> AI = "<STR_LIT>" <EOL> def importFunction ( * args ) : <EOL> if args [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> return origImport ( * args ) <EOL> raise TestImportStatementError ( args ) <EOL> importFunction = staticmethod ( importFunction ) <EOL> def setUp ( self ) : <EOL> __builtin__ . __import__ = self . importFunction <EOL> def tearDown ( self ) : <EOL> __builtin__ . __import__ = origImport <EOL> def runImport ( self , statement ) : <EOL> l = { } <EOL> g = { } <EOL> try : <EOL> exec statement in g , l <EOL> except TestImportStatementError , e : <EOL> self . assert_ ( e . globals is g , "<STR_LIT>" ) <EOL> self . assert_ ( e . locals is l , "<STR_LIT>" ) <EOL> return e <EOL> self . fail ( "<STR_LIT>" ) <EOL> def testFromDotsOnly ( self ) : <EOL> dots = '<STR_LIT>' <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> dots += '<STR_LIT:.>' <EOL> a = self . runImport ( "<STR_LIT>" % ( dots , ) ) <EOL> self . assertEqual ( a . len , <NUM_LIT:5> ) <EOL> self . assertEqual ( a . name , "<STR_LIT>" ) <EOL> self . assertEqual ( a . level , i ) <EOL> self . assertEqual ( a . fromlist , ( '<STR_LIT:A>' , '<STR_LIT:B>' ) ) <EOL> def testFromDotsOnlyAs ( self ) : <EOL> dots = '<STR_LIT>' <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> dots += '<STR_LIT:.>' <EOL> a = self . runImport ( "<STR_LIT>" % ( dots , ) ) <EOL> self . assertEqual ( a . len , <NUM_LIT:5> ) <EOL> self . assertEqual ( a . name , "<STR_LIT>" ) <EOL> self . assertEqual ( a . fromlist , ( '<STR_LIT:A>' , ) ) <EOL> self . assertEqual ( a . level , i ) <EOL> def testFromDotsAndName ( self ) : <EOL> dots = '<STR_LIT>' <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> dots += '<STR_LIT:.>' <EOL> a = self . runImport ( "<STR_LIT>" % ( dots , ) ) <EOL> self . assertEqual ( a . len , <NUM_LIT:5> ) <EOL> self . assertEqual ( a . name , "<STR_LIT:X>" ) <EOL> self . assertEqual ( a . fromlist , ( '<STR_LIT:A>' , ) ) <EOL> self . assertEqual ( a . level , i ) <EOL> def testFromDotsAndDotedName ( self ) : <EOL> dots = '<STR_LIT>' <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> dots += '<STR_LIT:.>' <EOL> a = self . runImport ( "<STR_LIT>" % ( dots , ) ) <EOL> self . assertEqual ( a . len , <NUM_LIT:5> ) <EOL> self . assertEqual ( a . name , "<STR_LIT>" ) <EOL> self . assertEqual ( a . fromlist , ( '<STR_LIT:A>' , ) ) <EOL> self . assertEqual ( a . level , i ) <EOL> def testAbsoluteFromDotedNameAs ( self ) : <EOL> a = self . runImport ( self . AI + "<STR_LIT>" ) <EOL> self . assertEqual ( a . len , <NUM_LIT:5> ) <EOL> self . assertEqual ( a . name , "<STR_LIT>" ) <EOL> self . assertEqual ( a . fromlist , ( '<STR_LIT:A>' , ) ) <EOL> self . assertEqual ( a . level , <NUM_LIT:0> ) <EOL> def testRelativeOrAbsoluteFromDotedNameAs ( self ) : <EOL> a = self . runImport ( "<STR_LIT>" ) <EOL> self . assertEqual ( a . name , "<STR_LIT>" ) <EOL> self . assertEqual ( a . fromlist , ( '<STR_LIT:A>' , ) ) <EOL> self . assertEqual ( a . len , <NUM_LIT:4> ) <EOL> def testAbsoluteFromDotedNameAll ( self ) : <EOL> a = self . runImport ( self . AI + "<STR_LIT>" ) <EOL> self . assertEqual ( a . len , <NUM_LIT:5> ) <EOL> self . assertEqual ( a . name , "<STR_LIT>" ) <EOL> self . assertEqual ( a . fromlist , ( '<STR_LIT:*>' , ) ) <EOL> self . assertEqual ( a . level , <NUM_LIT:0> ) <EOL> def testRelativeOrAbsoluteFromDotedNameAll ( self ) : <EOL> a = self . runImport ( "<STR_LIT>" ) <EOL> self . assertEqual ( a . name , "<STR_LIT>" ) <EOL> self . assertEqual ( a . fromlist , ( '<STR_LIT:*>' , ) ) <EOL> self . assertEqual ( a . len , <NUM_LIT:4> ) <EOL> def testAbsoluteImportName ( self ) : <EOL> a = self . runImport ( self . AI + "<STR_LIT>" ) <EOL> self . assertEqual ( a . len , <NUM_LIT:5> ) <EOL> self . assertEqual ( a . name , "<STR_LIT:X>" ) <EOL> self . assertEqual ( a . fromlist , None ) <EOL> self . assertEqual ( a . level , <NUM_LIT:0> ) <EOL> def testAbsoluteImportDotedName ( self ) : <EOL> a = self . runImport ( self . AI + "<STR_LIT>" ) <EOL> self . assertEqual ( a . len , <NUM_LIT:5> ) <EOL> self . assertEqual ( a . name , "<STR_LIT>" ) <EOL> self . assertEqual ( a . fromlist , None ) <EOL> self . assertEqual ( a . level , <NUM_LIT:0> ) <EOL> def testRelativeOrAbsoluteImportName ( self ) : <EOL> a = self . runImport ( "<STR_LIT>" ) <EOL> self . assertEqual ( a . name , "<STR_LIT:X>" ) <EOL> self . assertEqual ( a . fromlist , None ) <EOL> self . assertEqual ( a . len , <NUM_LIT:4> ) <EOL> def testRelativeOrAbsoluteImportDotedName ( self ) : <EOL> a = self . runImport ( "<STR_LIT>" ) <EOL> self . assertEqual ( a . name , "<STR_LIT>" ) <EOL> self . assertEqual ( a . fromlist , None ) <EOL> self . assertEqual ( a . len , <NUM_LIT:4> ) <EOL> def testAbsoluteImportDotedNameAs ( self ) : <EOL> a = self . runImport ( self . AI + "<STR_LIT>" ) <EOL> self . assertEqual ( a . len , <NUM_LIT:5> ) <EOL> self . assertEqual ( a . name , "<STR_LIT>" ) <EOL> self . assertEqual ( a . fromlist , None ) <EOL> self . assertEqual ( a . level , <NUM_LIT:0> ) <EOL> class TestImportFunctionError ( exceptions . ImportError ) : <EOL> pass <EOL> class TestImportFunction ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> nameX = "<STR_LIT>" <EOL> def setUp ( self ) : <EOL> self . modX = imp . new_module ( self . nameX ) <EOL> self . modX . __path__ = [ '<STR_LIT:X>' ] <EOL> self . modX2 = imp . new_module ( self . nameX + "<STR_LIT:2>" ) <EOL> self . modY = imp . new_module ( self . nameX + "<STR_LIT>" ) <EOL> self . modY . __path__ = [ '<STR_LIT>' ] <EOL> self . modY2 = imp . new_module ( self . nameX + "<STR_LIT>" ) <EOL> self . modY2 . __path__ = [ '<STR_LIT>' ] <EOL> self . modZ1 = imp . new_module ( self . nameX + "<STR_LIT>" ) <EOL> self . modZ2 = imp . new_module ( self . nameX + "<STR_LIT>" ) <EOL> self . expected = "<STR_LIT>" <EOL> sys . meta_path . insert ( <NUM_LIT:0> , self ) <EOL> def tearDown ( self ) : <EOL> try : <EOL> sys . meta_path . remove ( self ) <EOL> except ValueError : <EOL> pass <EOL> for k in sys . modules . keys ( ) : <EOL> if k . startswith ( self . nameX ) : <EOL> del sys . modules [ k ] <EOL> def importX ( self ) : <EOL> sys . modules [ self . modX . __name__ ] = self . modX <EOL> def importX2 ( self ) : <EOL> sys . modules [ self . modX2 . __name__ ] = self . modX2 <EOL> def importY ( self ) : <EOL> self . importX ( ) <EOL> sys . modules [ self . modY . __name__ ] = self . modY <EOL> self . modX . Y = self . modY <EOL> def importY2 ( self ) : <EOL> self . importX ( ) <EOL> sys . modules [ self . modY2 . __name__ ] = self . modY2 <EOL> self . modX . Y2 = self . modY2 <EOL> def importZ1 ( self ) : <EOL> self . importY ( ) <EOL> sys . modules [ self . modZ1 . __name__ ] = self . modZ1 <EOL> self . modY . Z1 = self . modZ1 <EOL> def top ( self ) : <EOL> if sys . modules . has_key ( "<STR_LIT:__main__>" ) : <EOL> return sys . modules [ "<STR_LIT:__main__>" ] . __dict__ <EOL> return globals ( ) <EOL> def find_module ( self , fullname , path = None ) : <EOL> if self . expected and self . expected != fullname : <EOL> return None <EOL> self . fullname = fullname <EOL> self . path = path <EOL> return self <EOL> def load_module ( self , fullname ) : <EOL> self . assertEqual ( fullname , self . fullname ) <EOL> raise TestImportFunctionError ( ) <EOL> def runImport ( self , expected , name , globals , fromlist = None , level = None ) : <EOL> self . expected = expected <EOL> if isinstance ( globals , types . ModuleType ) : <EOL> globals = globals . __dict__ <EOL> try : <EOL> if level is not None : <EOL> __import__ ( name , globals , None , fromlist , level ) <EOL> else : <EOL> __import__ ( name , globals , None , fromlist ) <EOL> except TestImportFunctionError : <EOL> return <EOL> self . fail ( "<STR_LIT>" ) <EOL> def testRelativeOrAbsolute_top_X2_1 ( self ) : <EOL> self . runImport ( None , self . modX2 . __name__ , self . top ( ) ) <EOL> self . assertEqual ( self . fullname , self . modX2 . __name__ ) <EOL> self . assertEqual ( self . path , None ) <EOL> def testRelativeOrAbsolute_top_X2_2 ( self ) : <EOL> self . runImport ( None , self . modX2 . __name__ , self . top ( ) , None , - <NUM_LIT:1> ) <EOL> self . assertEqual ( self . fullname , self . modX2 . __name__ ) <EOL> self . assertEqual ( self . path , None ) <EOL> def testRelativeOrAbsolute_top_Y_1 ( self ) : <EOL> self . importX ( ) <EOL> self . runImport ( None , self . modY . __name__ , self . top ( ) ) <EOL> self . assertEqual ( self . fullname , self . modY . __name__ ) <EOL> self . assertEqual ( self . path , [ '<STR_LIT:X>' ] ) <EOL> def testRelativeOrAbsolute_top_Y_2 ( self ) : <EOL> self . importX ( ) <EOL> self . runImport ( None , self . modY . __name__ , self . top ( ) , None , - <NUM_LIT:1> ) <EOL> self . assertEqual ( self . fullname , self . modY . __name__ ) <EOL> self . assertEqual ( self . path , [ '<STR_LIT:X>' ] ) <EOL> def testAbsolute_top_X2 ( self ) : <EOL> self . runImport ( None , self . modX2 . __name__ , globals ( ) , None , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . fullname , self . modX2 . __name__ ) <EOL> self . assertEqual ( self . path , None ) <EOL> def testAbsolute_top_Y ( self ) : <EOL> self . importX ( ) <EOL> self . runImport ( None , self . modY . __name__ , globals ( ) , None , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . fullname , self . modY . __name__ ) <EOL> self . assertEqual ( self . path , [ '<STR_LIT:X>' ] ) <EOL> def testRelativeOrAbsolute_X_X2_rel1 ( self ) : <EOL> self . importX ( ) <EOL> self . runImport ( None , self . modX2 . __name__ , self . modX ) <EOL> self . assertEqual ( self . fullname , self . nameX + "<STR_LIT:.>" + self . modX2 . __name__ ) <EOL> self . assertEqual ( self . path , [ '<STR_LIT:X>' ] ) <EOL> def testRelativeOrAbsolute_X_X2_rel2 ( self ) : <EOL> self . importX ( ) <EOL> self . runImport ( None , self . modX2 . __name__ , self . modX , None , - <NUM_LIT:1> ) <EOL> self . assertEqual ( self . path , [ '<STR_LIT:X>' ] ) <EOL> self . assertEqual ( self . fullname , self . nameX + "<STR_LIT:.>" + self . modX2 . __name__ ) <EOL> def testRelativeOrAbsolute_X_X2_abs1 ( self ) : <EOL> self . importX ( ) <EOL> self . runImport ( self . modX2 . __name__ , self . modX2 . __name__ , self . modX ) <EOL> self . assertEqual ( self . fullname , self . modX2 . __name__ ) <EOL> self . assertEqual ( self . path , None ) <EOL> def testRelativeOrAbsolute_X_X2_abs2 ( self ) : <EOL> self . importX ( ) <EOL> self . runImport ( self . modX2 . __name__ , self . modX2 . __name__ , self . modX , None , - <NUM_LIT:1> ) <EOL> self . assertEqual ( self . path , None ) <EOL> self . assertEqual ( self . fullname , self . modX2 . __name__ ) <EOL> def testAbsolute_X_X2 ( self ) : <EOL> self . importX ( ) <EOL> self . runImport ( None , self . modX2 . __name__ , self . modX , None , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . fullname , self . modX2 . __name__ ) <EOL> self . assertEqual ( self . path , None ) <EOL> def testAbsolute_X_Y ( self ) : <EOL> self . importX ( ) <EOL> self . runImport ( None , self . modY . __name__ , self . modX , None , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . fullname , self . modY . __name__ ) <EOL> self . assertEqual ( self . path , [ '<STR_LIT:X>' ] ) <EOL> def testRelative_Z1_Z2 ( self ) : <EOL> self . importZ1 ( ) <EOL> self . runImport ( None , "<STR_LIT>" , self . modZ1 , [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . fullname , self . modZ2 . __name__ ) <EOL> self . assertEqual ( self . path , [ '<STR_LIT>' ] ) <EOL> def testRelative_Z1_Y2 ( self ) : <EOL> self . importZ1 ( ) <EOL> self . runImport ( None , "<STR_LIT>" , self . modZ1 , [ "<STR_LIT>" ] , <NUM_LIT:2> ) <EOL> self . assertEqual ( self . fullname , self . modX . __name__ + "<STR_LIT>" ) <EOL> self . assertEqual ( self . path , [ '<STR_LIT:X>' ] ) <EOL> def testRelative_Z1_X2 ( self ) : <EOL> self . importZ1 ( ) <EOL> self . assertRaises ( ValueError , self . runImport , None , "<STR_LIT>" , self . modZ1 , [ self . modX2 . __name__ ] , <NUM_LIT:3> ) <EOL> def testRelative_X2_X ( self ) : <EOL> self . importX2 ( ) <EOL> self . assertRaises ( ValueError , self . runImport , None , "<STR_LIT>" , self . modX2 , [ self . modX . __name__ ] , <NUM_LIT:1> ) <EOL> def testRelative_X2_Y ( self ) : <EOL> self . importX2 ( ) <EOL> self . importX ( ) <EOL> self . assertRaises ( ValueError , self . runImport , None , self . modX . __name__ , self . modX2 , [ "<STR_LIT:Y>" ] , <NUM_LIT:1> ) <EOL> def testRelative_X_Z1_1 ( self ) : <EOL> self . importX ( ) <EOL> self . runImport ( None , "<STR_LIT:Y>" , self . modX , [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . fullname , self . modY . __name__ ) <EOL> self . assertEqual ( self . path , [ '<STR_LIT:X>' ] ) <EOL> def testRelative_X_Z1_2 ( self ) : <EOL> self . importY ( ) <EOL> self . runImport ( None , "<STR_LIT:Y>" , self . modX , [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . fullname , self . modZ1 . __name__ ) <EOL> self . assertEqual ( self . path , [ '<STR_LIT>' ] ) <EOL> def testRelative_Y_Z1 ( self ) : <EOL> self . importY ( ) <EOL> self . runImport ( None , "<STR_LIT>" , self . modY , [ '<STR_LIT:A>' , '<STR_LIT:B>' ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . fullname , self . modZ1 . __name__ ) <EOL> self . assertEqual ( self . path , [ '<STR_LIT>' ] ) <EOL> def testRelative_Y2_Z1_1 ( self ) : <EOL> self . importY2 ( ) <EOL> self . runImport ( None , "<STR_LIT:Y>" , self . modY2 , [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> self . assertEqual ( self . fullname , self . modY . __name__ ) <EOL> self . assertEqual ( self . path , [ '<STR_LIT:X>' ] ) <EOL> def testRelative_Y2_Z1_2 ( self ) : <EOL> self . importY2 ( ) <EOL> self . importY ( ) <EOL> self . runImport ( None , "<STR_LIT:Y>" , self . modY2 , [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> self . assertEqual ( self . fullname , self . modZ1 . __name__ ) <EOL> self . assertEqual ( self . path , [ '<STR_LIT>' ] ) <EOL> try : <EOL> from test import test_support <EOL> except ImportError : <EOL> test_main = unittest . main <EOL> else : <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( TestImportStatement , <EOL> TestImportFunction ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_main ( ) </s>
