@@ -158,6 +158,33 @@ def main():
158158
159159 ''' )
160160
161+ flush_std_files_common_code = textwrap .dedent ('''
162+ import sys
163+
164+ class FakeIO:
165+ def __init__(self, what):
166+ self.what = what
167+ def write(self, str):
168+ pass
169+ def flush(self):
170+ pass
171+ def fileno(self):
172+ return 0
173+
174+ @property
175+ def closed(self):
176+ stdfile = getattr(sys, self.what)
177+ setattr(sys, self.what, getattr(sys, "__" + self.what + "__"))
178+ del stdfile
179+ return False
180+
181+ def main():
182+ setattr(sys, '{0}', FakeIO('{0}'))
183+
184+ if __name__ == "__main__":
185+ main()
186+ ''' )
187+
161188
162189 def test_print_deleted_stdout (self ):
163190 # print should use strong reference to the stdout.
@@ -282,5 +309,19 @@ def test_errors_unraisablehook(self):
282309 self .assertNotIn (b"Segmentation fault" , err )
283310 self .assertNotIn (b"access violation" , err )
284311
312+ def test_py_finalize_flush_std_files_stdout (self ):
313+ test_code = self .flush_std_files_common_code .format ("stdout" )
314+ rc , _ , err = assert_python_ok ('-c' , test_code )
315+ self .assertEqual (rc , 0 )
316+ self .assertNotIn (b"Segmentation fault" , err )
317+ self .assertNotIn (b"access violation" , err )
318+
319+ def test_py_finalize_flush_std_files_stderr (self ):
320+ test_code = self .flush_std_files_common_code .format ("stderr" )
321+ rc , _ , err = assert_python_ok ('-c' , test_code )
322+ self .assertEqual (rc , 0 )
323+ self .assertNotIn (b"Segmentation fault" , err )
324+ self .assertNotIn (b"access violation" , err )
325+
285326if __name__ == "__main__" :
286327 unittest .main ()
0 commit comments