Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #11957 -- exceptions in admin.py are no longer hidden after sec…

…ond 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
  • Loading branch information...
commit f45e39ba8e19ade6b05be71b4e1ac09ea80c0af5 1 parent 38056e8
Brian Rosner authored April 12, 2010
26  django/contrib/admin/__init__.py
@@ -4,9 +4,6 @@
4 4
 from django.contrib.admin.sites import AdminSite, site
5 5
 from django.utils.importlib import import_module
6 6
 
7  
-# A flag to tell us if autodiscover is running.  autodiscover will set this to
8  
-# True while running, and False when it finishes.
9  
-LOADING = False
10 7
 
11 8
 def autodiscover():
12 9
     """
@@ -14,16 +11,8 @@ def autodiscover():
14 11
     not present. This forces an import on them to register any admin bits they
15 12
     may want.
16 13
     """
17  
-    # Bail out if autodiscover didn't finish loading from a previous call so
18  
-    # that we avoid running autodiscover again when the URLconf is loaded by
19  
-    # the exception handler to resolve the handler500 view.  This prevents an
20  
-    # admin.py module with errors from re-registering models and raising a
21  
-    # spurious AlreadyRegistered exception (see #8245).
22  
-    global LOADING
23  
-    if LOADING:
24  
-        return
25  
-    LOADING = True
26 14
 
  15
+    import copy
27 16
     import imp
28 17
     from django.conf import settings
29 18
 
@@ -53,6 +42,13 @@ def autodiscover():
53 42
 
54 43
         # Step 3: import the app's admin file. If this has errors we want them
55 44
         # to bubble up.
56  
-        import_module("%s.admin" % app)
57  
-    # autodiscover was successful, reset loading flag.
58  
-    LOADING = False
  45
+        try:
  46
+            before_import_registry = copy.copy(site._registry)
  47
+            import_module('%s.admin' % app)
  48
+        except:
  49
+            # Reset the model registry to the state before the last import as
  50
+            # this import will have to reoccur on the next request and this
  51
+            # could raise NotRegistered and AlreadyRegistered exceptions
  52
+            # (see #8245).
  53
+            site._registry = before_import_registry
  54
+            raise
13  tests/regressiontests/bug8245/tests.py
@@ -18,6 +18,13 @@ def test_bug_8245(self):
18 18
         else:
19 19
             self.fail(
20 20
                 'autodiscover should have raised a "Bad admin module" error.')
21  
-        # Calling autodiscover again should bail out early and not raise an
22  
-        # AlreadyRegistered error.
23  
-        admin.autodiscover()
  21
+
  22
+        # Calling autodiscover again should raise the very same error it did
  23
+        # the first time, not an AlreadyRegistered error.
  24
+        try:
  25
+            admin.autodiscover()
  26
+        except Exception, e:
  27
+            self.failUnlessEqual(str(e), "Bad admin module")
  28
+        else:
  29
+            self.fail( 
  30
+                'autodiscover should have raised a "Bad admin module" error.')

0 notes on commit f45e39b

Please sign in to comment.
Something went wrong with that request. Please try again.