Ticket #13348: egg_loading.diff

File egg_loading.diff, 6.4 KB (added by Karen Tracey, 14 years ago)
  • django/db/models/loading.py

     
    44from django.core.exceptions import ImproperlyConfigured
    55from django.utils.datastructures import SortedDict
    66from django.utils.importlib import import_module
     7from django.utils.module_loading import module_has_submodule
    78
    89import imp
    910import sys
     
    7273        """
    7374        self.handled[app_name] = None
    7475        self.nesting_level += 1
     76        models = None
    7577        app_module = import_module(app_name)
    76         try:
    77             imp.find_module('models', app_module.__path__)
    78         except ImportError:
    79             self.nesting_level -= 1
    80             # App has no models module, that's not a problem.
    81             return None
    82         try:
    83             models = import_module('.models', app_name)
    84         except ImportError:
    85             self.nesting_level -= 1
    86             if can_postpone:
    87                 # Either the app has an error, or the package is still being
    88                 # imported by Python and the model module isn't available yet.
    89                 # We will check again once all the recursion has finished (in
    90                 # populate).
    91                 self.postponed.append(app_name)
    92                 return None
    93             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
    9597        self.nesting_level -= 1
    96         if models not in self.app_store:
    97             self.app_store[models] = len(self.app_store)
    9898        return models
    9999
    100100    def app_cache_ready(self):
  • django/utils/module_loading.py

     
     1import os
     2import imp
     3
     4def 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
    
     
    11import os
    22import sys
    33import time
     4from unittest import TestCase
    45
    56from django.conf import Settings
     7from django.db.models.loading import load_app
    68
    79__test__ = {"API_TESTS": """
    810Test the globbing of INSTALLED_APPS.
     
    2527
    2628"""}
    2729
     30class 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)
Back to Top