Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Ticket #309: localwrap.diff

  • test/helper.py

    old new  
    101101        if body is not None: 
    102102            body = StringIO.StringIO(body) 
    103103         
     104        cherrypy.request.purge__() 
     105        cherrypy.response.purge__() 
     106        cherrypy.threadData.purge__() 
     107         
    104108        cherrypy.server.request((self.HOST, self.PORT), self.HOST, 
    105109                                requestLine, headers, body, "http") 
    106110         
  • test/test_core.py

    old new  
    253253 
    254254    def upload(self, file): 
    255255        return "Size: %s" % len(file.file.read()) 
     256 
     257class ThreadData(Test): 
     258     
     259    def index(self): 
     260        existing = repr(getattr(cherrypy.threadData, "asdf", None)) 
     261        cherrypy.threadData.asdf = "hello" 
     262        return existing 
     263 
    256264 
    257265logFile = os.path.join(localDir, "error.log") 
    258266logAccessFile = os.path.join(localDir, "access.log") 
     
    309317            ('/foo/bar', 'baz', 'that2'), 
    310318            ('/foo/nex', 'baz', 'that2'), 
    311319        ] 
     320        cherrypy.request.purge__() 
    312321        for path, key, expected in tests: 
    313322            cherrypy.request.path = path 
    314323            result = cherrypy.config.get(key, None) 
     
    668677            self.getPage('/maxrequestsize/upload', h, "POST", b) 
    669678            self.assertStatus("413 Request Entity Too Large") 
    670679            self.assertInBody("Request Entity Too Large") 
     680     
     681    def testEmptyThreadlocals(self): 
     682        results = [] 
     683        for x in xrange(20): 
     684            self.getPage("/threaddata/") 
     685            results.append(self.body) 
     686        self.assertEqual(results, ["None"] * 20) 
     687 
    671688 
    672689if __name__ == '__main__': 
    673690    helper.testmain() 
  • _cphttpserver.py

    old new  
    8181        if not self.parse_request(): # An error code has been sent, just exit 
    8282            return 
    8383         
     84        cherrypy.request.purge__() 
     85        cherrypy.response.purge__() 
     86        cherrypy.threadData.purge__() 
     87         
    8488        cherrypy.request.multithread = cherrypy.config.get("server.threadPool") > 1 
    8589        cherrypy.request.multiprocess = False 
    8690        cherrypy.server.request(self.client_address, 
  • _cpwsgi.py

    old new  
    8888        sys.stderr = NullWriter() 
    8989     
    9090    try: 
     91        cherrypy.request.purge__() 
     92        cherrypy.response.purge__() 
     93        cherrypy.threadData.purge__() 
     94         
    9195        # LOGON_USER is served by IIS, and is the name of the 
    9296        # user after having been mapped to a local account. 
    9397        # Both IIS and Apache set REMOTE_USER, when possible. 
  • __init__.py

    old new  
    5353    from threading import local 
    5454except ImportError: 
    5555    from cherrypy._cpthreadinglocal import local 
     56 
     57# Create a threadlocal object to hold the request, response, 
     58# and threadData objects. In this way, we can easily dump 
     59# those objects when we stop/start a new HTTP conversation. 
     60serving = local() 
     61 
     62class _AttributeDump: 
     63    pass 
    5664 
     65class _ThreadLocalWrapper: 
     66     
     67    def __init__(self, attrname): 
     68        self.__dict__["__attrname__"] = attrname 
     69     
     70    def purge__(self): 
     71        """Make a new, emtpy proxied object in cherrypy.serving.""" 
     72        setattr(serving, self.__attrname__, _AttributeDump()) 
     73     
     74    def __getattr__(self, name): 
     75        childobject = getattr(serving, self.__attrname__) 
     76        return getattr(childobject, name) 
     77     
     78    def __setattr__(self, name, value): 
     79        childobject = getattr(serving, self.__attrname__) 
     80        setattr(childobject, name, value) 
     81     
     82    def __delattr__(self, name): 
     83        childobject = getattr(serving, self.__attrname__) 
     84        delattr(childobject, name) 
     85 
     86 
    5787# Create request and response object (the same objects will be used 
    58 #   throughout the entire life of the webserver) 
    59 request = local() 
    60 response = local() 
     88#   throughout the entire life of the webserver, but will redirect 
     89#   to the "serving" object) 
     90request = _ThreadLocalWrapper('request') 
     91response = _ThreadLocalWrapper('response') 
    6192 
    6293# Create threadData object as a thread-specific all-purpose storage 
    63 threadData = local(
     94threadData = _ThreadLocalWrapper('threadData'
    6495 
    6596# Create variables needed for session (see lib/sessionfilter.py for more info) 
    6697from lib.filter import sessionfilter 

Hosted by WebFaction

Log in as guest/cpguest to create tickets