[Python-checkins] cpython (3.2): Issue #16220: wsgiref now always calls close() on an iterable response.

antoine.pitrou python-checkins at python.org
Sun Oct 21 14:17:13 CEST 2012


http://hg.python.org/cpython/rev/eef470032457
changeset:   79868:eef470032457
branch:      3.2
parent:      79856:566b7574becb
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sun Oct 21 14:09:05 2012 +0200
summary:
  Issue #16220: wsgiref now always calls close() on an iterable response.
Patch by Brent Tubbs.

files:
  Lib/test/test_wsgiref.py |  49 ++++++++++-----------------
  Lib/wsgiref/handlers.py  |  12 ++++--
  Misc/ACKS                |   1 +
  Misc/NEWS                |   3 +
  4 files changed, 29 insertions(+), 36 deletions(-)


diff --git a/Lib/test/test_wsgiref.py b/Lib/test/test_wsgiref.py
--- a/Lib/test/test_wsgiref.py
+++ b/Lib/test/test_wsgiref.py
@@ -656,40 +656,27 @@
             b"data",
             h.stdout.getvalue())
 
-# This epilogue is needed for compatibility with the Python 2.5 regrtest module
+    def testCloseOnError(self):
+        side_effects = {'close_called': False}
+        MSG = b"Some output has been sent"
+        def error_app(e,s):
+            s("200 OK",[])(MSG)
+            class CrashyIterable(object):
+                def __iter__(self):
+                    while True:
+                        yield b'blah'
+                        raise AssertionError("This should be caught by handler")
+                def close(self):
+                    side_effects['close_called'] = True
+            return CrashyIterable()
+
+        h = ErrorHandler()
+        h.run(error_app)
+        self.assertEqual(side_effects['close_called'], True)
+
 
 def test_main():
     support.run_unittest(__name__)
 
 if __name__ == "__main__":
     test_main()
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-# the above lines intentionally left blank
diff --git a/Lib/wsgiref/handlers.py b/Lib/wsgiref/handlers.py
--- a/Lib/wsgiref/handlers.py
+++ b/Lib/wsgiref/handlers.py
@@ -174,11 +174,13 @@
         in the event loop to iterate over the data, and to call
         'self.close()' once the response is finished.
         """
-        if not self.result_is_file() or not self.sendfile():
-            for data in self.result:
-                self.write(data)
-            self.finish_content()
-        self.close()
+        try:
+            if not self.result_is_file() or not self.sendfile():
+                for data in self.result:
+                    self.write(data)
+                self.finish_content()
+        finally:
+            self.close()
 
 
     def get_scheme(self):
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1067,6 +1067,7 @@
 Laurence Tratt
 John Tromp
 Jason Trowbridge
+Brent Tubbs
 Anthony Tuininga
 Erno Tukia
 David Turner
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -132,6 +132,9 @@
 Library
 -------
 
+- Issue #16220: wsgiref now always calls close() on an iterable response.
+  Patch by Brent Tubbs.
+
 - Issue #16270: urllib may hang when used for retrieving files via FTP by using
   a context manager.  Patch by Giampaolo Rodola'.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list