=== modified file 'django/core/handlers/base.py'
--- django/core/handlers/base.py	2009-11-16 01:58:00 +0000
+++ django/core/handlers/base.py	2010-01-10 17:19:42 +0000
@@ -68,24 +68,24 @@
         from django.core import exceptions, urlresolvers
         from django.conf import settings
 
-        # Reset the urlconf for this thread.
-        urlresolvers.set_urlconf(None)
-
-        # Apply request middleware
-        for middleware_method in self._request_middleware:
-            response = middleware_method(request)
-            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)
-
-        resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
         try:
             try:
+                # Reset the urlconf for this thread.
+                urlresolvers.set_urlconf(None)
+
+                # 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)
+                resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
+
+                # Apply request middleware
+                for middleware_method in self._request_middleware:
+                    response = middleware_method(request)
+                    if response:
+                        return response
+
                 callback, callback_args, callback_kwargs = resolver.resolve(
                         request.path_info)
 

=== added directory 'tests/regressiontests/middleware_exceptions'
=== added file 'tests/regressiontests/middleware_exceptions/__init__.py'
=== added file 'tests/regressiontests/middleware_exceptions/models.py'
--- tests/regressiontests/middleware_exceptions/models.py	1970-01-01 00:00:00 +0000
+++ tests/regressiontests/middleware_exceptions/models.py	2010-01-10 16:30:30 +0000
@@ -0,0 +1,1 @@
+from django.db import models

=== added file 'tests/regressiontests/middleware_exceptions/tests.py'
--- tests/regressiontests/middleware_exceptions/tests.py	1970-01-01 00:00:00 +0000
+++ tests/regressiontests/middleware_exceptions/tests.py	2010-01-10 17:22:39 +0000
@@ -0,0 +1,37 @@
+import sys
+
+from django.test import TestCase
+from django.core.signals import got_request_exception
+
+class RequestMiddleware(object):
+    def process_request(self, request):
+        raise Exception('Exception')
+
+class MiddlewareExceptionTest(TestCase):
+    def __init__(self, *args, **kwargs):
+        super(MiddlewareExceptionTest, self).__init__(*args, **kwargs)
+        self.exceptions = []
+        got_request_exception.connect(self._on_request_exception)
+
+    def setUp(self):
+        self.client.handler.load_middleware()
+
+    def tearDown(self):
+        self.exceptions = []
+
+    def _on_request_exception(self, sender, request, **kwargs):
+        self.exceptions.append(sys.exc_info())
+
+    def test_process_request(self):
+        self.client.handler._request_middleware.insert(0, RequestMiddleware().process_request)
+        try:
+            response = self.client.get('/')
+        except:
+            # 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
+        self.assertEquals(len(self.exceptions), 1)
+        exception, value, tb = self.exceptions[0]
+        self.assertEquals(value.args, ('Exception', ))

=== added file 'tests/regressiontests/middleware_exceptions/urls.py'
--- tests/regressiontests/middleware_exceptions/urls.py	1970-01-01 00:00:00 +0000
+++ tests/regressiontests/middleware_exceptions/urls.py	2010-01-10 16:46:32 +0000
@@ -0,0 +1,8 @@
+# coding: utf-8
+from django.conf.urls.defaults import *
+
+import views
+
+urlpatterns = patterns('',
+    (r'^$', views.index),
+)

=== added file 'tests/regressiontests/middleware_exceptions/views.py'
--- tests/regressiontests/middleware_exceptions/views.py	1970-01-01 00:00:00 +0000
+++ tests/regressiontests/middleware_exceptions/views.py	2010-01-10 16:47:23 +0000
@@ -0,0 +1,4 @@
+from django import http
+
+def index(request):
+    return http.HttpResponse('')

=== modified file 'tests/urls.py'
--- tests/urls.py	2009-03-22 07:58:29 +0000
+++ tests/urls.py	2010-01-10 16:23:37 +0000
@@ -35,4 +35,7 @@
 
     # conditional get views
     (r'condition/', include('regressiontests.conditional_processing.urls')),
+
+    # middleware exceptions tests
+    (r'middleware_exceptions/', include('regressiontests.middleware_exceptions.urls')),
 )

