Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-29183: Fix double exceptions in wsgiref.handlers.BaseHandler #12914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions 25 Lib/test/test_wsgiref.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,31 @@ def write(self, b):

self.assertFalse(stderr.getvalue())

def testDontResetInternalStateOnException(self):
class CustomException(ValueError):
pass

# We are raising CustomException here to trigger an exception
# during the execution of SimpleHandler.finish_response(), so
# we can easily test that the internal state of the handler is
# preserved in case of an exception.
class AbortingWriter:
def write(self, b):
raise CustomException

stderr = StringIO()
environ = {"SERVER_PROTOCOL": "HTTP/1.0"}
h = SimpleHandler(BytesIO(), AbortingWriter(), stderr, environ)
h.run(hello_app)

self.assertIn("CustomException", stderr.getvalue())

# Test that the internal state of the handler is preserved.
self.assertIsNotNone(h.result)
self.assertIsNotNone(h.headers)
self.assertIsNotNone(h.status)
self.assertIsNotNone(h.environ)


if __name__ == "__main__":
unittest.main()
11 changes: 10 additions & 1 deletion 11 Lib/wsgiref/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,16 @@ def finish_response(self):
for data in self.result:
self.write(data)
self.finish_content()
finally:
except:
# Call close() on the iterable returned by the WSGI application
# in case of an exception.
if hasattr(self.result, 'close'):
self.result.close()
raise
else:
# We only call close() when no exception is raised, because it
# will set status, result, headers, and environ fields to None.
# See bpo-29183 for more details.
self.close()


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix double exceptions in :class:`wsgiref.handlers.BaseHandler` by calling
its :meth:`~wsgiref.handlers.BaseHandler.close` method only when no
exception is raised.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.