@@ -589,32 +589,35 @@ def test_for_crash_traceback_with_redirected_stdout(self):
589589 with TemporaryDirectory () as tmp_dir :
590590 stdout_path = os .path .join (tmp_dir , "WinCMDLineTests.txt" )
591591
592- with open (stdout_path , "w" , encoding = "utf-8" ) as stdout_file , \
593- subprocess .Popen (
592+ with open (stdout_path , "w" ) as stdout_file , \
593+ subprocess .Popen (
594594 [sys .executable , '-i' , '-c' , script_command ],
595595 stdin = None ,
596596 stdout = stdout_file ,
597597 stderr = subprocess .PIPE ,
598- text = True , encoding = 'utf-8' , errors = 'replace'
599- ) as process :
600-
601- time .sleep (3 )
602-
603- if process .poll () is None :
598+ text = True , errors = 'replace'
599+ ) as process :
600+
601+ stderr_output = ""
602+
603+ try :
604+ process .wait (timeout = 3 )
605+ self .fail (
606+ "Process exited unexpectedly within the timeout."
607+ )
608+ except subprocess .TimeoutExpired :
604609 process .kill ()
610+ _ , stderr_output = process .communicate ()
605611
606- stderr_output = process .stderr .read ()
607-
608- has_crash_traceback = (
609- "OSError" in stderr_output
610- and len (stderr_output ) > 1200
611- )
612+ has_crash_traceback = (
613+ "OSError" in stderr_output and
614+ len (stderr_output ) > 1200
615+ )
616+
617+ if has_crash_traceback :
618+ self .fail ("Detected endless OSError traceback."
619+ f"\n --- stderr ---\n { stderr_output [:1200 ]} " )
612620
613- if has_crash_traceback :
614- self .fail (
615- "Detected the endless OSError traceback.\n "
616- f"Stderr was:\n { stderr_output [:1200 ]} "
617- )
618621
619622if __name__ == "__main__" :
620623 unittest .main ()
0 commit comments