Skip to content

Commit 4e2cfab

Browse files
committed
Fix the fix of the fixer fix
1 parent 4c8d5c8 commit 4e2cfab

File tree

4 files changed

+46
-29
lines changed

4 files changed

+46
-29
lines changed

Lib/test/libregrtest/run_workers.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
from .logger import Logger
1818
from .result import TestResult, State
1919
from .results import TestResults
20-
from .runtests import RunTests, JsonFileType, JSON_FILE_USE_FILENAME
20+
from .runtests import RunTests, JsonFileType
2121
from .single import PROGRESS_MIN_TIME
2222
from .utils import (
23-
StrPath, StrJSON, TestName, MS_WINDOWS,
23+
StrPath, StrJSON, TestName, MS_WINDOWS, TMP_PREFIX,
2424
format_duration, print_warning, count, plural)
2525
from .worker import create_worker_process, USE_PROCESS_GROUP
2626

@@ -228,9 +228,14 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
228228
err_msg = None
229229

230230
stdout_file = tempfile.TemporaryFile('w+', encoding=encoding)
231-
if JSON_FILE_USE_FILENAME:
232-
json_tmpfile = tempfile.NamedTemporaryFile('w+', encoding='utf8')
233-
print("main process: create NamedTemporaryFile")
231+
232+
json_file_use_filename = self.runtests.json_file_use_filename()
233+
print("main process: json_file_use_filename?", json_file_use_filename)
234+
if json_file_use_filename:
235+
prefix = TMP_PREFIX + 'json_'
236+
json_tmpfile = tempfile.NamedTemporaryFile('w+', encoding='utf8',
237+
prefix=prefix)
238+
print("main process: create NamedTemporaryFile", json_tmpfile.name)
234239
else:
235240
json_tmpfile = tempfile.TemporaryFile('w+', encoding='utf8')
236241
print("main process: create TemporaryFile")
@@ -239,14 +244,14 @@ def _runtest(self, test_name: TestName) -> MultiprocessResult:
239244
# non-blocking pipes on Emscripten with NodeJS.
240245
with (stdout_file, json_tmpfile):
241246
stdout_fd = stdout_file.fileno()
242-
if JSON_FILE_USE_FILENAME:
247+
if json_file_use_filename:
243248
json_file = json_tmpfile.name
244249
else:
245250
json_file = json_tmpfile.fileno()
246251
if MS_WINDOWS:
247252
json_file = msvcrt.get_osfhandle(json_file)
248-
print("main process json_type file:", type(json_file))
249-
print("main process json_type:", json_file)
253+
print("main process json_file type:", type(json_file))
254+
print("main process json_file:", json_file)
250255

251256
kwargs = {}
252257
if match_tests:

Lib/test/libregrtest/runtests.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,10 @@
88
StrPath, StrJSON, TestTuple, FilterTuple, FilterDict)
99

1010

11-
if support.is_emscripten or support.is_wasi:
12-
# On Emscripten/WASI, it's a filename. Passing a file descriptor to a
13-
# worker process fails with "OSError: [Errno 8] Bad file descriptor" in the
14-
# worker process.
15-
JsonFileType = StrPath
16-
JSON_FILE_USE_FILENAME = True
17-
else:
18-
# On Unix, it's a file descriptor.
19-
# On Windows, it's a handle.
20-
JsonFileType = int
21-
JSON_FILE_USE_FILENAME = False
11+
# See RunTests.json_file_use_filename()
12+
JsonFileType = int | StrPath
2213

2314
import os
24-
print(os.getpid(), "JSON_FILE_USE_FILENAME:", JSON_FILE_USE_FILENAME)
2515
print(os.getpid(), "JsonFileType:", JsonFileType)
2616

2717

@@ -58,7 +48,7 @@ class RunTests:
5848
python_cmd: tuple[str] | None
5949
randomize: bool
6050
random_seed: int | None
61-
json_file: JsonFileType | None
51+
json_file: JsonFileType | None
6252

6353
def copy(self, **override):
6454
state = dataclasses.asdict(self)
@@ -92,6 +82,18 @@ def as_json(self) -> StrJSON:
9282
def from_json(worker_json: StrJSON) -> 'RunTests':
9383
return json.loads(worker_json, object_hook=_decode_runtests)
9484

85+
def json_file_use_filename(self):
86+
# On Unix, it's a file descriptor.
87+
# On Windows, it's a handle.
88+
# On Emscripten/WASI, it's a filename. Passing a file descriptor to a
89+
# worker process fails with "OSError: [Errno 8] Bad file descriptor" in the
90+
# worker process.
91+
return (
92+
self.python_cmd
93+
or support.is_emscripten
94+
or support.is_wasi
95+
)
96+
9597

9698
class _EncodeRunTests(json.JSONEncoder):
9799
def default(self, o: Any) -> dict[str, Any]:

Lib/test/libregrtest/utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717

1818

1919
MS_WINDOWS = (sys.platform == 'win32')
20-
WORK_DIR_PREFIX = 'test_python_'
21-
WORKER_WORK_DIR_PREFIX = f'{WORK_DIR_PREFIX}worker_'
20+
21+
# All temporary files and temporary directories created by libregrtest should
22+
# use TMP_PREFIX so cleanup_temp_dir() can remove them all.
23+
TMP_PREFIX = 'test_python_'
24+
WORK_DIR_PREFIX = TMP_PREFIX
25+
WORKER_WORK_DIR_PREFIX = WORK_DIR_PREFIX + 'worker_'
2226

2327
# bpo-38203: Maximum delay in seconds to exit Python (call Py_Finalize()).
2428
# Used to protect against threading._shutdown() hang.
@@ -580,7 +584,7 @@ def display_header():
580584
def cleanup_temp_dir(tmp_dir: StrPath):
581585
import glob
582586

583-
path = os.path.join(glob.escape(tmp_dir), WORK_DIR_PREFIX + '*')
587+
path = os.path.join(glob.escape(tmp_dir), TMP_PREFIX + '*')
584588
print("Cleanup %s directory" % tmp_dir)
585589
for name in glob.glob(path):
586590
if os.path.isdir(name):

Lib/test/libregrtest/worker.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from test.support import os_helper
88

99
from .setup import setup_process, setup_test_dir
10-
from .runtests import RunTests, JsonFileType, JSON_FILE_USE_FILENAME
10+
from .runtests import RunTests, JsonFileType
1111
from .single import run_single_test
1212
from .utils import (
1313
StrPath, StrJSON, FilterTuple, MS_WINDOWS,
@@ -57,17 +57,23 @@ def create_worker_process(runtests: RunTests,
5757
close_fds=True,
5858
cwd=work_dir,
5959
)
60-
if JSON_FILE_USE_FILENAME:
61-
# Nothing to do to pass the JSON filename to the worker process
60+
61+
# Pass json_file to the worker process
62+
if isinstance(json_file, str):
63+
# Filename: nothing to do to
64+
print("create_worker_process() json_file: filename")
6265
pass
6366
elif MS_WINDOWS:
64-
# Pass the JSON handle to the worker process
67+
# Windows handle
68+
print("create_worker_process() json_file: Windows handle")
6569
startupinfo = subprocess.STARTUPINFO()
6670
startupinfo.lpAttributeList = {"handle_list": [json_file]}
6771
kwargs['startupinfo'] = startupinfo
6872
else:
69-
# Pass the JSON file descriptor to the worker process
73+
# Unix file descriptor
74+
print("create_worker_process() json_file: Unix fd")
7075
kwargs['pass_fds'] = [json_file]
76+
7177
if USE_PROCESS_GROUP:
7278
kwargs['start_new_session'] = True
7379

0 commit comments

Comments
 (0)