Ticket #16674: wsgi-expose-exc-info-v2.patch

File wsgi-expose-exc-info-v2.patch, 2.3 KB (added by James Henstridge, 13 years ago)

A modified version of the patch that doesn't use thread local storage

  • django/core/handlers/wsgi.py

     
    139139            content_length = 0
    140140        self._stream = LimitedStream(self.environ['wsgi.input'], content_length)
    141141        self._read_started = False
     142        self._wsgi_exc_info = None
    142143
    143144    def get_full_path(self):
    144145        # RFC 3986 requires query string arguments to be in the ASCII range.
     
    215216
    216217        set_script_prefix(base.get_script_name(environ))
    217218        signals.request_started.send(sender=self.__class__)
     219        exc_info = None
    218220        try:
    219221            try:
    220222                request = self.request_class(environ)
    221223            except UnicodeDecodeError:
     224                exc_info = sys.exc_info()
    222225                logger.warning('Bad Request (UnicodeDecodeError)',
    223                     exc_info=sys.exc_info(),
     226                    exc_info=exc_info,
    224227                    extra={
    225228                        'status_code': 400,
    226229                    }
    227230                )
    228231                response = http.HttpResponseBadRequest()
    229232            else:
    230                 response = self.get_response(request)
     233                try:
     234                    response = self.get_response(request)
     235                finally:
     236                    exc_info = request._wsgi_exc_info
     237                    request._wsgi_exc_info = None
    231238        finally:
    232239            signals.request_finished.send(sender=self.__class__)
    233240
     
    239246        response_headers = [(str(k), str(v)) for k, v in response.items()]
    240247        for c in response.cookies.values():
    241248            response_headers.append(('Set-Cookie', str(c.output(header=''))))
    242         start_response(status, response_headers)
     249        try:
     250            start_response(status, response_headers, exc_info)
     251        finally:
     252            exc_info = None
    243253        return response
    244254
     255    def handle_uncaught_exception(self, request, resolver, exc_info):
     256        # Capture the exception context so we can pass it back to
     257        # __call__()
     258        request._wsgi_exc_info = exc_info
     259        try:
     260            return super(WSGIHandler, self).handle_uncaught_exception(
     261                request, resolver, exc_info)
     262        finally:
     263            exc_info = None
Back to Top