diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py index d735cd7cc5..dac0adfb93 100644 --- a/django/core/handlers/base.py +++ b/django/core/handlers/base.py @@ -70,10 +70,10 @@ class BaseHandler(object): try: try: - # Reset the urlconf for this thread. - urlresolvers.set_urlconf(None) - # Obtain a default resolver. It's needed early for handling 404's. - resolver = urlresolvers.RegexURLResolver(r'^/', None) + # Setup default url resolver for this thread. + urlconf = settings.ROOT_URLCONF + urlresolvers.set_urlconf(urlconf) + resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) # Apply request middleware for middleware_method in self._request_middleware: @@ -81,12 +81,11 @@ class BaseHandler(object): if response: return response - # Get urlconf from request object, if available. Otherwise use default. - urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF) - # Set the urlconf for this thread to the one specified above. - urlresolvers.set_urlconf(urlconf) - # Reset the resolver with a possibly new urlconf - resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) + if hasattr(request, "urlconf"): + # Reset url resolver with a custom urlconf. + urlconf = request.urlconf + urlresolvers.set_urlconf(urlconf) + resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) callback, callback_args, callback_kwargs = resolver.resolve( request.path_info) diff --git a/tests/regressiontests/middleware_exceptions/tests.py b/tests/regressiontests/middleware_exceptions/tests.py index 00c7d8b59f..3d9c5f6e83 100644 --- a/tests/regressiontests/middleware_exceptions/tests.py +++ b/tests/regressiontests/middleware_exceptions/tests.py @@ -3,9 +3,12 @@ import sys from django.test import TestCase from django.core.signals import got_request_exception -class RequestMiddleware(object): +class TestException(Exception): + pass + +class TestMiddleware(object): def process_request(self, request): - raise Exception('Exception') + raise TestException('Test Exception') class MiddlewareExceptionTest(TestCase): def setUp(self): @@ -21,15 +24,17 @@ class MiddlewareExceptionTest(TestCase): self.exceptions.append(sys.exc_info()) def test_process_request(self): - self.client.handler._request_middleware.insert(0, RequestMiddleware().process_request) + self.client.handler._request_middleware.insert(0, TestMiddleware().process_request) try: response = self.client.get('/') - except: + except TestException, e: # Test client indefinitely re-raises any exceptions being raised # during request handling. Hence actual testing that exception was # properly handled is done by relying on got_request_exception # signal being sent. pass + except Exception, e: + self.fail("Unexpected exception: %s" % e) self.assertEquals(len(self.exceptions), 1) exception, value, tb = self.exceptions[0] - self.assertEquals(value.args, ('Exception', )) + self.assertEquals(value.args, ('Test Exception', ))