Skip to content

Commit 655e555

Browse files
committed
Add support for os.POSIX_SPAWN_CHDIR and posix_spawn_file_actions_addchdir_np
1 parent 9c1aefc commit 655e555

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

Lib/subprocess.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ def _use_posix_spawn():
749749
# These are primarily fail-safe knobs for negatives. A True value does not
750750
# guarantee the given libc/syscall API will be used.
751751
_USE_POSIX_SPAWN = _use_posix_spawn()
752+
_HAVE_POSIX_SPAWN_CHDIR = hasattr(os, 'POSIX_SPAWN_CHDIR')
752753
_HAVE_POSIX_SPAWN_CLOSEFROM = hasattr(os, 'POSIX_SPAWN_CLOSEFROM')
753754

754755

@@ -1757,7 +1758,7 @@ def _get_handles(self, stdin, stdout, stderr):
17571758
errread, errwrite)
17581759

17591760

1760-
def _posix_spawn(self, args, executable, env, restore_signals, close_fds,
1761+
def _posix_spawn(self, args, executable, env, restore_signals, close_fds, cwd,
17611762
p2cread, p2cwrite,
17621763
c2pread, c2pwrite,
17631764
errread, errwrite):
@@ -1784,6 +1785,9 @@ def _posix_spawn(self, args, executable, env, restore_signals, close_fds,
17841785
if fd != -1:
17851786
file_actions.append((os.POSIX_SPAWN_DUP2, fd, fd2))
17861787

1788+
if cwd:
1789+
file_actions.append((os.POSIX_SPAWN_CHDIR, cwd))
1790+
17871791
if close_fds:
17881792
file_actions.append((os.POSIX_SPAWN_CLOSEFROM, 3))
17891793

@@ -1836,7 +1840,7 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
18361840
and preexec_fn is None
18371841
and (not close_fds or _HAVE_POSIX_SPAWN_CLOSEFROM)
18381842
and not pass_fds
1839-
and cwd is None
1843+
and (cwd is None or _HAVE_POSIX_SPAWN_CHDIR)
18401844
and (p2cread == -1 or p2cread > 2)
18411845
and (c2pwrite == -1 or c2pwrite > 2)
18421846
and (errwrite == -1 or errwrite > 2)
@@ -1846,7 +1850,8 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
18461850
and gids is None
18471851
and uid is None
18481852
and umask < 0):
1849-
self._posix_spawn(args, executable, env, restore_signals, close_fds,
1853+
self._posix_spawn(args, executable, env, restore_signals,
1854+
close_fds, cwd,
18501855
p2cread, p2cwrite,
18511856
c2pread, c2pwrite,
18521857
errread, errwrite)

Modules/posixmodule.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7127,6 +7127,9 @@ enum posix_spawn_file_actions_identifier {
71277127
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP
71287128
,POSIX_SPAWN_CLOSEFROM
71297129
#endif
7130+
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NP
7131+
,POSIX_SPAWN_CHDIR
7132+
#endif
71307133
};
71317134

71327135
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
@@ -7384,6 +7387,25 @@ parse_file_actions(PyObject *file_actions,
73847387
}
73857388
break;
73867389
}
7390+
#endif
7391+
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NP
7392+
case POSIX_SPAWN_CHDIR: {
7393+
PyObject *path;
7394+
if (!PyArg_ParseTuple(file_action, "OO&"
7395+
";A chdir file_action tuple must have 2 elements",
7396+
&tag_obj, PyUnicode_FSConverter, &path))
7397+
{
7398+
goto fail;
7399+
}
7400+
errno = posix_spawn_file_actions_addchdir_np(file_actionsp,
7401+
PyBytes_AS_STRING(path));
7402+
if (errno) {
7403+
posix_error();
7404+
Py_DECREF(path);
7405+
goto fail;
7406+
}
7407+
Py_DECREF(path);
7408+
break;
73877409
#endif
73887410
default: {
73897411
PyErr_SetString(PyExc_TypeError,
@@ -17576,6 +17598,9 @@ all_ins(PyObject *m)
1757617598
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP
1757717599
if (PyModule_AddIntMacro(m, POSIX_SPAWN_CLOSEFROM)) return -1;
1757817600
#endif
17601+
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NP
17602+
if (PyModule_AddIntMacro(m, POSIX_SPAWN_CHDIR)) return -1;
17603+
#endif
1757917604
#endif
1758017605

1758117606
#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)

0 commit comments

Comments
 (0)