99import threading
1010import time
1111import traceback
12- from typing import Literal , TextIO
12+ from typing import Literal
1313
1414from test import support
1515from test .support import os_helper
2121from .single import PROGRESS_MIN_TIME
2222from .utils import (
2323 StrPath , StrJSON , TestName , MS_WINDOWS ,
24- format_duration , print_warning )
24+ format_duration , print_warning , WORKER_DIR_PREFIX )
2525from .worker import create_worker_process , USE_PROCESS_GROUP
2626
2727if MS_WINDOWS :
@@ -156,10 +156,10 @@ def mp_result_error(
156156 return MultiprocessResult (test_result , stdout , err_msg )
157157
158158 def _run_process (self , runtests : RunTests , output_fd : int , json_fd : int ,
159- tmp_dir : StrPath | None = None ) -> int :
159+ start_work_dir : StrPath | None = None ) -> int :
160160 try :
161161 popen = create_worker_process (runtests , output_fd , json_fd ,
162- tmp_dir )
162+ start_work_dir )
163163
164164 self ._killed = False
165165 self ._popen = popen
@@ -235,33 +235,39 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
235235 if MS_WINDOWS :
236236 json_fd = msvcrt .get_osfhandle (json_fd )
237237
238+ # gh-93353: Check for leaked temporary files in the parent process,
239+ # since the deletion of temporary files can happen late during
240+ # Python finalization: too late for libregrtest.
241+ tmp_dir = tempfile .mkdtemp (prefix = WORKER_DIR_PREFIX )
242+ tmp_dir = os .path .abspath (tmp_dir )
243+
238244 kwargs = {}
239245 if match_tests :
240246 kwargs ['match_tests' ] = match_tests
241247 worker_runtests = self .runtests .copy (
242248 tests = tests ,
243249 json_fd = json_fd ,
250+ work_dir = tmp_dir ,
244251 ** kwargs )
245252
246- # gh-93353: Check for leaked temporary files in the parent process,
247- # since the deletion of temporary files can happen late during
248- # Python finalization: too late for libregrtest.
249- if not support .is_wasi :
250- # Don't check for leaked temporary files and directories if Python is
251- # run on WASI. WASI don't pass environment variables like TMPDIR to
252- # worker processes.
253- tmp_dir = tempfile .mkdtemp (prefix = "test_python_" )
254- tmp_dir = os .path .abspath (tmp_dir )
255- try :
256- retcode = self ._run_process (worker_runtests ,
257- stdout_fd , json_fd , tmp_dir )
258- finally :
259- tmp_files = os .listdir (tmp_dir )
260- os_helper .rmtree (tmp_dir )
253+ # Starting working directory of the worker process.
254+ #
255+ # Emscripten and WASI Python must start in the Python source code
256+ # directory to get 'python.js' or 'python.wasm' file.
257+ #
258+ # Then worker_process() calls change_cwd(runtests.work_dir).
259+ if support .is_emscripten or support .is_wasi :
260+ start_work_dir = os_helper .SAVEDCWD
261261 else :
262+ start_work_dir = tmp_dir
263+
264+ try :
262265 retcode = self ._run_process (worker_runtests ,
263- stdout_fd , json_fd )
264- tmp_files = ()
266+ stdout_fd , json_fd ,
267+ start_work_dir )
268+ finally :
269+ tmp_files = os .listdir (tmp_dir )
270+ os_helper .rmtree (tmp_dir )
265271 stdout_file .seek (0 )
266272
267273 try :
@@ -280,7 +286,7 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
280286 if worker_json :
281287 result = TestResult .from_json (worker_json )
282288 else :
283- err_msg = f "empty JSON"
289+ err_msg = "empty JSON"
284290 except Exception as exc :
285291 # gh-101634: Catch UnicodeDecodeError if stdout cannot be
286292 # decoded from encoding
0 commit comments