Fixed #11957 -- exceptions in admin.py are no longer hidden after second request

Before you had to restart runserver for the correct exception message to show
up again. Reverts fix in r9680 which has this side-affect.

Thanks to jarrow, carljm and ramiro for their work on the patch and tickets.

Backport of r12956 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12957 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Brian Rosner 2010-04-12 14:55:45 +00:00
parent 38056e82d7
commit f45e39ba8e
2 changed files with 21 additions and 18 deletions

View File

@ -4,9 +4,6 @@ from django.contrib.admin.options import StackedInline, TabularInline
from django.contrib.admin.sites import AdminSite, site from django.contrib.admin.sites import AdminSite, site
from django.utils.importlib import import_module from django.utils.importlib import import_module
# A flag to tell us if autodiscover is running. autodiscover will set this to
# True while running, and False when it finishes.
LOADING = False
def autodiscover(): def autodiscover():
""" """
@ -14,16 +11,8 @@ def autodiscover():
not present. This forces an import on them to register any admin bits they not present. This forces an import on them to register any admin bits they
may want. may want.
""" """
# Bail out if autodiscover didn't finish loading from a previous call so
# that we avoid running autodiscover again when the URLconf is loaded by
# the exception handler to resolve the handler500 view. This prevents an
# admin.py module with errors from re-registering models and raising a
# spurious AlreadyRegistered exception (see #8245).
global LOADING
if LOADING:
return
LOADING = True
import copy
import imp import imp
from django.conf import settings from django.conf import settings
@ -53,6 +42,13 @@ def autodiscover():
# Step 3: import the app's admin file. If this has errors we want them # Step 3: import the app's admin file. If this has errors we want them
# to bubble up. # to bubble up.
import_module("%s.admin" % app) try:
# autodiscover was successful, reset loading flag. before_import_registry = copy.copy(site._registry)
LOADING = False import_module('%s.admin' % app)
except:
# Reset the model registry to the state before the last import as
# this import will have to reoccur on the next request and this
# could raise NotRegistered and AlreadyRegistered exceptions
# (see #8245).
site._registry = before_import_registry
raise

View File

@ -18,6 +18,13 @@ class Bug8245Test(TestCase):
else: else:
self.fail( self.fail(
'autodiscover should have raised a "Bad admin module" error.') 'autodiscover should have raised a "Bad admin module" error.')
# Calling autodiscover again should bail out early and not raise an
# AlreadyRegistered error. # Calling autodiscover again should raise the very same error it did
admin.autodiscover() # the first time, not an AlreadyRegistered error.
try:
admin.autodiscover()
except Exception, e:
self.failUnlessEqual(str(e), "Bad admin module")
else:
self.fail(
'autodiscover should have raised a "Bad admin module" error.')