Ticket #5685: ticket_5685__revision_6809.diff

File ticket_5685__revision_6809.diff, 7.9 KB (added by Ben Slavin, 17 years ago)

A few changes... described below.

  • django/test/client.py

     
    2929        from django.conf import settings
    3030        from django.core import signals
    3131
    32         # Set up middleware if needed. We couldn't do this earlier, because
    33         # settings weren't available.
    34         if self._request_middleware is None:
    35             self.load_middleware()
     32        # Perform state setup to ensure middleware is registered, among
     33        # other things.
     34        self.state.ensure_state()
    3635
    3736        dispatcher.send(signal=signals.request_started)
    3837        try:
  • django/conf/global_settings.py

     
    131131EMAIL_HOST_PASSWORD = ''
    132132EMAIL_USE_TLS = False
    133133
     134# List of functions to be called to setup the runtime environment
     135RUNTIME_SETUP_FUNCTIONS = ()
     136
    134137# List of strings representing installed apps.
    135138INSTALLED_APPS = ()
    136139
  • django/core/handlers/wsgi.py

     
    180180    raw_post_data = property(_get_raw_post_data)
    181181
    182182class WSGIHandler(BaseHandler):
    183     initLock = Lock()
    184183    request_class = WSGIRequest
    185184
    186185    def __call__(self, environ, start_response):
    187186        from django.conf import settings
    188187
    189         # Set up middleware if needed. We couldn't do this earlier, because
    190         # settings weren't available.
    191         if self._request_middleware is None:
    192             self.initLock.acquire()
    193             # Check that middleware is still uninitialised.
    194             if self._request_middleware is None:
    195                 self.load_middleware()
    196             self.initLock.release()
     188        # Perform state setup to ensure middleware is registered, among
     189        # other things.
     190        self.state.ensure_state()
    197191
    198192        dispatcher.send(signal=signals.request_started)
    199193        try:
  • django/core/handlers/base.py

     
    11from django.core import signals
    22from django.dispatch import dispatcher
     3from django.utils.setup import setup_runtime
    34from django import http
    45import sys
     6import threading
     7import logging
    58
    6 class BaseHandler(object):
    7     # Changes that are always applied to a response (in this order).
    8     response_fixes = [http.fix_location_header,
    9             http.conditional_content_removal]
     9class HandlerState(object):
     10    """
     11    A cache that stores/ensures state required for handling requests.
     12    This includes loading the middleware and performing runtime setup.
     13    """
     14    # Use the Borg pattern to share state between all instances. Details at
     15    # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531.
     16    __shared_state = dict(
     17        request_middleware = [],
     18        view_middleware = [],
     19        response_middleware = [],
     20        exception_middleware = [],
     21        runtime_setup = False,  # Flag to monitor whether generic runtime environment setup has occured
    1022
     23        # -- Variables used to setup the state --
     24        loaded = False,         # Flag to monitor the state of the cache
     25        write_lock = threading.RLock(),
     26    )
     27   
    1128    def __init__(self):
    12         self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None
     29        self.__dict__ = self.__shared_state
     30       
     31    def ensure_state(self):
     32        """
     33        Fill in all the cache information, and perform any setup that is
     34        necessary. This method is threadsafe, in the sense that every caller
     35        will see the same state upon return, and if the cache is already
     36        initialised, it does no work.
     37        """
     38        if self.loaded:
     39            return
     40        self.write_lock.acquire()
     41        try:
     42            if self.loaded:
     43                return
    1344
     45            setup_runtime()
     46            self.runtime_setup = True
     47 
     48            self.load_middleware()
     49 
     50            self.loaded = True
     51        finally:
     52            self.write_lock.release()
     53
    1454    def load_middleware(self):
    1555        """
    1656        Populate middleware lists from settings.MIDDLEWARE_CLASSES.
     
    1959        """
    2060        from django.conf import settings
    2161        from django.core import exceptions
    22         self._request_middleware = []
    23         self._view_middleware = []
    24         self._response_middleware = []
    25         self._exception_middleware = []
     62
    2663        for middleware_path in settings.MIDDLEWARE_CLASSES:
    2764            try:
    2865                dot = middleware_path.rindex('.')
     
    4481                continue
    4582
    4683            if hasattr(mw_instance, 'process_request'):
    47                 self._request_middleware.append(mw_instance.process_request)
     84                self.request_middleware.append(mw_instance.process_request)
    4885            if hasattr(mw_instance, 'process_view'):
    49                 self._view_middleware.append(mw_instance.process_view)
     86                self.view_middleware.append(mw_instance.process_view)
    5087            if hasattr(mw_instance, 'process_response'):
    51                 self._response_middleware.insert(0, mw_instance.process_response)
     88                self.response_middleware.insert(0, mw_instance.process_response)
    5289            if hasattr(mw_instance, 'process_exception'):
    53                 self._exception_middleware.insert(0, mw_instance.process_exception)
     90                self.exception_middleware.insert(0, mw_instance.process_exception)           
    5491
     92handler_state = HandlerState()
     93         
     94class BaseHandler(object):
     95    state = handler_state
     96    _request_middleware = handler_state.request_middleware
     97    _view_middleware = handler_state.view_middleware
     98    _response_middleware = handler_state.response_middleware
     99    _exception_middleware = handler_state.exception_middleware
     100
     101    # Changes that are always applied to a response (in this order).
     102    response_fixes = [http.fix_location_header,
     103            http.conditional_content_removal]
     104
    55105    def get_response(self, request):
    56106        "Returns an HttpResponse object for the given HttpRequest"
    57107        from django.core import exceptions, urlresolvers
  • django/core/handlers/modpython.py

     
    146146        # that use settings now can work
    147147        from django.conf import settings
    148148
    149         # if we need to set up middleware, now that settings works we can do it now.
    150         if self._request_middleware is None:
    151             self.load_middleware()
     149        # Perform state setup to ensure middleware is registered, among
     150        # other things.
     151        self.state.ensure_state()
    152152
    153153        dispatcher.send(signal=signals.request_started)
    154154        try:
  • django/core/management/commands/shell.py

     
    11import os
    22from django.core.management.base import NoArgsCommand
     3from django.utils.setup import setup_runtime
    34from optparse import make_option
    45
    56class Command(NoArgsCommand):
     
    1213    requires_model_validation = False
    1314
    1415    def handle_noargs(self, **options):
     16        setup_runtime() # Perform some setup that may be necessary to get the runtime environment ready
     17
    1518        # XXX: (Temporary) workaround for ticket #1796: force early loading of all
    1619        # models from installed apps.
    1720        from django.db.models.loading import get_models
Back to Top