Skip to content

Commit 2a03bd8

Browse files
jaracoclaude
andcommitted
Source requires_fork through a shim tolerant of where test.support keeps it.
test.support is a private, unversioned corner of the stdlib: its helpers migrate between the package and its *_helper submodules, and some distributions ship Python without the test package at all. Look for requires_fork in each plausible home and approximate it when no home has one, so the import can't break a supported Python. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent cfa0e04 commit 2a03bd8

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎tests/compat/support.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
``test.support.requires_fork``, defended against a stdlib that keeps it
3+
somewhere else or not at all.
4+
5+
Relying on ``test.support`` is fine, but it's a private, unversioned corner
6+
of the stdlib: its helpers migrate between the package and its ``*_helper``
7+
submodules, and some distributions ship Python without the ``test`` package.
8+
Prefer whatever the running interpreter supplies and approximate it
9+
otherwise.
10+
"""
11+
12+
import contextlib
13+
import importlib
14+
import os
15+
import sys
16+
import unittest
17+
18+
homes = (
19+
'test.support',
20+
'test.support.os_helper',
21+
'test.support.threading_helper',
22+
)
23+
"""
24+
Modules that host ``requires_fork`` or might plausibly come to host it.
25+
"""
26+
27+
28+
def _supplied():
29+
"""
30+
The stdlib's own ``requires_fork``, wherever it currently lives.
31+
"""
32+
for home in homes:
33+
with contextlib.suppress(ImportError, AttributeError):
34+
return importlib.import_module(home).requires_fork
35+
36+
37+
def _approximated():
38+
"""
39+
Stand in for ``requires_fork`` on a stdlib that doesn't supply one.
40+
41+
Mirrors ``test.support.has_fork_support``: skip where ``os.fork`` is
42+
missing, where subprocesses don't work at all (WASM, Apple mobile), and
43+
on Android, where forking is unsafe because every app is threaded.
44+
"""
45+
unsupported = {'emscripten', 'wasi', 'ios', 'tvos', 'watchos', 'android'}
46+
supported = hasattr(os, 'fork') and sys.platform not in unsupported
47+
return unittest.skipUnless(supported, 'requires working os.fork()')
48+
49+
50+
requires_fork = _supplied() or _approximated

‎tests/test_zip.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import sys
44
import unittest
55

6-
from test.support import requires_fork
7-
86
from importlib_metadata import (
97
FastPath,
108
PackageNotFoundError,
@@ -17,6 +15,7 @@
1715

1816
from . import fixtures
1917
from .compat.py314 import warnings_helper
18+
from .compat.support import requires_fork
2019

2120

2221
class TestZip(fixtures.ZipFixtures, unittest.TestCase):

0 commit comments

Comments
 (0)