<s> import sys <EOL> import imp <EOL> import os <EOL> import unittest <EOL> from test import test_support <EOL> test_src = """<STR_LIT>""" <EOL> test_co = compile ( test_src , "<STR_LIT>" , "<STR_LIT>" ) <EOL> test_path = "<STR_LIT>" <EOL> class ImportTracker : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . imports = [ ] <EOL> def find_module ( self , fullname , path = None ) : <EOL> self . imports . append ( fullname ) <EOL> return None <EOL> class TestImporter : <EOL> modules = { <EOL> "<STR_LIT>" : ( False , test_co ) , <EOL> "<STR_LIT>" : ( True , test_co ) , <EOL> "<STR_LIT>" : ( True , test_co ) , <EOL> "<STR_LIT>" : ( False , test_co ) , <EOL> } <EOL> def __init__ ( self , path = test_path ) : <EOL> if path != test_path : <EOL> raise ImportError <EOL> self . path = path <EOL> def _get__path__ ( self ) : <EOL> raise NotImplementedError <EOL> def find_module ( self , fullname , path = None ) : <EOL> if fullname in self . modules : <EOL> return self <EOL> else : <EOL> return None <EOL> def load_module ( self , fullname ) : <EOL> ispkg , code = self . modules [ fullname ] <EOL> mod = imp . new_module ( fullname ) <EOL> sys . modules [ fullname ] = mod <EOL> mod . __file__ = "<STR_LIT>" % self . __class__ . __name__ <EOL> mod . __loader__ = self <EOL> if ispkg : <EOL> mod . __path__ = self . _get__path__ ( ) <EOL> exec code in mod . __dict__ <EOL> return mod <EOL> class MetaImporter ( TestImporter ) : <EOL> def _get__path__ ( self ) : <EOL> return [ ] <EOL> class PathImporter ( TestImporter ) : <EOL> def _get__path__ ( self ) : <EOL> return [ self . path ] <EOL> class ImportBlocker : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * namestoblock ) : <EOL> self . namestoblock = { } <EOL> [ self . namestoblock . __setitem__ ( x , None ) for x in namestoblock ] <EOL> def find_module ( self , fullname , path = None ) : <EOL> if fullname in self . namestoblock : <EOL> return self <EOL> return None <EOL> def load_module ( self , fullname ) : <EOL> raise ImportError , "<STR_LIT>" <EOL> class ImpWrapper : <EOL> def __init__ ( self , path = None ) : <EOL> if path is not None and not os . path . isdir ( path ) : <EOL> raise ImportError <EOL> self . path = path <EOL> def find_module ( self , fullname , path = None ) : <EOL> subname = fullname . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:1> ] <EOL> if subname != fullname and self . path is None : <EOL> return None <EOL> if self . path is None : <EOL> path = None <EOL> else : <EOL> path = [ self . path ] <EOL> try : <EOL> file , filename , stuff = imp . find_module ( subname , path ) <EOL> except ImportError : <EOL> return None <EOL> return ImpLoader ( file , filename , stuff ) <EOL> class ImpLoader : <EOL> def __init__ ( self , file , filename , stuff ) : <EOL> self . file = file <EOL> self . filename = filename <EOL> self . stuff = stuff <EOL> def load_module ( self , fullname ) : <EOL> mod = imp . load_module ( fullname , self . file , self . filename , self . stuff ) <EOL> if self . file : <EOL> self . file . close ( ) <EOL> mod . __loader__ = self <EOL> return mod <EOL> class ImportHooksBaseTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . path = sys . path [ : ] <EOL> self . meta_path = sys . meta_path [ : ] <EOL> self . path_hooks = sys . path_hooks [ : ] <EOL> sys . path_importer_cache . clear ( ) <EOL> self . tracker = ImportTracker ( ) <EOL> sys . meta_path . insert ( <NUM_LIT:0> , self . tracker ) <EOL> def tearDown ( self ) : <EOL> sys . path [ : ] = self . path <EOL> sys . meta_path [ : ] = self . meta_path <EOL> sys . path_hooks [ : ] = self . path_hooks <EOL> sys . path_importer_cache . clear ( ) <EOL> for fullname in self . tracker . imports : <EOL> if fullname in sys . modules : <EOL> del sys . modules [ fullname ] <EOL> class ImportHooksTestCase ( ImportHooksBaseTestCase ) : <EOL> def doTestImports ( self , importer = None ) : <EOL> import hooktestmodule <EOL> import hooktestpackage <EOL> import hooktestpackage . sub <EOL> import hooktestpackage . sub . subber <EOL> self . assertEqual ( hooktestmodule . get_name ( ) , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( hooktestpackage . get_name ( ) , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( hooktestpackage . sub . get_name ( ) , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( hooktestpackage . sub . subber . get_name ( ) , <EOL> "<STR_LIT>" ) <EOL> if importer : <EOL> self . assertEqual ( hooktestmodule . __loader__ , importer ) <EOL> self . assertEqual ( hooktestpackage . __loader__ , importer ) <EOL> self . assertEqual ( hooktestpackage . sub . __loader__ , importer ) <EOL> self . assertEqual ( hooktestpackage . sub . subber . __loader__ , importer ) <EOL> def testMetaPath ( self ) : <EOL> i = MetaImporter ( ) <EOL> sys . meta_path . append ( i ) <EOL> self . doTestImports ( i ) <EOL> def testPathHook ( self ) : <EOL> sys . path_hooks . append ( PathImporter ) <EOL> sys . path . append ( test_path ) <EOL> self . doTestImports ( ) <EOL> def testBlocker ( self ) : <EOL> mname = "<STR_LIT>" <EOL> if mname in sys . modules : <EOL> del sys . modules [ mname ] <EOL> sys . meta_path . append ( ImportBlocker ( mname ) ) <EOL> try : <EOL> __import__ ( mname ) <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" % mname ) <EOL> def doTestImpWrapper ( self , globals_func = globals , locals_func = locals ) : <EOL> i = ImpWrapper ( ) <EOL> sys . meta_path . append ( i ) <EOL> sys . path_hooks . append ( ImpWrapper ) <EOL> mnames = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> for mname in mnames : <EOL> parent = mname . split ( "<STR_LIT:.>" ) [ <NUM_LIT:0> ] <EOL> for n in sys . modules . keys ( ) : <EOL> if n . startswith ( parent ) : <EOL> del sys . modules [ n ] <EOL> for mname in mnames : <EOL> m = __import__ ( mname , globals_func ( ) , locals_func ( ) , [ "<STR_LIT>" ] ) <EOL> m . __loader__ <EOL> def testImpWrapper ( self ) : <EOL> self . doTestImpWrapper ( ) <EOL> def testImpWrapperNoContext ( self ) : <EOL> self . doTestImpWrapper ( lambda : None , lambda : None ) <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( ImportHooksTestCase ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
