Ticket #9427: admin_autodiscover_impfix3.patch

File admin_autodiscover_impfix3.patch, 2.5 KB (added by Matt McDonald, 16 years ago)
  • django/contrib/admin/__init__.py

     
    99    may want.
    1010    """
    1111    import imp
     12    import sys
    1213    from django.conf import settings
    1314
    1415    for app in settings.INSTALLED_APPS:
     
    2526            app_path = __import__(app, {}, {}, [app.split('.')[-1]]).__path__
    2627        except AttributeError:
    2728            continue
    28 
    29         # Step 2: use imp.find_module to find the app's admin.py. For some
    30         # reason imp.find_module raises ImportError if the app can't be found
    31         # but doesn't actually try to import the module. So skip this app if
    32         # its admin.py doesn't exist
     29           
     30        # Import the app's admin module.
    3331        try:
    34             imp.find_module('admin', app_path)
     32            __import__("%s.admin" % app)
    3533        except ImportError:
    36             continue
    37 
    38         # Step 3: import the app's admin file. If this has errors we want them
    39         # to bubble up.
    40         __import__("%s.admin" % app)
     34            # The admin module may be raising an ImportError itself, so if
     35            # this is the case, we want that error to bubble up.  However, if
     36            # this app simply does not provide an admin module, we want to
     37            # silently continue to the next app.
     38            #
     39            # Checking to see if this app has an admin module requires one of
     40            # two different approaches.  If this app was imported through the
     41            # use of a custom importer, we need to use that importer's
     42            # find_module method, otherwise, we can just use imp.find_module.
     43            # (imp does not have support for custom import hooks. For details,
     44            # see the section titled "Integration with the 'imp' module" at
     45            # http://www.python.org/dev/peps/pep-0302/)
     46            #
     47            # imp.find_module will raise an ImportError if it fails to find
     48            # the specified module, but a custom importer will return None (as
     49            # defined in PEP 302).
     50            importer = sys.path_importer_cache.get(app_path[0], None) or imp
     51            try:
     52                if importer.find_module('admin', app_path) is not None:
     53                    # The admin module exists, so let the ImportError bubble up.
     54                    raise
     55            except ImportError:
     56                # The module failed to import and doesn't exist, so move on.
     57                continue
Back to Top