From 6726daccd78cc5960fd32f67710aeaffbc6ff935 Mon Sep 17 00:00:00 2001 From: nanjekyejoannah Date: Tue, 18 Dec 2018 22:38:17 +0300 Subject: [PATCH 1/4] use os.posix_spawn in subprocess --- Lib/subprocess.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 696617697047d2..b7f738eabf9d1c 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -1390,6 +1390,11 @@ def _get_handles(self, stdin, stdout, stderr): errread, errwrite) + def _posix_spawn(self, args, executable): + """Execute program using os.posix_spawn().""" + env = dict(os.environ) + self.pid = os.posix_spawn(executable, args, env) + def _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, @@ -1414,6 +1419,16 @@ def _execute_child(self, args, executable, preexec_fn, close_fds, if executable is None: executable = args[0] + + if (preexec_fn is None + and not close_fds + and not pass_fds + and cwd is None + and env is None + and restore_signals is False + and not start_new_session): + return self._posix_spawn(args, executable) + orig_executable = executable # For transferring possible exec failure from child to parent. From f7df9e8505172ca77513b18dcd2abe2aa4bdadb9 Mon Sep 17 00:00:00 2001 From: nanjekyejoannah Date: Thu, 20 Dec 2018 19:20:09 +0300 Subject: [PATCH 2/4] use os.posix_spawn in subprocess --- Lib/subprocess.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index b7f738eabf9d1c..fd87aaaf0daa3a 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -1392,7 +1392,7 @@ def _get_handles(self, stdin, stdout, stderr): def _posix_spawn(self, args, executable): """Execute program using os.posix_spawn().""" - env = dict(os.environ) + env = os.environ self.pid = os.posix_spawn(executable, args, env) def _execute_child(self, args, executable, preexec_fn, close_fds, @@ -1425,9 +1425,12 @@ def _execute_child(self, args, executable, preexec_fn, close_fds, and not pass_fds and cwd is None and env is None - and restore_signals is False - and not start_new_session): - return self._posix_spawn(args, executable) + and not restore_signals + and not start_new_session + and os.path.dirname(executable) + and self.stdin is self.stdout is self.stderr is None): + self._posix_spawn(args, executable) + return orig_executable = executable From c05a078a64a9f33d17f5b833927161e452c52c6f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" Date: Thu, 20 Dec 2018 16:24:51 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2018-12-20-16-24-51.bpo-35537.z4E7aA.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2018-12-20-16-24-51.bpo-35537.z4E7aA.rst diff --git a/Misc/NEWS.d/next/Library/2018-12-20-16-24-51.bpo-35537.z4E7aA.rst b/Misc/NEWS.d/next/Library/2018-12-20-16-24-51.bpo-35537.z4E7aA.rst new file mode 100644 index 00000000000000..e86c3ab7fd16ea --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-12-20-16-24-51.bpo-35537.z4E7aA.rst @@ -0,0 +1 @@ +subprocess.Popen can now use posix_spawn() in some cases. \ No newline at end of file From 1b9357ab98c23390ef714054834fcaf84dcbe28e Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 20 Dec 2018 22:06:43 +0100 Subject: [PATCH 4/4] Fix test deciding if posix_spawn() can be used Test (p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite) instead of (self.stdin, self.stdout, self.stderr). --- Lib/subprocess.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index fd87aaaf0daa3a..741cc4a5bb324f 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -1392,8 +1392,7 @@ def _get_handles(self, stdin, stdout, stderr): def _posix_spawn(self, args, executable): """Execute program using os.posix_spawn().""" - env = os.environ - self.pid = os.posix_spawn(executable, args, env) + self.pid = os.posix_spawn(executable, args, os.environ) def _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, @@ -1420,15 +1419,17 @@ def _execute_child(self, args, executable, preexec_fn, close_fds, if executable is None: executable = args[0] - if (preexec_fn is None + if (os.path.dirname(executable) + and preexec_fn is None and not close_fds and not pass_fds and cwd is None and env is None + and p2cread == p2cwrite == -1 + and c2pread == c2pwrite == -1 + and errread == errwrite == -1 and not restore_signals - and not start_new_session - and os.path.dirname(executable) - and self.stdin is self.stdout is self.stderr is None): + and not start_new_session): self._posix_spawn(args, executable) return