Robert Brewer avatar Robert Brewer committed adf1238

Final 3.0.x fix for #718 (High count of uncollectable objects).

Comments (0)

Files changed (2)

cherrypy/test/test_wsgi_ns.py

 
 def setup_server():
     
+    class WSGIResponse(object):
+        
+        def __init__(self, appresults):
+            self.appresults = appresults
+            self.iter = iter(appresults)
+        
+        def __iter__(self):
+            return self
+        
+        def next(self):
+            return self.iter.next()
+        
+        def close(self):
+            if hasattr(self.appresults, "close"):
+                self.appresults.close()
+    
+    
     class ChangeCase(object):
         
         def __init__(self, app, to=None):
             self.to = to
         
         def __call__(self, environ, start_response):
-            res = ''.join(self.app(environ, start_response))
-            return [getattr(res, self.to)()]
+            res = self.app(environ, start_response)
+            class CaseResults(WSGIResponse):
+                def next(this):
+                    return getattr(this.iter.next(), self.to)()
+            return CaseResults(res)
     
-    def replace(app, map={}):
-        def replace_app(environ, start_response):
-            for line in app(environ, start_response):
-                for k, v in map.iteritems():
-                    line = line.replace(k, v)
-                yield line
-        return replace_app
+    class Replacer(object):
+        
+        def __init__(self, app, map={}):
+            self.app = app
+            self.map = map
+        
+        def __call__(self, environ, start_response):
+            res = self.app(environ, start_response)
+            class ReplaceResults(WSGIResponse):
+                def next(this):
+                    line = this.iter.next()
+                    for k, v in self.map.iteritems():
+                        line = line.replace(k, v)
+                    return line
+            return ReplaceResults(res)
     
     class Root(object):
         
         index.exposed = True
     
     
-    root_conf = {'wsgi.pipeline': [('replace', replace)],
+    root_conf = {'wsgi.pipeline': [('replace', Replacer)],
                  'wsgi.replace.map': {'L': 'X', 'l': 'r'},
                  }
     

cherrypy/test/test_wsgiapps.py

         for k in keys:
             output.append('%s: %s\n' % (k,environ[k]))
         return output
-
+    
     def test_empty_string_app(environ, start_response):
         status = '200 OK'
         response_headers = [('Content-type', 'text/plain')]
         start_response(status, response_headers)
         return ['Hello', '', ' ', '', 'world']
     
-    def reversing_middleware(app):
-        def _app(environ, start_response):
+    
+    class WSGIResponse(object):
+        
+        def __init__(self, appresults):
+            self.appresults = appresults
+            self.iter = iter(appresults)
+        
+        def __iter__(self):
+            return self
+        
+        def next(self):
+            return self.iter.next()
+        
+        def close(self):
+            if hasattr(self.appresults, "close"):
+                self.appresults.close()
+    
+    
+    class ReversingMiddleware(object):
+        
+        def __init__(self, app):
+            self.app = app
+        
+        def __call__(self, environ, start_response):
             results = app(environ, start_response)
-            if not isinstance(results, basestring):
-                results = "".join(results)
-            results = list(results)
-            results.reverse()
-            return ["".join(results)]
-        return _app
+            class Reverser(WSGIResponse):
+                def next(this):
+                    line = list(this.iter.next())
+                    line.reverse()
+                    return "".join(line)
+            return Reverser(results)
     
     class Root:
         def index(self):
     # Set script_name explicitly to None to signal CP that it should
     # be pulled from the WSGI environ each time.
     app = cherrypy.Application(Root(), script_name=None)
-    cherrypy.tree.graft(reversing_middleware(app), '/hosted/app2')
+    cherrypy.tree.graft(ReversingMiddleware(app), '/hosted/app2')
 
 from cherrypy.test import helper
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.