1717from .logger import Logger
1818from .result import TestResult , State
1919from .results import TestResults
20- from .runtests import RunTests
20+ from .runtests import RunTests , JsonFileType , JSON_FILE_USE_FILENAME
2121from .single import PROGRESS_MIN_TIME
2222from .utils import (
2323StrPath , StrJSON , TestName , MS_WINDOWS ,
@@ -155,10 +155,11 @@ def mp_result_error(
155155 ) -> MultiprocessResult :
156156return MultiprocessResult (test_result , stdout , err_msg )
157157
158- def _run_process (self , runtests : RunTests , output_fd : int , json_fd : int ,
158+ def _run_process (self , runtests : RunTests , output_fd : int ,
159+ json_file : JsonFileType ,
159160tmp_dir : StrPath | None = None ) -> int :
160161try :
161- popen = create_worker_process (runtests , output_fd , json_fd ,
162+ popen = create_worker_process (runtests , output_fd , json_file ,
162163tmp_dir )
163164
164165self ._killed = False
@@ -226,21 +227,29 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
226227match_tests = None
227228err_msg = None
228229
230+ stdout_file = tempfile .TemporaryFile ('w+' , encoding = encoding )
231+ if JSON_FILE_USE_FILENAME :
232+ json_tmpfile = tempfile .NamedTemporaryFile ('w+' , encoding = 'utf8' )
233+ else :
234+ json_tmpfile = tempfile .TemporaryFile ('w+' , encoding = 'utf8' )
235+
229236# gh-94026: Write stdout+stderr to a tempfile as workaround for
230237# non-blocking pipes on Emscripten with NodeJS.
231- with (tempfile .TemporaryFile ('w+' , encoding = encoding ) as stdout_file ,
232- tempfile .TemporaryFile ('w+' , encoding = 'utf8' ) as json_file ):
238+ with (stdout_file , json_tmpfile ):
233239stdout_fd = stdout_file .fileno ()
234- json_fd = json_file .fileno ()
235- if MS_WINDOWS :
236- json_fd = msvcrt .get_osfhandle (json_fd )
240+ if JSON_FILE_USE_FILENAME :
241+ json_file = json_tmpfile .name
242+ else :
243+ json_file = json_tmpfile .fileno ()
244+ if MS_WINDOWS :
245+ json_file = msvcrt .get_osfhandle (json_file )
237246
238247kwargs = {}
239248if match_tests :
240249kwargs ['match_tests' ] = match_tests
241250worker_runtests = self .runtests .copy (
242251tests = tests ,
243- json_fd = json_fd ,
252+ json_file = json_file ,
244253** kwargs )
245254
246255# gh-93353: Check for leaked temporary files in the parent process,
@@ -254,13 +263,13 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
254263tmp_dir = os .path .abspath (tmp_dir )
255264try :
256265retcode = self ._run_process (worker_runtests ,
257- stdout_fd , json_fd , tmp_dir )
266+ stdout_fd , json_file , tmp_dir )
258267finally :
259268tmp_files = os .listdir (tmp_dir )
260269os_helper .rmtree (tmp_dir )
261270else :
262271retcode = self ._run_process (worker_runtests ,
263- stdout_fd , json_fd )
272+ stdout_fd , json_file )
264273tmp_files = ()
265274stdout_file .seek (0 )
266275
@@ -275,8 +284,8 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
275284
276285try :
277286# deserialize run_tests_worker() output
278- json_file .seek (0 )
279- worker_json : StrJSON = json_file .read ()
287+ json_tmpfile .seek (0 )
288+ worker_json : StrJSON = json_tmpfile .read ()
280289if worker_json :
281290result = TestResult .from_json (worker_json )
282291else :
0 commit comments