21
21
from .runtests import RunTests , JsonFile , JsonFileType
22
22
from .single import PROGRESS_MIN_TIME
23
23
from .utils import (
24
- StrPath , StrJSON , TestName , MS_WINDOWS , TMP_PREFIX ,
24
+ StrPath , StrJSON , TestName , MS_WINDOWS ,
25
25
format_duration , print_warning , count , plural )
26
26
from .worker import create_worker_process , USE_PROCESS_GROUP
27
27
@@ -225,16 +225,9 @@ def create_stdout(self, stack: contextlib.ExitStack) -> TextIO:
225
225
def create_json_file (self , stack : contextlib .ExitStack ) -> tuple [JsonFile , TextIO | None ]:
226
226
"""Create JSON file."""
227
227
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 )
238
231
json_tmpfile = None
239
232
else :
240
233
json_tmpfile = tempfile .TemporaryFile ('w+' , encoding = 'utf8' )
@@ -300,11 +293,14 @@ def read_stdout(self, stdout_file: TextIO) -> str:
300
293
f"Cannot read process stdout: { exc } " , None )
301
294
302
295
def read_json (self , json_file : JsonFile , json_tmpfile : TextIO | None ,
303
- stdout : str ) -> TestResult :
296
+ stdout : str ) -> tuple [ TestResult , str ] :
304
297
try :
305
298
if json_tmpfile is not None :
306
299
json_tmpfile .seek (0 )
307
300
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 ()
308
304
else :
309
305
with json_file .open (encoding = 'utf8' ) as json_fp :
310
306
worker_json : StrJSON = json_fp .read ()
@@ -319,14 +315,16 @@ def read_json(self, json_file: JsonFile, json_tmpfile: TextIO | None,
319
315
raise WorkerError (self .test_name , "empty JSON" , stdout )
320
316
321
317
try :
322
- return TestResult .from_json (worker_json )
318
+ result = TestResult .from_json (worker_json )
323
319
except Exception as exc :
324
320
# gh-101634: Catch UnicodeDecodeError if stdout cannot be
325
321
# decoded from encoding
326
322
err_msg = f"Failed to parse worker process JSON: { exc } "
327
323
raise WorkerError (self .test_name , err_msg , stdout ,
328
324
state = State .MULTIPROCESSING_ERROR )
329
325
326
+ return (result , stdout )
327
+
330
328
def _runtest (self , test_name : TestName ) -> MultiprocessResult :
331
329
with contextlib .ExitStack () as stack :
332
330
stdout_file = self .create_stdout (stack )
@@ -341,7 +339,7 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
341
339
if retcode is None :
342
340
raise WorkerError (self .test_name , None , stdout , state = State .TIMEOUT )
343
341
344
- result = self .read_json (json_file , json_tmpfile , stdout )
342
+ result , stdout = self .read_json (json_file , json_tmpfile , stdout )
345
343
346
344
if retcode != 0 :
347
345
raise WorkerError (self .test_name , f"Exit code { retcode } " , stdout )
0 commit comments