Author: Alex
Date: 2010-11-07 10:02:22 -0600 (Sun, 07 Nov 2010)
New Revision: 14488

Modified:
   django/trunk/django/core/handlers/base.py
   django/trunk/tests/regressiontests/middleware_exceptions/tests.py
Log:
Fixed #13684 -- if settings.ROOT_URLCONF isn't defined don't blow up with an 
UnboundLocalError.

Modified: django/trunk/django/core/handlers/base.py
===================================================================
--- django/trunk/django/core/handlers/base.py   2010-11-07 15:41:17 UTC (rev 
14487)
+++ django/trunk/django/core/handlers/base.py   2010-11-07 16:02:22 UTC (rev 
14488)
@@ -73,13 +73,15 @@
         from django.conf import settings
 
         try:
+            # Setup default url resolver for this thread, this code is outside
+            # the try/except so we don't get a spurious "unbound local
+            # variable" exception in the event an exception is raised before
+            # resolver is set
+            urlconf = settings.ROOT_URLCONF
+            urlresolvers.set_urlconf(urlconf)
+            resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
             try:
-                # Setup default url resolver for this thread.
-                urlconf = settings.ROOT_URLCONF
-                urlresolvers.set_urlconf(urlconf)
-                resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
                 response = None
-
                 # Apply request middleware
                 for middleware_method in self._request_middleware:
                     response = middleware_method(request)
@@ -239,4 +241,3 @@
     if script_url:
         return force_unicode(script_url[:-len(environ.get('PATH_INFO', ''))])
     return force_unicode(environ.get('SCRIPT_NAME', u''))
-

Modified: django/trunk/tests/regressiontests/middleware_exceptions/tests.py
===================================================================
--- django/trunk/tests/regressiontests/middleware_exceptions/tests.py   
2010-11-07 15:41:17 UTC (rev 14487)
+++ django/trunk/tests/regressiontests/middleware_exceptions/tests.py   
2010-11-07 16:02:22 UTC (rev 14488)
@@ -1,8 +1,9 @@
 import sys
 
-from django.test import TestCase
+from django.conf import settings
 from django.core.signals import got_request_exception
 from django.http import HttpResponse
+from django.test import TestCase
 
 
 class TestException(Exception):
@@ -694,3 +695,19 @@
         self.assert_middleware_usage(pre_middleware,  True,  True,  True, 
False)
         self.assert_middleware_usage(bad_middleware,  True,  True,  True,  
True)
         self.assert_middleware_usage(post_middleware, True,  True,  True,  
True)
+
+
+_missing = object()
+class RootUrlconfTests(TestCase):
+    def test_missing_root_urlconf(self):
+        try:
+            original_ROOT_URLCONF = settings.ROOT_URLCONF
+            del settings.ROOT_URLCONF
+        except AttributeError:
+            original_ROOT_URLCONF = _missing
+        self.assertRaises(AttributeError,
+            self.client.get, "/middleware_exceptions/view/"
+        )
+
+        if original_ROOT_URLCONF is not _missing:
+            settings.ROOT_URLCONF = original_ROOT_URLCONF

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to