Skip to content

Commit

Permalink
Improved subprocess handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed May 16, 2022
1 parent 5ec2836 commit 3730a2a
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 65 deletions.
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ The following standard library modules are available on macOS, but not the other
Apple platforms:
* curses
* posixshmem
* posixsubprocess

The binaries support x86_64 and arm64 for macOS; arm64 for iOS and appleTV
devices; and arm64_32 for watchOS. It also supports device simulators on both
Expand Down
68 changes: 7 additions & 61 deletions patch/Python/Python.patch
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ index 49bcaea78d..891356e54d 100644
+ _CASE_INSENSITIVE_PLATFORMS_STR_KEY)

diff --git a/Lib/os.py b/Lib/os.py
index d26cfc9993..b4db279e00 100644
index d26cfc9993..ae6629424e 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -36,7 +36,7 @@
Expand All @@ -716,7 +716,7 @@ index d26cfc9993..b4db279e00 100644
del _fscodec

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

diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index ccb46a6337..6193bcbeaf 100644
index ccb46a6337..45fefd522d 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -71,9 +71,13 @@
_mswindows = True
except ModuleNotFoundError:
_mswindows = False
- import _posixsubprocess
- import select
- import selectors
+ try:
+ import _posixsubprocess
+ import select
+ import selectors
+ except ModuleNotFoundError:
+ # iOS *has* subprocesses, but doesn't support them.
+ _posixsubprocess = None
else:
from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP,
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
@@ -208,7 +212,7 @@
return "%s(%d)" % (self.__class__.__name__, int(self))

__del__ = Close
-else:
+elif _posixsubprocess:
# When select or poll has indicated that the file is writable,
# we can write up to _PIPE_BUF bytes without risk of blocking.
# POSIX defines PIPE_BUF as >= 512.
@@ -236,7 +240,7 @@

def _cleanup():
pass
-else:
+elif _posixsubprocess:
# This lists holds Popen instances for which the underlying process had not
# exited at the time its __del__ method got called: those processes are
# wait()ed for synchronously from _cleanup() when a new Popen object is
@@ -255,6 +259,9 @@
# This can happen if two threads create a new Popen instance.
# It's harmless that it was already removed, so ignore.
pass
+else:
+ def _cleanup():
+ pass

PIPE = -1
STDOUT = -2
@@ -656,7 +663,7 @@
Prefer an implementation which can use vfork() in some cases for best
performance.
"""
- if _mswindows or not hasattr(os, 'posix_spawn'):
+ if _mswindows or _posixsubprocess is None or not hasattr(os, 'posix_spawn'):
# os.posix_spawn() is not available
return False

@@ -759,6 +766,9 @@
@@ -759,6 +759,9 @@
pass_fds=(), *, user=None, group=None, extra_groups=None,
encoding=None, errors=None, text=None, umask=-1, pipesize=-1):
"""Create new Popen instance."""
+ if not _mswindows and _posixsubprocess is None:
+ if not os.allows_subprocesses:
+ raise RuntimeError(f"Subprocesses are not supported on {sys.platform}")
+
_cleanup()
# Held while anything is calling waitpid before returncode has been
# updated to prevent clobbering returncode if wait() or poll() are
@@ -1855,7 +1865,7 @@
@@ -1855,7 +1858,7 @@
else:
self.returncode = waitstatus_to_exitcode(sts)

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

"""
Expand Down
1 change: 1 addition & 0 deletions patch/Python/Setup.embedded
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ _multiprocessing _multiprocessing/multiprocessing.c _multiprocessing/semaphore.c
_opcode _opcode.c
_operator _operator.c
_pickle _pickle.c -DPy_BUILD_CORE_MODULE
_posixsubprocess _posixsubprocess.c
_queue _queuemodule.c
_random _randommodule.c -DPy_BUILD_CORE_MODULE
_sha1 sha1module.c
Expand Down
1 change: 0 additions & 1 deletion patch/Python/Setup.macOS
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ _decimal _decimal/_decimal.c \
-DCONFIG_64=1 -DANSI=1 -DHAVE_UINT128_T=1

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

_scproxy _scproxy.c -framework SystemConfiguration -framework CoreFoundation

Expand Down
4 changes: 2 additions & 2 deletions tests/testbed/src/testbed/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ def main():
print(f"{sys_platform}: {test.__name__}", end="...")
test()
print(" ok")
except Exception as e:
except Exception:
failures += 1
print(" FAILED!")
print("-" * 80)
traceback.print_exception(e)
traceback.print_exc()
print("-" * 80)

print("=" * 80)
Expand Down

0 comments on commit 3730a2a

Please sign in to comment.