File tree Expand file tree Collapse file tree 3 files changed +38
-1
lines changed
Filter options
Expand file tree Collapse file tree 3 files changed +38
-1
lines changed
Original file line number Diff line number Diff line change @@ -806,6 +806,31 @@ def write(self, b):
806
806
807
807
self .assertFalse (stderr .getvalue ())
808
808
809
+ def testDontResetInternalStateOnException (self ):
810
+ class CustomException (ValueError ):
811
+ pass
812
+
813
+ # We are raising CustomException here to trigger an exception
814
+ # during the execution of SimpleHandler.finish_response(), so
815
+ # we can easily test that the internal state of the handler is
816
+ # preserved in case of an exception.
817
+ class AbortingWriter :
818
+ def write (self , b ):
819
+ raise CustomException
820
+
821
+ stderr = StringIO ()
822
+ environ = {"SERVER_PROTOCOL" : "HTTP/1.0" }
823
+ h = SimpleHandler (BytesIO (), AbortingWriter (), stderr , environ )
824
+ h .run (hello_app )
825
+
826
+ self .assertIn ("CustomException" , stderr .getvalue ())
827
+
828
+ # Test that the internal state of the handler is preserved.
829
+ self .assertIsNotNone (h .result )
830
+ self .assertIsNotNone (h .headers )
831
+ self .assertIsNotNone (h .status )
832
+ self .assertIsNotNone (h .environ )
833
+
809
834
810
835
if __name__ == "__main__" :
811
836
unittest .main ()
Original file line number Diff line number Diff line change @@ -183,7 +183,16 @@ def finish_response(self):
183
183
for data in self .result :
184
184
self .write (data )
185
185
self .finish_content ()
186
- finally :
186
+ except :
187
+ # Call close() on the iterable returned by the WSGI application
188
+ # in case of an exception.
189
+ if hasattr (self .result , 'close' ):
190
+ self .result .close ()
191
+ raise
192
+ else :
193
+ # We only call close() when no exception is raised, because it
194
+ # will set status, result, headers, and environ fields to None.
195
+ # See bpo-29183 for more details.
187
196
self .close ()
188
197
189
198
Original file line number Diff line number Diff line change
1
+ Fix double exceptions in :class: `wsgiref.handlers.BaseHandler ` by calling
2
+ its :meth: `~wsgiref.handlers.BaseHandler.close ` method only when no
3
+ exception is raised.
You can’t perform that action at this time.
0 commit comments