Skip to content

Commit acf814a

Browse files
committed
[3.9] pythongh-112736: Refactor del-safe symbol handling in subprocess (python#112738)
Refactor delete-safe symbol handling in subprocess. Only module globals are force-cleared during interpreter finalization, using a class reference instead of individually listing the constants everywhere is simpler.
1 parent f3994ad commit acf814a

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

Lib/subprocess.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,19 @@
6969
# NOTE: We intentionally exclude list2cmdline as it is
7070
# considered an internal implementation detail. issue10838.
7171

72+
# use presence of msvcrt to detect Windows-like platforms (see bpo-8110)
7273
try:
7374
import msvcrt
74-
import _winapi
75-
_mswindows = True
7675
except ModuleNotFoundError:
7776
_mswindows = False
78-
import _posixsubprocess
79-
import select
80-
import selectors
8177
else:
78+
_mswindows = True
79+
80+
# some platforms do not support subprocesses
81+
_can_fork_exec = sys.platform not in {"ios", "tvos", "watchos"}
82+
83+
if _mswindows:
84+
import _winapi
8285
from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP,
8386
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
8487
STD_ERROR_HANDLE, SW_HIDE,
@@ -99,6 +102,12 @@
99102
"NORMAL_PRIORITY_CLASS", "REALTIME_PRIORITY_CLASS",
100103
"CREATE_NO_WINDOW", "DETACHED_PROCESS",
101104
"CREATE_DEFAULT_ERROR_MODE", "CREATE_BREAKAWAY_FROM_JOB"])
105+
else:
106+
if _can_fork_exec:
107+
import _posixsubprocess
108+
109+
import select
110+
import selectors
102111

103112

104113
# Exception classes used by this module.
@@ -762,6 +771,11 @@ def __init__(self, args, bufsize=-1, executable=None,
762771
pass_fds=(), *, user=None, group=None, extra_groups=None,
763772
encoding=None, errors=None, text=None, umask=-1):
764773
"""Create new Popen instance."""
774+
if not _can_fork_exec:
775+
raise OSError(
776+
errno.ENOTSUP, f"{sys.platform} does not support processes."
777+
)
778+
765779
_cleanup()
766780
# Held while anything is calling waitpid before returncode has been
767781
# updated to prevent clobbering returncode if wait() or poll() are
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The use of del-safe symbols in ``subprocess`` was refactored to allow for use in cross-platform build environments.

0 commit comments

Comments
 (0)