Ticket #13348: egg_loading.diff
File egg_loading.diff, 6.4 KB (added by , 15 years ago) |
---|
-
django/db/models/loading.py
4 4 from django.core.exceptions import ImproperlyConfigured 5 5 from django.utils.datastructures import SortedDict 6 6 from django.utils.importlib import import_module 7 from django.utils.module_loading import module_has_submodule 7 8 8 9 import imp 9 10 import sys … … 72 73 """ 73 74 self.handled[app_name] = None 74 75 self.nesting_level += 1 76 models = None 75 77 app_module = import_module(app_name) 76 try: 77 imp.find_module('models', app_module.__path__)78 except ImportError:79 self.nesting_level -= 180 # App has no models module, that's not a problem.81 return None82 try:83 models = import_module('.models', app_name)84 except ImportError:85 self.nesting_level -= 186 if can_postpone:87 # Either the app has an error, or the package is still being88 # imported by Python and the model module isn't available yet.89 # We will check again once all the recursion has finished (in90 # populate).91 self.postponed.append(app_name) 92 return None93 else:94 raise 78 79 if module_has_submodule(app_module, 'models'): 80 try: 81 models = import_module('.models', app_name) 82 except ImportError: 83 self.nesting_level -= 1 84 if can_postpone: 85 # Either the app has an error, or the package is still being 86 # imported by Python and the model module isn't available yet. 87 # We will check again once all the recursion has finished (in 88 # populate). 89 self.postponed.append(app_name) 90 return None 91 else: 92 raise 93 94 if models not in self.app_store: 95 self.app_store[models] = len(self.app_store) 96 95 97 self.nesting_level -= 1 96 if models not in self.app_store:97 self.app_store[models] = len(self.app_store)98 98 return models 99 99 100 100 def app_cache_ready(self): -
django/utils/module_loading.py
1 import os 2 import imp 3 4 def module_has_submodule(mod, submod_name): 5 # If the module was loaded from an egg, __loader__ will be set and 6 # 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__, submod_name) 10 mod_path = mod_path[len(loader.prefix):] 11 x = loader.find_module(mod_path) 12 if x is None: 13 # zipimport.zipimporter.find_module is documented to take 14 # dotted paths but in fact through Pyton 2.7 is observed 15 # to require os.sep in place of dots...so try using os.sep 16 # if the dotted path version failed to find the requested 17 # submodule. 18 x = loader.find_module(mod_path.replace('.', os.sep)) 19 return x is not None 20 21 try: 22 imp.find_module(submod_name, mod.__path__) 23 return True 24 except ImportError: 25 return False 26 27 -
tests/regressiontests/app_loading/tests.py
Property changes on: django/utils/module_loading.py ___________________________________________________________________ Added: svn:eol-style + native Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream Property changes on: tests/regressiontests/app_loading/eggs/nomodelapp.egg ___________________________________________________________________ Added: svn:executable + * Added: svn:mime-type + application/octet-stream Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream Property changes on: tests/regressiontests/app_loading/eggs/modelapp.egg ___________________________________________________________________ Added: svn:executable + * Added: svn:mime-type + application/octet-stream Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream Property changes on: tests/regressiontests/app_loading/eggs/omelet.egg ___________________________________________________________________ Added: svn:executable + * Added: svn:mime-type + application/octet-stream
1 1 import os 2 2 import sys 3 3 import time 4 from unittest import TestCase 4 5 5 6 from django.conf import Settings 7 from django.db.models.loading import load_app 6 8 7 9 __test__ = {"API_TESTS": """ 8 10 Test the globbing of INSTALLED_APPS. … … 25 27 26 28 """} 27 29 30 class EggLoadingTest(TestCase): 31 32 def setUp(self): 33 self.old_path = sys.path 34 self.egg_dir = '%s/eggs' % os.path.dirname(__file__) 35 36 def tearDown(self): 37 sys.path = self.old_path 38 39 def test_egg1(self): 40 egg_name = '%s/modelapp.egg' % self.egg_dir 41 sys.path.append(egg_name) 42 models = load_app('app_with_models') 43 self.failIf(models is None) 44 45 def test_egg2(self): 46 egg_name = '%s/nomodelapp.egg' % self.egg_dir 47 sys.path.append(egg_name) 48 models = load_app('app_no_models') 49 self.failUnless(models is None) 50 51 def test_egg3(self): 52 egg_name = '%s/omelet.egg' % self.egg_dir 53 sys.path.append(egg_name) 54 models = load_app('omelet.app_with_models') 55 self.failIf(models is None) 56 57 def test_egg4(self): 58 egg_name = '%s/omelet.egg' % self.egg_dir 59 sys.path.append(egg_name) 60 models = load_app('omelet.app_no_models') 61 self.failUnless(models is None)