Ticket #9923: django.patch

File django.patch, 3.8 KB (added by James Aylett, 11 years ago)

Patch to bomb out on exception during middleware loading

  • ext/django/core/handlers/base.py

     
    2828        self._view_middleware = []
    2929        self._response_middleware = []
    3030        self._exception_middleware = []
    31         for middleware_path in settings.MIDDLEWARE_CLASSES:
    32             try:
    33                 dot = middleware_path.rindex('.')
    34             except ValueError:
    35                 raise exceptions.ImproperlyConfigured, '%s isn\'t a middleware module' % middleware_path
    36             mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:]
    37             try:
    38                 mod = __import__(mw_module, {}, {}, [''])
    39             except ImportError, e:
    40                 raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e)
    41             try:
    42                 mw_class = getattr(mod, mw_classname)
    43             except AttributeError:
    44                 raise exceptions.ImproperlyConfigured, 'Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname)
     31        try:
     32            for middleware_path in settings.MIDDLEWARE_CLASSES:
     33                try:
     34                    dot = middleware_path.rindex('.')
     35                except ValueError:
     36                    raise exceptions.ImproperlyConfigured, '%s isn\'t a middleware module' % middleware_path
     37                mw_module, mw_classname = middleware_path[:dot], middleware_path[dot+1:]
     38                try:
     39                    mod = __import__(mw_module, {}, {}, [''])
     40                except ImportError, e:
     41                    raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e)
     42                try:
     43                    mw_class = getattr(mod, mw_classname)
     44                except AttributeError:
     45                    raise exceptions.ImproperlyConfigured, 'Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname)
    4546
     47                try:
     48                    mw_instance = mw_class()
     49                except exceptions.MiddlewareNotUsed:
     50                    continue
     51
     52                if hasattr(mw_instance, 'process_request'):
     53                    self._request_middleware.append(mw_instance.process_request)
     54                if hasattr(mw_instance, 'process_view'):
     55                    self._view_middleware.append(mw_instance.process_view)
     56                if hasattr(mw_instance, 'process_response'):
     57                    self._response_middleware.insert(0, mw_instance.process_response)
     58                if hasattr(mw_instance, 'process_exception'):
     59                    self._exception_middleware.insert(0, mw_instance.process_exception)
     60
     61        except:
    4662            try:
    47                 mw_instance = mw_class()
    48             except exceptions.MiddlewareNotUsed:
    49                 continue
     63                print 'middleware load failure: EXITING IMMEDIATELY'
     64                import traceback
     65                traceback.print_exc()
     66            except:
     67                pass
     68            import os
     69            os._exit(-1)
    5070
    51             if hasattr(mw_instance, 'process_request'):
    52                 self._request_middleware.append(mw_instance.process_request)
    53             if hasattr(mw_instance, 'process_view'):
    54                 self._view_middleware.append(mw_instance.process_view)
    55             if hasattr(mw_instance, 'process_response'):
    56                 self._response_middleware.insert(0, mw_instance.process_response)
    57             if hasattr(mw_instance, 'process_exception'):
    58                 self._exception_middleware.insert(0, mw_instance.process_exception)
    5971
    6072    def get_response(self, request):
    6173        "Returns an HttpResponse object for the given HttpRequest"
Back to Top