Skip to content

Commit 98825bf

Browse files
committed
Add support for os.POSIX_SPAWN_CHDIR and posix_spawn_file_actions_addchdir_np
1 parent 90d71a3 commit 98825bf

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

Lib/subprocess.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ def _can_use_kqueue():
806806
# These are primarily fail-safe knobs for negatives. A True value does not
807807
# guarantee the given libc/syscall API will be used.
808808
_USE_POSIX_SPAWN = _use_posix_spawn()
809+
_HAVE_POSIX_SPAWN_CHDIR = hasattr(os, 'POSIX_SPAWN_CHDIR')
809810
_HAVE_POSIX_SPAWN_CLOSEFROM = hasattr(os, 'POSIX_SPAWN_CLOSEFROM')
810811

811812

@@ -1836,7 +1837,7 @@ def _get_handles(self, stdin, stdout, stderr):
18361837
errread, errwrite)
18371838

18381839

1839-
def _posix_spawn(self, args, executable, env, restore_signals, close_fds,
1840+
def _posix_spawn(self, args, executable, env, restore_signals, close_fds, cwd,
18401841
p2cread, p2cwrite,
18411842
c2pread, c2pwrite,
18421843
errread, errwrite):
@@ -1863,6 +1864,9 @@ def _posix_spawn(self, args, executable, env, restore_signals, close_fds,
18631864
if fd != -1:
18641865
file_actions.append((os.POSIX_SPAWN_DUP2, fd, fd2))
18651866

1867+
if cwd:
1868+
file_actions.append((os.POSIX_SPAWN_CHDIR, cwd))
1869+
18661870
if close_fds:
18671871
file_actions.append((os.POSIX_SPAWN_CLOSEFROM, 3))
18681872

@@ -1915,7 +1919,7 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
19151919
and preexec_fn is None
19161920
and (not close_fds or _HAVE_POSIX_SPAWN_CLOSEFROM)
19171921
and not pass_fds
1918-
and cwd is None
1922+
and (cwd is None or _HAVE_POSIX_SPAWN_CHDIR)
19191923
and (p2cread == -1 or p2cread > 2)
19201924
and (c2pwrite == -1 or c2pwrite > 2)
19211925
and (errwrite == -1 or errwrite > 2)
@@ -1925,7 +1929,8 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
19251929
and gids is None
19261930
and uid is None
19271931
and umask < 0):
1928-
self._posix_spawn(args, executable, env, restore_signals, close_fds,
1932+
self._posix_spawn(args, executable, env, restore_signals,
1933+
close_fds, cwd,
19291934
p2cread, p2cwrite,
19301935
c2pread, c2pwrite,
19311936
errread, errwrite)

Modules/posixmodule.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7606,6 +7606,9 @@ enum posix_spawn_file_actions_identifier {
76067606
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP
76077607
,POSIX_SPAWN_CLOSEFROM
76087608
#endif
7609+
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NP
7610+
,POSIX_SPAWN_CHDIR
7611+
#endif
76097612
};
76107613

76117614
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
@@ -7864,6 +7867,25 @@ parse_file_actions(PyObject *file_actions,
78647867
}
78657868
break;
78667869
}
7870+
#endif
7871+
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NP
7872+
case POSIX_SPAWN_CHDIR: {
7873+
PyObject *path;
7874+
if (!PyArg_ParseTuple(file_action, "OO&"
7875+
";A chdir file_action tuple must have 2 elements",
7876+
&tag_obj, PyUnicode_FSConverter, &path))
7877+
{
7878+
goto fail;
7879+
}
7880+
errno = posix_spawn_file_actions_addchdir_np(file_actionsp,
7881+
PyBytes_AS_STRING(path));
7882+
if (errno) {
7883+
posix_error();
7884+
Py_DECREF(path);
7885+
goto fail;
7886+
}
7887+
Py_DECREF(path);
7888+
break;
78677889
#endif
78687890
default: {
78697891
PyErr_SetString(PyExc_TypeError,
@@ -18150,6 +18172,9 @@ all_ins(PyObject *m)
1815018172
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP
1815118173
if (PyModule_AddIntMacro(m, POSIX_SPAWN_CLOSEFROM)) return -1;
1815218174
#endif
18175+
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NP
18176+
if (PyModule_AddIntMacro(m, POSIX_SPAWN_CHDIR)) return -1;
18177+
#endif
1815318178
#endif
1815418179

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

0 commit comments

Comments
 (0)