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 , # TMP_PREFIX,
25
25
format_duration , print_warning , count , plural )
26
26
from .worker import create_worker_process , USE_PROCESS_GROUP
27
27
@@ -225,17 +225,20 @@ 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
232
+ #elif 0:
233
+ # # create an empty file to make the creation atomic
234
+ # # (to prevent races with other worker threads)
235
+ # prefix = TMP_PREFIX + 'json_'
236
+ # json_fd, json_filename = tempfile.mkstemp(prefix=prefix)
237
+ # os.close(json_fd)
238
+ #
239
+ # stack.callback(os_helper.unlink, json_filename)
240
+ # json_file = JsonFile(json_filename, JsonFileType.FILENAME)
241
+ # json_tmpfile = None
239
242
else :
240
243
json_tmpfile = tempfile .TemporaryFile ('w+' , encoding = 'utf8' )
241
244
stack .enter_context (json_tmpfile )
@@ -300,11 +303,14 @@ def read_stdout(self, stdout_file: TextIO) -> str:
300
303
f"Cannot read process stdout: { exc } " , None )
301
304
302
305
def read_json (self , json_file : JsonFile , json_tmpfile : TextIO | None ,
303
- stdout : str ) -> TestResult :
306
+ stdout : str ) -> ( TestResult , str ) :
304
307
try :
305
308
if json_tmpfile is not None :
306
309
json_tmpfile .seek (0 )
307
310
worker_json : StrJSON = json_tmpfile .read ()
311
+ elif json_file .file_type == JsonFileType .STDOUT :
312
+ stdout , _ , worker_json = stdout .rpartition ("\n " )
313
+ stdout = stdout .rstrip ()
308
314
else :
309
315
with json_file .open (encoding = 'utf8' ) as json_fp :
310
316
worker_json : StrJSON = json_fp .read ()
@@ -319,14 +325,16 @@ def read_json(self, json_file: JsonFile, json_tmpfile: TextIO | None,
319
325
raise WorkerError (self .test_name , "empty JSON" , stdout )
320
326
321
327
try :
322
- return TestResult .from_json (worker_json )
328
+ result = TestResult .from_json (worker_json )
323
329
except Exception as exc :
324
330
# gh-101634: Catch UnicodeDecodeError if stdout cannot be
325
331
# decoded from encoding
326
332
err_msg = f"Failed to parse worker process JSON: { exc } "
327
333
raise WorkerError (self .test_name , err_msg , stdout ,
328
334
state = State .MULTIPROCESSING_ERROR )
329
335
336
+ return (result , stdout )
337
+
330
338
def _runtest (self , test_name : TestName ) -> MultiprocessResult :
331
339
with contextlib .ExitStack () as stack :
332
340
stdout_file = self .create_stdout (stack )
@@ -341,7 +349,7 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
341
349
if retcode is None :
342
350
raise WorkerError (self .test_name , None , stdout , state = State .TIMEOUT )
343
351
344
- result = self .read_json (json_file , json_tmpfile , stdout )
352
+ result , stdout = self .read_json (json_file , json_tmpfile , stdout )
345
353
346
354
if retcode != 0 :
347
355
raise WorkerError (self .test_name , f"Exit code { retcode } " , stdout )
0 commit comments