Skip to content

Commit 64b9b29

Browse files
committed
[3.10] 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 976ea78 commit 64b9b29

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

Lib/subprocess.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,19 @@
6565
# NOTE: We intentionally exclude list2cmdline as it is
6666
# considered an internal implementation detail. issue10838.
6767

68+
# use presence of msvcrt to detect Windows-like platforms (see bpo-8110)
6869
try:
6970
import msvcrt
70-
import _winapi
71-
_mswindows = True
7271
except ModuleNotFoundError:
7372
_mswindows = False
74-
import _posixsubprocess
75-
import select
76-
import selectors
7773
else:
74+
_mswindows = True
75+
76+
# some platforms do not support subprocesses
77+
_can_fork_exec = sys.platform not in {"ios", "tvos", "watchos"}
78+
79+
if _mswindows:
80+
import _winapi
7881
from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP,
7982
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
8083
STD_ERROR_HANDLE, SW_HIDE,
@@ -95,6 +98,12 @@
9598
"NORMAL_PRIORITY_CLASS", "REALTIME_PRIORITY_CLASS",
9699
"CREATE_NO_WINDOW", "DETACHED_PROCESS",
97100
"CREATE_DEFAULT_ERROR_MODE", "CREATE_BREAKAWAY_FROM_JOB"])
101+
else:
102+
if _can_fork_exec:
103+
import _posixsubprocess
104+
105+
import select
106+
import selectors
98107

99108

100109
# Exception classes used by this module.
@@ -764,6 +773,11 @@ def __init__(self, args, bufsize=-1, executable=None,
764773
pass_fds=(), *, user=None, group=None, extra_groups=None,
765774
encoding=None, errors=None, text=None, umask=-1, pipesize=-1):
766775
"""Create new Popen instance."""
776+
if not _can_fork_exec:
777+
raise OSError(
778+
errno.ENOTSUP, f"{sys.platform} does not support processes."
779+
)
780+
767781
_cleanup()
768782
# Held while anything is calling waitpid before returncode has been
769783
# updated to prevent clobbering returncode if wait() or poll() are
Lines changed: 1 addition & 0 deletions
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)