Ticket #13480: django-1.0.2-nice-ImportError.patch

File django-1.0.2-nice-ImportError.patch, 2.9 KB (added by FunnyMan3595, 5 years ago)

Updated version of patch, catches SyntaxError as well.

  • core/handlers/base.py

    diff -ur django-1.0.2/core/handlers/base.py django-1.0.2-nice-ImportErrror/core/handlers/base.py
    old new  
    6262        from django.core import exceptions, urlresolvers
    6363        from django.conf import settings
    6464
     65
    6566        # Apply request middleware
    66         for middleware_method in self._request_middleware:
    67             response = middleware_method(request)
    68             if response:
    69                 return response
     67        try:
     68            for middleware_method in self._request_middleware:
     69                response = middleware_method(request)
     70                if response:
     71                    return response
     72
     73        # Catch a ViewDoesNotExist error wrapping an ImportError,
     74        # or a naked SyntaxError.
     75        except (exceptions.ViewDoesNotExist, SyntaxError), e:
     76            if isinstance(e, exceptions.ViewDoesNotExist):
     77                if len(e.args) < 3 or not isinstance(e.args[1], ImportError):
     78                    raise
     79                else:
     80                    # Wrapped ImportError, use the packaged exc_info.
     81                    exc_info = e.args[2]
     82            else: # Naked SyntaxErrror, get the exc_info directly.
     83                exc_info = sys.exc_info()
     84
     85            # Get urlconf from request object, if available.  Otherwise use default.
     86            urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
     87
     88            resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
     89            receivers = signals.got_request_exception.send(sender=self.__class__, request=request)
     90            return self.handle_uncaught_exception(request, resolver, exc_info)
    7091
    7192        # Get urlconf from request object, if available.  Otherwise use default.
    7293        urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
  • core/urlresolvers.py

    diff -ur django-1.0.2/core/urlresolvers.py django-1.0.2-nice-ImportErrror/core/urlresolvers.py
    old new  
    77    (view_function, function_args, function_kwargs)
    88"""
    99
    10 import re
     10import re, sys
    1111
    1212from django.http import Http404
    1313from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
     
    130130            self._callback = get_callable(self._callback_str)
    131131        except ImportError, e:
    132132            mod_name, _ = get_mod_func(self._callback_str)
    133             raise ViewDoesNotExist, "Could not import %s. Error was: %s" % (mod_name, str(e))
     133            raise ViewDoesNotExist("Could not import %s. Error was: %s" % (mod_name, str(e)), e, sys.exc_info())
    134134        except AttributeError, e:
    135135            mod_name, func_name = get_mod_func(self._callback_str)
    136136            raise ViewDoesNotExist, "Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e))
Back to Top