Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ae247a5

Browse files
committed
Issue #16220: wsgiref now always calls close() on an iterable response.
Patch by Brent Tubbs.
1 parent 2778d0d commit ae247a5

4 files changed

Lines changed: 29 additions & 36 deletions

File tree

Lib/test/test_wsgiref.py

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -656,40 +656,27 @@ def app(e, s):
656656
b"data",
657657
h.stdout.getvalue())
658658

659-
# This epilogue is needed for compatibility with the Python 2.5 regrtest module
659+
def testCloseOnError(self):
660+
side_effects = {'close_called': False}
661+
MSG = b"Some output has been sent"
662+
def error_app(e,s):
663+
s("200 OK",[])(MSG)
664+
class CrashyIterable(object):
665+
def __iter__(self):
666+
while True:
667+
yield b'blah'
668+
raise AssertionError("This should be caught by handler")
669+
def close(self):
670+
side_effects['close_called'] = True
671+
return CrashyIterable()
672+
673+
h = ErrorHandler()
674+
h.run(error_app)
675+
self.assertEqual(side_effects['close_called'], True)
676+
660677

661678
def test_main():
662679
support.run_unittest(__name__)
663680

664681
if __name__ == "__main__":
665682
test_main()
666-
667-
668-
669-
670-
671-
672-
673-
674-
675-
676-
677-
678-
679-
680-
681-
682-
683-
684-
685-
686-
687-
688-
689-
690-
691-
692-
693-
694-
695-
# the above lines intentionally left blank

Lib/wsgiref/handlers.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,13 @@ def finish_response(self):
174174
in the event loop to iterate over the data, and to call
175175
'self.close()' once the response is finished.
176176
"""
177-
if not self.result_is_file() or not self.sendfile():
178-
for data in self.result:
179-
self.write(data)
180-
self.finish_content()
181-
self.close()
177+
try:
178+
if not self.result_is_file() or not self.sendfile():
179+
for data in self.result:
180+
self.write(data)
181+
self.finish_content()
182+
finally:
183+
self.close()
182184

183185

184186
def get_scheme(self):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,7 @@ Richard Townsend
10671067
Laurence Tratt
10681068
John Tromp
10691069
Jason Trowbridge
1070+
Brent Tubbs
10701071
Anthony Tuininga
10711072
Erno Tukia
10721073
David Turner

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ Core and Builtins
132132
Library
133133
-------
134134

135+
- Issue #16220: wsgiref now always calls close() on an iterable response.
136+
Patch by Brent Tubbs.
137+
135138
- Issue #16270: urllib may hang when used for retrieving files via FTP by using
136139
a context manager. Patch by Giampaolo Rodola'.
137140

0 commit comments

Comments
 (0)