Ticket #13464: django-importer.2.diff
File django-importer.2.diff, 5.4 KB (added by , 15 years ago) |
---|
-
django/utils/module_loading.py
diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py index 9bcdd27..f251035 100644
a b 1 import os2 1 import imp 2 import os 3 import sys 3 4 4 def module_has_submodule(mod, submod_name):5 # If the module was loaded from an egg, __loader__ will be set and6 # its find_module must be used to search for submodules.7 loader = getattr(mod, '__loader__', None)8 if loader:9 mod_path = "%s.%s" % (mod.__name__.rsplit('.',1)[-1], submod_name)10 x = loader.find_module(mod_path)11 if x is None:12 # zipimport.zipimporter.find_module is documented to take13 # dotted paths but in fact through Python 2.7 is observed14 # to require os.sep in place of dots...so try using os.sep15 # if the dotted path version failed to find the requested16 # submodule.17 x = loader.find_module(mod_path.replace('.', os.sep))18 return x is not None19 5 20 try: 21 imp.find_module(submod_name, mod.__path__) 6 def module_has_submodule(package, module_name): 7 """See if 'module' is in 'package'.""" 8 name = ".".join([package.__name__, module_name]) 9 if name in sys.modules: 22 10 return True 23 except ImportError: 11 for finder in sys.meta_path: 12 if finder.find_module(name): 13 return True 14 for entry in package.__path__: # No __path__, then not a package. 15 try: 16 # Try the cached finder. 17 finder = sys.path_importer_cache[entry] 18 if finder is None: 19 # Implicit import machinery should be used. 20 try: 21 file_, _, _ = imp.find_module(module_name, [entry]) 22 if file_: 23 file_.close() 24 return True 25 except ImportError: 26 continue 27 # Else see if the finder knows of a loader. 28 elif finder.find_module(name): 29 return True 30 else: 31 continue 32 except KeyError: 33 # No cached finder, so try and make one. 34 for hook in sys.path_hooks: 35 try: 36 finder = hook(entry) 37 # XXX Could cache in sys.path_importer_cache 38 if finder.find_module(name): 39 return True 40 else: 41 # Once a finder is found, stop the search. 42 break 43 except ImportError: 44 # Continue the search for a finder. 45 continue 46 else: 47 # No finder found. 48 # Try the implicit import machinery if searching a directory. 49 if os.path.isdir(entry): 50 try: 51 file_, _, _ = imp.find_module(module_name, [entry]) 52 if file_: 53 file_.close() 54 return True 55 except ImportError: 56 pass 57 # XXX Could insert None or NullImporter 58 else: 59 # Exhausted the search, so the module cannot be found. 24 60 return False 25 26 -
tests/regressiontests/templates/tests.py
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 5902e8d..2c8b151 100644
a b 1 1 # -*- coding: utf-8 -*- 2 from django.conf import settings3 4 if __name__ == '__main__':5 # When running this file in isolation, we need to set up the configuration6 # before importing 'template'.7 settings.configure()8 2 9 from datetime import datetime, timedelta10 import time11 3 import os 12 4 import sys 5 import time 13 6 import traceback 14 7 import unittest 8 from datetime import datetime, timedelta 9 from zipimport import zipimporter 15 10 16 11 from django import template 12 from django.conf import settings 17 13 from django.core import urlresolvers 18 14 from django.template import loader 19 15 from django.template.loaders import app_directories, filesystem, cached … … class Templates(unittest.TestCase): 1317 1313 } 1318 1314 1319 1315 1320 class TemplateTagLoading(unittest.TestCase): 1316 class TestFinder(object): 1317 def __init__(self, *args, **kwargs): 1318 self.importer = zipimporter(*args, **kwargs) 1319 1320 def find_module(self, path): 1321 importer = self.importer.find_module(path) 1322 if importer is None: 1323 return 1324 return TestLoader(importer) 1325 1326 class TestLoader(object): 1327 def __init__(self, importer): 1328 self.importer = importer 1329 1330 def load_module(self, name): 1331 mod = self.importer.load_module(name) 1332 mod.__loader__ = self 1333 return mod 1321 1334 1335 1336 class TemplateTagLoading(unittest.TestCase): 1322 1337 def setUp(self): 1323 1338 self.old_path = sys.path 1324 1339 self.old_apps = settings.INSTALLED_APPS … … class TemplateTagLoading(unittest.TestCase): 1351 1366 except template.TemplateSyntaxError, e: 1352 1367 self.assertTrue('ImportError' in e.args[0]) 1353 1368 self.assertTrue('Xtemplate' in e.args[0]) 1369 1370 def test_custom_loader_error(self): 1371 sys.path_hooks.insert(0, TestFinder) 1372 sys.path_importer_cache.clear() 1373 1374 self.test_load_error_egg() 1375 1376 sys.path_hooks.pop(0) 1354 1377 1355 1378 def test_load_working_egg(self): 1356 1379 ttext = "{% load working_egg %}"