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 (
2323 StrPath , StrJSON , TestName , MS_WINDOWS ,
@@ -155,10 +155,11 @@ def mp_result_error(
155155 ) -> MultiprocessResult :
156156 return 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 ,
159160 tmp_dir : StrPath | None = None ) -> int :
160161 try :
161- popen = create_worker_process (runtests , output_fd , json_fd ,
162+ popen = create_worker_process (runtests , output_fd , json_file ,
162163 tmp_dir )
163164
164165 self ._killed = False
@@ -226,21 +227,29 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
226227 match_tests = None
227228 err_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 ):
233239 stdout_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
238247 kwargs = {}
239248 if match_tests :
240249 kwargs ['match_tests' ] = match_tests
241250 worker_runtests = self .runtests .copy (
242251 tests = 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:
254263 tmp_dir = os .path .abspath (tmp_dir )
255264 try :
256265 retcode = self ._run_process (worker_runtests ,
257- stdout_fd , json_fd , tmp_dir )
266+ stdout_fd , json_file , tmp_dir )
258267 finally :
259268 tmp_files = os .listdir (tmp_dir )
260269 os_helper .rmtree (tmp_dir )
261270 else :
262271 retcode = self ._run_process (worker_runtests ,
263- stdout_fd , json_fd )
272+ stdout_fd , json_file )
264273 tmp_files = ()
265274 stdout_file .seek (0 )
266275
@@ -275,8 +284,8 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
275284
276285 try :
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 ()
280289 if worker_json :
281290 result = TestResult .from_json (worker_json )
282291 else :
0 commit comments