From 1013d06f15ddfacb460c1f4fa3565d7000818ac1 Mon Sep 17 00:00:00 2001 From: nanjekyejoannah Date: Mon, 18 Feb 2019 15:32:50 +0300 Subject: [PATCH 1/5] change subprocess to use posix_spawnp instead of posix_spawn --- Lib/subprocess.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 0496b447e8ea03..5107b056e050a9 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -607,10 +607,10 @@ def getoutput(cmd): return getstatusoutput(cmd)[1] -def _use_posix_spawn(): - """Check if posix_spawn() can be used for subprocess. +def _use_posix_spawnp(): + """Check if posix_spawnp() can be used for subprocess. - subprocess requires a posix_spawn() implementation that properly reports + subprocess requires a posix_spawnp() implementation that properly reports errors to the parent process, & sets errno on the following failures: * Process attribute actions failed. @@ -620,12 +620,12 @@ def _use_posix_spawn(): Prefer an implementation which can use vfork() in some cases for best performance. """ - if _mswindows or not hasattr(os, 'posix_spawn'): - # os.posix_spawn() is not available + if _mswindows or not hasattr(os, 'posix_spawnp'): + # os.posix_spawnp() is not available return False if sys.platform == 'darwin': - # posix_spawn() is a syscall on macOS and properly reports errors + # posix_spawnp() is a syscall on macOS and properly reports errors return True # Check libc name and runtime libc version @@ -640,8 +640,6 @@ def _use_posix_spawn(): version = tuple(map(int, parts[1].split('.'))) if sys.platform == 'linux' and libc == 'glibc' and version >= (2, 24): - # glibc 2.24 has a new Linux posix_spawn implementation using vfork - # which properly reports errors to the parent process. return True # Note: Don't use the implementation in earlier glibc because it doesn't # use vfork (even if glibc 2.26 added a pipe to properly report errors @@ -650,11 +648,11 @@ def _use_posix_spawn(): # os.confstr() or CS_GNU_LIBC_VERSION value not available pass - # By default, assume that posix_spawn() does not properly report errors. + # By default, assume that posix_spawnp() does not properly report errors. return False -_USE_POSIX_SPAWN = _use_posix_spawn() +_USE_POSIX_SPAWNP = _use_posix_spawnp() class Popen(object): @@ -1461,11 +1459,11 @@ def _get_handles(self, stdin, stdout, stderr): errread, errwrite) - def _posix_spawn(self, args, executable, env, restore_signals, + def _posix_spawnp(self, args, executable, env, restore_signals, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite): - """Execute program using os.posix_spawn().""" + """Execute program using os.posix_spawnp().""" if env is None: env = os.environ @@ -1493,7 +1491,7 @@ def _posix_spawn(self, args, executable, env, restore_signals, if file_actions: kwargs['file_actions'] = file_actions - self.pid = os.posix_spawn(executable, args, env, **kwargs) + self.pid = os.posix_spawnp(executable, args, env, **kwargs) self._child_created = True self._close_pipe_fds(p2cread, p2cwrite, @@ -1525,7 +1523,7 @@ def _execute_child(self, args, executable, preexec_fn, close_fds, if executable is None: executable = args[0] - if (_USE_POSIX_SPAWN + if (_USE_POSIX_SPAWNP and os.path.dirname(executable) and preexec_fn is None and not close_fds @@ -1535,7 +1533,7 @@ def _execute_child(self, args, executable, preexec_fn, close_fds, and (c2pwrite == -1 or c2pwrite > 2) and (errwrite == -1 or errwrite > 2) and not start_new_session): - self._posix_spawn(args, executable, env, restore_signals, + self._posix_spawnp(args, executable, env, restore_signals, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite) From 6309240dbcc0fd7c470f80dff42ab7a5af68276f Mon Sep 17 00:00:00 2001 From: nanjekyejoannah Date: Mon, 18 Feb 2019 16:40:51 +0300 Subject: [PATCH 2/5] change subprocess to use posix_spawnp instead of posix_spawn --- Lib/subprocess.py | 1 - .../NEWS.d/next/Library/2019-02-18-12-45-33.bpo-35537.xshtGJ.rst | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2019-02-18-12-45-33.bpo-35537.xshtGJ.rst diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 5107b056e050a9..9a1dfb6788def0 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -1524,7 +1524,6 @@ def _execute_child(self, args, executable, preexec_fn, close_fds, executable = args[0] if (_USE_POSIX_SPAWNP - and os.path.dirname(executable) and preexec_fn is None and not close_fds and not pass_fds diff --git a/Misc/NEWS.d/next/Library/2019-02-18-12-45-33.bpo-35537.xshtGJ.rst b/Misc/NEWS.d/next/Library/2019-02-18-12-45-33.bpo-35537.xshtGJ.rst new file mode 100644 index 00000000000000..75b327d9c45c3e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-02-18-12-45-33.bpo-35537.xshtGJ.rst @@ -0,0 +1 @@ +The :mod:`subprocess` now uses :func:`os.posix_spawnp` instead of :func:`os.posix_spawn` \ No newline at end of file From ed39610599d318855e1998fdaa66cb90d53604fd Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com> Date: Mon, 18 Feb 2019 16:49:09 +0300 Subject: [PATCH 3/5] update docs --- Doc/whatsnew/3.8.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 6bc131419ca87c..b493a3960c3222 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -317,14 +317,13 @@ xml Optimizations ============= -* The :mod:`subprocess` module can now use the :func:`os.posix_spawn` function +* The :mod:`subprocess` module can now use the :func:`os.posix_spawnp` function in some cases for better performance. Currently, it is only used on macOS and Linux (using glibc 2.24 or newer) if all these conditions are met: * *close_fds* is false; * *preexec_fn*, *pass_fds*, *cwd* and *start_new_session* parameters are not set; - * the *executable* path contains a directory. * :func:`shutil.copyfile`, :func:`shutil.copy`, :func:`shutil.copy2`, :func:`shutil.copytree` and :func:`shutil.move` use platform-specific From 001699dca5b44a702f5f96a3d4afd2a00eb9b0a1 Mon Sep 17 00:00:00 2001 From: nanjekyejoannah Date: Tue, 19 Feb 2019 09:21:58 +0300 Subject: [PATCH 4/5] fix typo and indetation --- Lib/subprocess.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 9a1dfb6788def0..97766b262e068d 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -1532,10 +1532,8 @@ def _execute_child(self, args, executable, preexec_fn, close_fds, and (c2pwrite == -1 or c2pwrite > 2) and (errwrite == -1 or errwrite > 2) and not start_new_session): - self._posix_spawnp(args, executable, env, restore_signals, - p2cread, p2cwrite, - c2pread, c2pwrite, - errread, errwrite) + self._posix_spawnp(args, executable, env, restore_signals, p2cread, + p2cwrite, c2pread, c2pwrite, errread, errwrite) return orig_executable = executable From 9af6006170ccba04519e68bfa4bc08138e0d0811 Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com> Date: Tue, 19 Feb 2019 09:25:51 +0300 Subject: [PATCH 5/5] Fix typo. --- Doc/whatsnew/3.8.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index b493a3960c3222..9814c808eb883f 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -323,7 +323,7 @@ Optimizations * *close_fds* is false; * *preexec_fn*, *pass_fds*, *cwd* and *start_new_session* parameters - are not set; + are not set. * :func:`shutil.copyfile`, :func:`shutil.copy`, :func:`shutil.copy2`, :func:`shutil.copytree` and :func:`shutil.move` use platform-specific