Skip to content

Commit 0785889

Browse files
authored
bpo-35537: subprocess can now use os.posix_spawnp (GH-11579)
The subprocess module can now use the os.posix_spawnp() function, if it is available, to locate the program in the PATH.
1 parent 92b8322 commit 0785889

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

Doc/whatsnew/3.8.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ Optimizations
281281

282282
* *close_fds* is false;
283283
* *preexec_fn*, *pass_fds*, *cwd*, *stdin*, *stdout*, *stderr* and
284-
*start_new_session* parameters are not set;
285-
* the *executable* path contains a directory.
284+
*start_new_session* parameters are not set.
286285

287286
* :func:`shutil.copyfile`, :func:`shutil.copy`, :func:`shutil.copy2`,
288287
:func:`shutil.copytree` and :func:`shutil.move` use platform-specific

Lib/subprocess.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ def _use_posix_spawn():
655655

656656

657657
_USE_POSIX_SPAWN = _use_posix_spawn()
658+
_HAVE_POSIX_SPAWNP = hasattr(os, 'posix_spawnp')
658659

659660

660661
class Popen(object):
@@ -1442,7 +1443,10 @@ def _get_handles(self, stdin, stdout, stderr):
14421443

14431444

14441445
def _posix_spawn(self, args, executable, env, restore_signals):
1445-
"""Execute program using os.posix_spawn()."""
1446+
"""Execute program using os.posix_spawnp().
1447+
1448+
Or use os.posix_spawn() if os.posix_spawnp() is not available.
1449+
"""
14461450
if env is None:
14471451
env = os.environ
14481452

@@ -1456,7 +1460,10 @@ def _posix_spawn(self, args, executable, env, restore_signals):
14561460
sigset.append(signum)
14571461
kwargs['setsigdef'] = sigset
14581462

1459-
self.pid = os.posix_spawn(executable, args, env, **kwargs)
1463+
if _HAVE_POSIX_SPAWNP:
1464+
self.pid = os.posix_spawnp(executable, args, env, **kwargs)
1465+
else:
1466+
self.pid = os.posix_spawn(executable, args, env, **kwargs)
14601467

14611468
def _execute_child(self, args, executable, preexec_fn, close_fds,
14621469
pass_fds, cwd, env,
@@ -1484,7 +1491,7 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
14841491
executable = args[0]
14851492

14861493
if (_USE_POSIX_SPAWN
1487-
and os.path.dirname(executable)
1494+
and (_HAVE_POSIX_SPAWNP or os.path.dirname(executable))
14881495
and preexec_fn is None
14891496
and not close_fds
14901497
and not pass_fds

Lib/test/pythoninfo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,8 @@ def collect_get_config(info_add):
612612

613613
def collect_subprocess(info_add):
614614
import subprocess
615-
copy_attributes(info_add, subprocess, 'subprocess.%s', ('_USE_POSIX_SPAWN',))
615+
attrs = ('_USE_POSIX_SPAWN', '_HAVE_POSIX_SPAWNP')
616+
copy_attributes(info_add, subprocess, 'subprocess.%s', attrs)
616617

617618

618619
def collect_info(info):

0 commit comments

Comments
 (0)