Ticket #3240: base.py-4290.patch
File base.py-4290.patch, 8.2 KB (added by , 18 years ago) |
---|
-
core/handlers/wsgi.py
1 1 from django.core.handlers.base import BaseHandler 2 2 from django.core import signals 3 from django.dispatch import dispatcher4 3 from django.utils import datastructures 5 4 from django import http 6 5 from pprint import pformat … … 176 175 177 176 class WSGIHandler(BaseHandler): 178 177 def __call__(self, environ, start_response): 179 from django.conf import settings178 request, response = self.dispatch_request(WSGIRequest,environ) 180 179 181 # Set up middleware if needed. We couldn't do this earlier, because182 # settings weren't available.183 if self._request_middleware is None:184 self.load_middleware()185 186 dispatcher.send(signal=signals.request_started)187 180 try: 188 request = WSGIRequest(environ)189 response = self.get_response(request)190 191 # Apply response middleware192 for middleware_method in self._response_middleware:193 response = middleware_method(request, response)194 195 finally:196 dispatcher.send(signal=signals.request_finished)197 198 try:199 181 status_text = STATUS_CODE_TEXT[response.status_code] 200 182 except KeyError: 201 183 status_text = 'UNKNOWN STATUS CODE' -
core/handlers/base.py
3 3 from django import http 4 4 import sys 5 5 6 def get_traceback(exc_info=None): 7 "Helper function to return the traceback as a string" 8 import traceback 9 return '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info()))) 10 11 def _500View(request,resolver=None): 12 from django.conf import settings 13 if settings.DEBUG: 14 from django.views import debug 15 return debug.technical_500_response(request, *sys.exc_info()) 16 else: 17 from django.core import urlresolvers 18 from django.core.mail import mail_admins 19 if not resolver: 20 resolver = urlresolvers.RegexURLResolver(r'^/', settings.ROOT_URLCONF) 21 # Get the exception info now, in case another exception is thrown later. 22 exc_info = sys.exc_info() 23 receivers = dispatcher.send(signal=signals.got_request_exception) 24 # When DEBUG is False, send an error message to the admins. 25 subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path) 26 try: 27 request_repr = repr(request) 28 except: 29 request_repr = "Request repr() unavailable" 30 message = "%s\n\n%s" % (get_traceback(exc_info), request_repr) 31 mail_admins(subject, message, fail_silently=True) 32 resolver = urlresolvers.RegexURLResolver(r'^/', settings.ROOT_URLCONF) 33 # Return an HttpResponse that displays a friendly error message. 34 callback, param_dict = resolver.resolve500() 35 return callback(request, **param_dict) 36 6 37 class BaseHandler(object): 7 38 def __init__(self): 8 39 self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None … … 54 85 from django.core.mail import mail_admins 55 86 from django.conf import settings 56 87 57 # Apply request middleware58 for middleware_method in self._request_middleware:59 response = middleware_method(request)60 if response:61 return response62 88 63 # Get urlconf from request object, if available. Otherwise use default. 64 urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF) 89 resolver = urlresolvers.RegexURLResolver(r'^/', settings.ROOT_URLCONF) 90 try: 91 # Apply request middleware 92 for middleware_method in self._request_middleware: 93 response = middleware_method(request) 94 if response: 95 return response 65 96 66 resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)67 try:68 97 callback, callback_args, callback_kwargs = resolver.resolve(request.path) 69 98 70 99 # Apply view middleware … … 106 135 except SystemExit: 107 136 pass # See http://code.djangoproject.com/ticket/1023 108 137 except: # Handle everything else, including SuspiciousOperation, etc. 109 if settings.DEBUG: 110 from django.views import debug 111 return debug.technical_500_response(request, *sys.exc_info()) 112 else: 113 # Get the exception info now, in case another exception is thrown later. 114 exc_info = sys.exc_info() 115 receivers = dispatcher.send(signal=signals.got_request_exception) 116 # When DEBUG is False, send an error message to the admins. 117 subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path) 118 try: 119 request_repr = repr(request) 120 except: 121 request_repr = "Request repr() unavailable" 122 message = "%s\n\n%s" % (self._get_traceback(exc_info), request_repr) 123 mail_admins(subject, message, fail_silently=True) 124 # Return an HttpResponse that displays a friendly error message. 125 callback, param_dict = resolver.resolve500() 126 return callback(request, **param_dict) 138 return _500View(request) 127 139 128 def _get_traceback(self, exc_info=None): 129 "Helper function to return the traceback as a string" 130 import traceback 131 return '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info()))) 140 def _get_traceback(self,exc_info=None): 141 return get_traceback(exc_info) 142 143 def dispatch_request(self,handle_request,*args,**kwd): 144 from django.conf import settings 145 146 # Set up middleware if needed. We couldn't do this earlier, because 147 # settings weren't available. 148 if self._request_middleware is None: 149 self.load_middleware() 150 151 dispatcher.send(signal=signals.request_started) 152 try: 153 request = handle_request(*args,**kwd) 154 response = self.get_response(request) 155 156 try: 157 # Apply response middleware 158 for middleware_method in self._response_middleware: 159 response = middleware_method(request, response) 160 except: 161 from django.core.handlers.base import _500View 162 response = _500View(request) 163 164 finally: 165 dispatcher.send(signal=signals.request_finished) 166 return request, response -
core/handlers/modpython.py
135 135 def __call__(self, req): 136 136 # mod_python fakes the environ, and thus doesn't process SetEnv. This fixes that 137 137 os.environ.update(req.subprocess_env) 138 request, response = self.dispatch_request(ModPythonRequest,req) 138 139 139 # now that the environ works we can see the correct settings, so imports140 # that use settings now can work141 from django.conf import settings142 143 # if we need to set up middleware, now that settings works we can do it now.144 if self._request_middleware is None:145 self.load_middleware()146 147 dispatcher.send(signal=signals.request_started)148 try:149 request = ModPythonRequest(req)150 response = self.get_response(request)151 152 # Apply response middleware153 for middleware_method in self._response_middleware:154 response = middleware_method(request, response)155 156 finally:157 dispatcher.send(signal=signals.request_finished)158 159 140 # Convert our custom HttpResponse object back into the mod_python req. 160 141 req.content_type = response['Content-Type'] 161 142 for key, value in response.headers.items():