Code

Ticket #3240: base.py-4290.patch

File base.py-4290.patch, 8.2 KB (added by robin@…, 8 years ago)

patch against svn rev 4290

  • core/handlers/wsgi.py

     
    11from django.core.handlers.base import BaseHandler 
    22from django.core import signals 
    3 from django.dispatch import dispatcher 
    43from django.utils import datastructures 
    54from django import http 
    65from pprint import pformat 
     
    176175 
    177176class WSGIHandler(BaseHandler): 
    178177    def __call__(self, environ, start_response): 
    179         from django.conf import settings 
     178        request, response = self.dispatch_request(WSGIRequest,environ) 
    180179 
    181         # Set up middleware if needed. We couldn't do this earlier, because 
    182         # settings weren't available. 
    183         if self._request_middleware is None: 
    184             self.load_middleware() 
    185  
    186         dispatcher.send(signal=signals.request_started) 
    187180        try: 
    188             request = WSGIRequest(environ) 
    189             response = self.get_response(request) 
    190  
    191             # Apply response middleware 
    192             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: 
    199181            status_text = STATUS_CODE_TEXT[response.status_code] 
    200182        except KeyError: 
    201183            status_text = 'UNKNOWN STATUS CODE' 
  • core/handlers/base.py

     
    33from django import http 
    44import sys 
    55 
     6def 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 
     11def _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 
    637class BaseHandler(object): 
    738    def __init__(self): 
    839        self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None 
     
    5485        from django.core.mail import mail_admins 
    5586        from django.conf import settings 
    5687 
    57         # Apply request middleware 
    58         for middleware_method in self._request_middleware: 
    59             response = middleware_method(request) 
    60             if response: 
    61                 return response 
    6288 
    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 
    6596 
    66         resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) 
    67         try: 
    6897            callback, callback_args, callback_kwargs = resolver.resolve(request.path) 
    6998 
    7099            # Apply view middleware 
     
    106135        except SystemExit: 
    107136            pass # See http://code.djangoproject.com/ticket/1023 
    108137        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) 
    127139 
    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

     
    135135    def __call__(self, req): 
    136136        # mod_python fakes the environ, and thus doesn't process SetEnv.  This fixes that 
    137137        os.environ.update(req.subprocess_env) 
     138        request, response = self.dispatch_request(ModPythonRequest,req) 
    138139 
    139         # now that the environ works we can see the correct settings, so imports 
    140         # that use settings now can work 
    141         from django.conf import settings 
    142  
    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 middleware 
    153             for middleware_method in self._response_middleware: 
    154                 response = middleware_method(request, response) 
    155  
    156         finally: 
    157             dispatcher.send(signal=signals.request_finished) 
    158  
    159140        # Convert our custom HttpResponse object back into the mod_python req. 
    160141        req.content_type = response['Content-Type'] 
    161142        for key, value in response.headers.items():