Skip to content

Commit 3730a2a

Browse files
committed
Improved subprocess handling.
1 parent 5ec2836 commit 3730a2a

File tree

5 files changed

+10
-65
lines changed

5 files changed

+10
-65
lines changed

README.rst

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ The following standard library modules are available on macOS, but not the other
2424
Apple platforms:
2525
* curses
2626
* posixshmem
27-
* posixsubprocess
2827

2928
The binaries support x86_64 and arm64 for macOS; arm64 for iOS and appleTV
3029
devices; and arm64_32 for watchOS. It also supports device simulators on both

patch/Python/Python.patch

+7-61
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ index 49bcaea78d..891356e54d 100644
699699
+ _CASE_INSENSITIVE_PLATFORMS_STR_KEY)
700700

701701
diff --git a/Lib/os.py b/Lib/os.py
702-
index d26cfc9993..b4db279e00 100644
702+
index d26cfc9993..ae6629424e 100644
703703
--- a/Lib/os.py
704704
+++ b/Lib/os.py
705705
@@ -36,7 +36,7 @@
@@ -716,7 +716,7 @@ index d26cfc9993..b4db279e00 100644
716716
del _fscodec
717717

718718
+
719-
+if sys.platform in ('iOS', 'tvos', 'watchos'):
719+
+if sys.platform in ('ios', 'tvos', 'watchos'):
720720
+ allows_subprocesses = False
721721
+else:
722722
+ allows_subprocesses = True
@@ -834,74 +834,20 @@ index 939893eb5e..8c550ed95a 100644
834834
return f'{userbase}/lib/python{version[0]}.{version[1]}/site-packages'
835835

836836
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
837-
index ccb46a6337..6193bcbeaf 100644
837+
index ccb46a6337..45fefd522d 100644
838838
--- a/Lib/subprocess.py
839839
+++ b/Lib/subprocess.py
840-
@@ -71,9 +71,13 @@
841-
_mswindows = True
842-
except ModuleNotFoundError:
843-
_mswindows = False
844-
- import _posixsubprocess
845-
- import select
846-
- import selectors
847-
+ try:
848-
+ import _posixsubprocess
849-
+ import select
850-
+ import selectors
851-
+ except ModuleNotFoundError:
852-
+ # iOS *has* subprocesses, but doesn't support them.
853-
+ _posixsubprocess = None
854-
else:
855-
from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP,
856-
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
857-
@@ -208,7 +212,7 @@
858-
return "%s(%d)" % (self.__class__.__name__, int(self))
859-
860-
__del__ = Close
861-
-else:
862-
+elif _posixsubprocess:
863-
# When select or poll has indicated that the file is writable,
864-
# we can write up to _PIPE_BUF bytes without risk of blocking.
865-
# POSIX defines PIPE_BUF as >= 512.
866-
@@ -236,7 +240,7 @@
867-
868-
def _cleanup():
869-
pass
870-
-else:
871-
+elif _posixsubprocess:
872-
# This lists holds Popen instances for which the underlying process had not
873-
# exited at the time its __del__ method got called: those processes are
874-
# wait()ed for synchronously from _cleanup() when a new Popen object is
875-
@@ -255,6 +259,9 @@
876-
# This can happen if two threads create a new Popen instance.
877-
# It's harmless that it was already removed, so ignore.
878-
pass
879-
+else:
880-
+ def _cleanup():
881-
+ pass
882-
883-
PIPE = -1
884-
STDOUT = -2
885-
@@ -656,7 +663,7 @@
886-
Prefer an implementation which can use vfork() in some cases for best
887-
performance.
888-
"""
889-
- if _mswindows or not hasattr(os, 'posix_spawn'):
890-
+ if _mswindows or _posixsubprocess is None or not hasattr(os, 'posix_spawn'):
891-
# os.posix_spawn() is not available
892-
return False
893-
894-
@@ -759,6 +766,9 @@
840+
@@ -759,6 +759,9 @@
895841
pass_fds=(), *, user=None, group=None, extra_groups=None,
896842
encoding=None, errors=None, text=None, umask=-1, pipesize=-1):
897843
"""Create new Popen instance."""
898-
+ if not _mswindows and _posixsubprocess is None:
844+
+ if not os.allows_subprocesses:
899845
+ raise RuntimeError(f"Subprocesses are not supported on {sys.platform}")
900846
+
901847
_cleanup()
902848
# Held while anything is calling waitpid before returncode has been
903849
# updated to prevent clobbering returncode if wait() or poll() are
904-
@@ -1855,7 +1865,7 @@
850+
@@ -1855,7 +1858,7 @@
905851
else:
906852
self.returncode = waitstatus_to_exitcode(sts)
907853

@@ -910,7 +856,7 @@ index ccb46a6337..6193bcbeaf 100644
910856
_WNOHANG=os.WNOHANG, _ECHILD=errno.ECHILD):
911857
"""Check if child process has terminated. Returns returncode
912858
attribute.
913-
@@ -1864,6 +1874,8 @@
859+
@@ -1864,6 +1867,8 @@
914860
outside of the local scope (nor can any methods it calls).
915861

916862
"""

patch/Python/Setup.embedded

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ _multiprocessing _multiprocessing/multiprocessing.c _multiprocessing/semaphore.c
3636
_opcode _opcode.c
3737
_operator _operator.c
3838
_pickle _pickle.c -DPy_BUILD_CORE_MODULE
39+
_posixsubprocess _posixsubprocess.c
3940
_queue _queuemodule.c
4041
_random _randommodule.c -DPy_BUILD_CORE_MODULE
4142
_sha1 sha1module.c

patch/Python/Setup.macOS

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ _decimal _decimal/_decimal.c \
3737
-DCONFIG_64=1 -DANSI=1 -DHAVE_UINT128_T=1
3838

3939
_posixshmem -I$(srcdir)/Modules/_multiprocessing _multiprocessing/posixshmem.c
40-
_posixsubprocess _posixsubprocess.c
4140

4241
_scproxy _scproxy.c -framework SystemConfiguration -framework CoreFoundation
4342

tests/testbed/src/testbed/app.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ def main():
4848
print(f"{sys_platform}: {test.__name__}", end="...")
4949
test()
5050
print(" ok")
51-
except Exception as e:
51+
except Exception:
5252
failures += 1
5353
print(" FAILED!")
5454
print("-" * 80)
55-
traceback.print_exception(e)
55+
traceback.print_exc()
5656
print("-" * 80)
5757

5858
print("=" * 80)

0 commit comments

Comments
 (0)