2121from .runtests import RunTests , JsonFile , JsonFileType
2222from .single import PROGRESS_MIN_TIME
2323from .utils import (
24- StrPath , StrJSON , TestName , MS_WINDOWS , TMP_PREFIX ,
24+ StrPath , StrJSON , TestName , MS_WINDOWS ,
2525 format_duration , print_warning , count , plural )
2626from .worker import create_worker_process , USE_PROCESS_GROUP
2727
@@ -225,16 +225,9 @@ def create_stdout(self, stack: contextlib.ExitStack) -> TextIO:
225225 def create_json_file (self , stack : contextlib .ExitStack ) -> tuple [JsonFile , TextIO | None ]:
226226 """Create JSON file."""
227227
228- json_file_use_filename = self .runtests .json_file_use_filename ()
229- if json_file_use_filename :
230- # create an empty file to make the creation atomic
231- # (to prevent races with other worker threads)
232- prefix = TMP_PREFIX + 'json_'
233- json_fd , json_filename = tempfile .mkstemp (prefix = prefix )
234- os .close (json_fd )
235-
236- stack .callback (os_helper .unlink , json_filename )
237- json_file = JsonFile (json_filename , JsonFileType .FILENAME )
228+ json_file_use_stdout = self .runtests .json_file_use_stdout ()
229+ if json_file_use_stdout :
230+ json_file = JsonFile (None , JsonFileType .STDOUT )
238231 json_tmpfile = None
239232 else :
240233 json_tmpfile = tempfile .TemporaryFile ('w+' , encoding = 'utf8' )
@@ -300,11 +293,14 @@ def read_stdout(self, stdout_file: TextIO) -> str:
300293 f"Cannot read process stdout: { exc } " , None )
301294
302295 def read_json (self , json_file : JsonFile , json_tmpfile : TextIO | None ,
303- stdout : str ) -> TestResult :
296+ stdout : str ) -> tuple [ TestResult , str ] :
304297 try :
305298 if json_tmpfile is not None :
306299 json_tmpfile .seek (0 )
307300 worker_json : StrJSON = json_tmpfile .read ()
301+ elif json_file .file_type == JsonFileType .STDOUT :
302+ stdout , _ , worker_json = stdout .rpartition ("\n " )
303+ stdout = stdout .rstrip ()
308304 else :
309305 with json_file .open (encoding = 'utf8' ) as json_fp :
310306 worker_json : StrJSON = json_fp .read ()
@@ -319,14 +315,16 @@ def read_json(self, json_file: JsonFile, json_tmpfile: TextIO | None,
319315 raise WorkerError (self .test_name , "empty JSON" , stdout )
320316
321317 try :
322- return TestResult .from_json (worker_json )
318+ result = TestResult .from_json (worker_json )
323319 except Exception as exc :
324320 # gh-101634: Catch UnicodeDecodeError if stdout cannot be
325321 # decoded from encoding
326322 err_msg = f"Failed to parse worker process JSON: { exc } "
327323 raise WorkerError (self .test_name , err_msg , stdout ,
328324 state = State .MULTIPROCESSING_ERROR )
329325
326+ return (result , stdout )
327+
330328 def _runtest (self , test_name : TestName ) -> MultiprocessResult :
331329 with contextlib .ExitStack () as stack :
332330 stdout_file = self .create_stdout (stack )
@@ -341,7 +339,7 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
341339 if retcode is None :
342340 raise WorkerError (self .test_name , None , stdout , state = State .TIMEOUT )
343341
344- result = self .read_json (json_file , json_tmpfile , stdout )
342+ result , stdout = self .read_json (json_file , json_tmpfile , stdout )
345343
346344 if retcode != 0 :
347345 raise WorkerError (self .test_name , f"Exit code { retcode } " , stdout )
0 commit comments