]> granicus.if.org Git - python/commitdiff
Issue #16220: wsgiref now always calls close() on an iterable response.
authorAntoine Pitrou <solipsis@pitrou.net>
Sun, 21 Oct 2012 12:09:05 +0000 (14:09 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Sun, 21 Oct 2012 12:09:05 +0000 (14:09 +0200)
Patch by Brent Tubbs.

Lib/test/test_wsgiref.py
Lib/wsgiref/handlers.py
Misc/ACKS
Misc/NEWS

index a08f66b7b476b36d07de2cc68a14f9095dbae882..c06f94a88822ae4e5183dad80833f42be3967ccf 100644 (file)
@@ -656,40 +656,27 @@ class HandlerTests(TestCase):
             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
index 67064a68756fdcd7df67d722a2803ecdfd7da6f0..63d5993eca00f408382945f77e4fa243cf52b716 100644 (file)
@@ -174,11 +174,13 @@ class BaseHandler:
         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):
index ba87ecbb79b6c73997a4144dc0227302fec32a59..8bdbe8f3c2954371f8886dc2aaa2fe86fd77e6ca 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1067,6 +1067,7 @@ Richard Townsend
 Laurence Tratt
 John Tromp
 Jason Trowbridge
+Brent Tubbs
 Anthony Tuininga
 Erno Tukia
 David Turner
index 107222f332c8d37de7085327f32bed06ee71ffed..b68970ae1495590de7164afe1665a2f26030f7f2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -132,6 +132,9 @@ Core and Builtins
 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'.