Skip to content

Commit

Permalink
Fixed #13684 -- if settings.ROOT_URLCONF isn't defined don't blow up …
Browse files Browse the repository at this point in the history
…with an UnboundLocalError.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14488 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
alex committed Nov 7, 2010
1 parent 035cb99 commit 96cc7ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
13 changes: 7 additions & 6 deletions django/core/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ def get_response(self, request):
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)
Expand Down Expand Up @@ -239,4 +241,3 @@ def get_script_name(environ):
if script_url:
return force_unicode(script_url[:-len(environ.get('PATH_INFO', ''))])
return force_unicode(environ.get('SCRIPT_NAME', u''))

19 changes: 18 additions & 1 deletion tests/regressiontests/middleware_exceptions/tests.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -694,3 +695,19 @@ def test_process_exception_bad_middleware_permission_denied(self):
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

0 comments on commit 96cc7ba

Please sign in to comment.