<s> import imp <EOL> import os <EOL> import sys <EOL> import unittest <EOL> from importlib import import_module <EOL> from zipimport import zipimporter <EOL> from django . test import SimpleTestCase , TestCase , modify_settings <EOL> from django . test . utils import extend_sys_path <EOL> from django . utils import six <EOL> from django . utils . _os import upath <EOL> from django . utils . module_loading import ( <EOL> autodiscover_modules , import_string , module_has_submodule , <EOL> ) <EOL> class DefaultLoader ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> sys . meta_path . insert ( <NUM_LIT:0> , ProxyFinder ( ) ) <EOL> def tearDown ( self ) : <EOL> sys . meta_path . pop ( <NUM_LIT:0> ) <EOL> def test_loader ( self ) : <EOL> "<STR_LIT>" <EOL> test_module = import_module ( '<STR_LIT>' ) <EOL> test_no_submodule = import_module ( <EOL> '<STR_LIT>' ) <EOL> self . assertTrue ( module_has_submodule ( test_module , '<STR_LIT>' ) ) <EOL> mod = import_module ( '<STR_LIT>' ) <EOL> self . assertEqual ( mod . content , '<STR_LIT>' ) <EOL> self . assertTrue ( module_has_submodule ( test_module , '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ImportError ) : <EOL> import_module ( '<STR_LIT>' ) <EOL> self . assertFalse ( module_has_submodule ( test_module , '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ImportError ) : <EOL> import_module ( '<STR_LIT>' ) <EOL> self . assertFalse ( module_has_submodule ( test_module , '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ImportError ) : <EOL> import_module ( '<STR_LIT>' ) <EOL> import types <EOL> self . assertFalse ( module_has_submodule ( sys . modules [ '<STR_LIT>' ] , '<STR_LIT>' ) ) <EOL> self . assertFalse ( module_has_submodule ( test_no_submodule , '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ImportError ) : <EOL> import_module ( '<STR_LIT>' ) <EOL> class EggLoader ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . egg_dir = '<STR_LIT>' % os . path . dirname ( upath ( __file__ ) ) <EOL> def tearDown ( self ) : <EOL> sys . path_importer_cache . clear ( ) <EOL> sys . modules . pop ( '<STR_LIT>' , None ) <EOL> sys . modules . pop ( '<STR_LIT>' , None ) <EOL> sys . modules . pop ( '<STR_LIT>' , None ) <EOL> sys . modules . pop ( '<STR_LIT>' , None ) <EOL> sys . modules . pop ( '<STR_LIT>' , None ) <EOL> sys . modules . pop ( '<STR_LIT>' , None ) <EOL> sys . modules . pop ( '<STR_LIT>' , None ) <EOL> def test_shallow_loader ( self ) : <EOL> "<STR_LIT>" <EOL> egg_name = '<STR_LIT>' % self . egg_dir <EOL> with extend_sys_path ( egg_name ) : <EOL> egg_module = import_module ( '<STR_LIT>' ) <EOL> self . assertTrue ( module_has_submodule ( egg_module , '<STR_LIT>' ) ) <EOL> mod = import_module ( '<STR_LIT>' ) <EOL> self . assertEqual ( mod . content , '<STR_LIT>' ) <EOL> self . assertTrue ( module_has_submodule ( egg_module , '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ImportError ) : <EOL> import_module ( '<STR_LIT>' ) <EOL> self . assertFalse ( module_has_submodule ( egg_module , '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ImportError ) : <EOL> import_module ( '<STR_LIT>' ) <EOL> def test_deep_loader ( self ) : <EOL> "<STR_LIT>" <EOL> egg_name = '<STR_LIT>' % self . egg_dir <EOL> with extend_sys_path ( egg_name ) : <EOL> egg_module = import_module ( '<STR_LIT>' ) <EOL> self . assertTrue ( module_has_submodule ( egg_module , '<STR_LIT>' ) ) <EOL> mod = import_module ( '<STR_LIT>' ) <EOL> self . assertEqual ( mod . content , '<STR_LIT>' ) <EOL> self . assertTrue ( module_has_submodule ( egg_module , '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ImportError ) : <EOL> import_module ( '<STR_LIT>' ) <EOL> self . assertFalse ( module_has_submodule ( egg_module , '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ImportError ) : <EOL> import_module ( '<STR_LIT>' ) <EOL> class ModuleImportTestCase ( TestCase ) : <EOL> def test_import_string ( self ) : <EOL> cls = import_string ( '<STR_LIT>' ) <EOL> self . assertEqual ( cls , import_string ) <EOL> with self . assertRaises ( ImportError ) : <EOL> import_string ( '<STR_LIT>' ) <EOL> msg = '<STR_LIT>' <EOL> with self . assertRaisesMessage ( ImportError , msg ) : <EOL> import_string ( '<STR_LIT>' ) <EOL> @ modify_settings ( INSTALLED_APPS = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> class AutodiscoverModulesTestCase ( SimpleTestCase ) : <EOL> def tearDown ( self ) : <EOL> sys . path_importer_cache . clear ( ) <EOL> sys . modules . pop ( '<STR_LIT>' , None ) <EOL> sys . modules . pop ( '<STR_LIT>' , None ) <EOL> sys . modules . pop ( '<STR_LIT>' , None ) <EOL> sys . modules . pop ( '<STR_LIT>' , None ) <EOL> sys . modules . pop ( '<STR_LIT>' , None ) <EOL> def test_autodiscover_modules_found ( self ) : <EOL> autodiscover_modules ( '<STR_LIT>' ) <EOL> def test_autodiscover_modules_not_found ( self ) : <EOL> autodiscover_modules ( '<STR_LIT>' ) <EOL> def test_autodiscover_modules_found_but_bad_module ( self ) : <EOL> with six . assertRaisesRegex ( self , ImportError , "<STR_LIT>" ) : <EOL> autodiscover_modules ( '<STR_LIT>' ) <EOL> def test_autodiscover_modules_several_one_bad_module ( self ) : <EOL> with six . assertRaisesRegex ( self , ImportError , "<STR_LIT>" ) : <EOL> autodiscover_modules ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_autodiscover_modules_several_found ( self ) : <EOL> autodiscover_modules ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_autodiscover_modules_several_found_with_registry ( self ) : <EOL> from . test_module import site <EOL> autodiscover_modules ( '<STR_LIT>' , '<STR_LIT>' , register_to = site ) <EOL> self . assertEqual ( site . _registry , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_validate_registry_keeps_intact ( self ) : <EOL> from . test_module import site <EOL> with self . assertRaisesMessage ( Exception , "<STR_LIT>" ) : <EOL> autodiscover_modules ( '<STR_LIT>' , register_to = site ) <EOL> self . assertEqual ( site . _registry , { } ) <EOL> def test_validate_registry_resets_after_erroneous_module ( self ) : <EOL> from . test_module import site <EOL> with self . assertRaisesMessage ( Exception , "<STR_LIT>" ) : <EOL> autodiscover_modules ( '<STR_LIT>' , '<STR_LIT>' , register_to = site ) <EOL> self . assertEqual ( site . _registry , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_validate_registry_resets_after_missing_module ( self ) : <EOL> from . test_module import site <EOL> autodiscover_modules ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , register_to = site ) <EOL> self . assertEqual ( site . _registry , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> class ProxyFinder ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _cache = { } <EOL> def find_module ( self , fullname , path = None ) : <EOL> tail = fullname . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] <EOL> try : <EOL> fd , fn , info = imp . find_module ( tail , path ) <EOL> if fullname in self . _cache : <EOL> old_fd = self . _cache [ fullname ] [ <NUM_LIT:0> ] <EOL> if old_fd : <EOL> old_fd . close ( ) <EOL> self . _cache [ fullname ] = ( fd , fn , info ) <EOL> except ImportError : <EOL> return None <EOL> else : <EOL> return self <EOL> def load_module ( self , fullname ) : <EOL> if fullname in sys . modules : <EOL> return sys . modules [ fullname ] <EOL> fd , fn , info = self . _cache [ fullname ] <EOL> try : <EOL> return imp . load_module ( fullname , fd , fn , info ) <EOL> finally : <EOL> if fd : <EOL> fd . close ( ) <EOL> class TestFinder ( object ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . importer = zipimporter ( * args , ** kwargs ) <EOL> def find_module ( self , path ) : <EOL> importer = self . importer . find_module ( path ) <EOL> if importer is None : <EOL> return <EOL> return TestLoader ( importer ) <EOL> class TestLoader ( object ) : <EOL> def __init__ ( self , importer ) : <EOL> self . importer = importer <EOL> def load_module ( self , name ) : <EOL> mod = self . importer . load_module ( name ) <EOL> mod . __loader__ = self <EOL> return mod <EOL> class CustomLoader ( EggLoader ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( CustomLoader , self ) . setUp ( ) <EOL> sys . path_hooks . insert ( <NUM_LIT:0> , TestFinder ) <EOL> sys . path_importer_cache . clear ( ) <EOL> def tearDown ( self ) : <EOL> super ( CustomLoader , self ) . tearDown ( ) <EOL> sys . path_hooks . pop ( <NUM_LIT:0> ) </s>
