Ticket #15662: module_has_submodule.patch
File module_has_submodule.patch, 2.7 KB (added by , 14 years ago) |
---|
-
tests/regressiontests/utils/module_loading.py
1 1 import os 2 2 import sys 3 import imp 3 4 from zipimport import zipimporter 4 5 5 6 from django.utils import unittest … … 8 9 9 10 10 11 class DefaultLoader(unittest.TestCase): 12 def setUp(self): 13 sys.meta_path.insert(0, ProxyFinder()) 14 15 def tearDown(self): 16 sys.meta_path.pop(0) 17 11 18 def test_loader(self): 12 19 "Normal module existence can be tested" 13 20 test_module = import_module('regressiontests.utils.test_module') … … 25 32 self.assertFalse(module_has_submodule(test_module, 'no_such_module')) 26 33 self.assertRaises(ImportError, import_module, 'regressiontests.utils.test_module.no_such_module') 27 34 35 # A child that doesn't exist, but is the name of a package on the path 36 self.assertFalse(module_has_submodule(test_module, 'django')) 37 self.assertRaises(ImportError, import_module, 'regressiontests.utils.test_module.django') 38 28 39 # Don't be confused by caching of import misses 29 40 import types # causes attempted import of regressiontests.utils.types 30 41 self.assertFalse(module_has_submodule(sys.modules['regressiontests.utils'], 'types')) … … 84 95 self.assertFalse(module_has_submodule(egg_module, 'no_such_module')) 85 96 self.assertRaises(ImportError, import_module, 'egg_module.sub1.sub2.no_such_module') 86 97 98 class ProxyFinder(object): 99 def __init__(self): 100 self._cache = {} 101 102 def find_module(self, fullname, path=None): 103 tail = fullname.rsplit('.', 1)[-1] 104 try: 105 self._cache[fullname] = imp.find_module(tail, path) 106 except ImportError: 107 return None 108 else: 109 return self # this is a loader as well 110 111 def load_module(self, fullname): 112 if fullname in sys.modules: 113 return sys.modules[fullname] 114 fd, fn, info = self._cache[fullname] 115 return imp.load_module(fullname, fd, fn, info) 116 87 117 class TestFinder(object): 88 118 def __init__(self, *args, **kwargs): 89 119 self.importer = zipimporter(*args, **kwargs) -
django/utils/module_loading.py
12 12 except KeyError: 13 13 pass 14 14 for finder in sys.meta_path: 15 if finder.find_module(name ):15 if finder.find_module(name, package): 16 16 return True 17 17 for entry in package.__path__: # No __path__, then not a package. 18 18 try: