Skip to content

Commit 82d3c2f

Browse files
committed
Add support for os.POSIX_SPAWN_CHDIR and posix_spawn_file_actions_addchdir_np
1 parent 6fbbfc8 commit 82d3c2f

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
@@ -748,6 +748,7 @@ def _use_posix_spawn():
748748
# guarantee the given libc/syscall API will be used.
749749
_USE_POSIX_SPAWN = _use_posix_spawn()
750750
_USE_VFORK = True
751+
_HAVE_POSIX_SPAWN_CHDIR = hasattr(os, 'POSIX_SPAWN_CHDIR')
751752
_HAVE_POSIX_SPAWN_CLOSEFROM = hasattr(os, 'POSIX_SPAWN_CLOSEFROM')
752753

753754

@@ -1752,7 +1753,7 @@ def _get_handles(self, stdin, stdout, stderr):
17521753
errread, errwrite)
17531754

17541755

1755-
def _posix_spawn(self, args, executable, env, restore_signals, close_fds,
1756+
def _posix_spawn(self, args, executable, env, restore_signals, close_fds, cwd,
17561757
p2cread, p2cwrite,
17571758
c2pread, c2pwrite,
17581759
errread, errwrite):
@@ -1779,6 +1780,9 @@ def _posix_spawn(self, args, executable, env, restore_signals, close_fds,
17791780
if fd != -1:
17801781
file_actions.append((os.POSIX_SPAWN_DUP2, fd, fd2))
17811782

1783+
if cwd:
1784+
file_actions.append((os.POSIX_SPAWN_CHDIR, cwd))
1785+
17821786
if close_fds:
17831787
file_actions.append((os.POSIX_SPAWN_CLOSEFROM, 3))
17841788

@@ -1831,7 +1835,7 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
18311835
and preexec_fn is None
18321836
and (not close_fds or _HAVE_POSIX_SPAWN_CLOSEFROM)
18331837
and not pass_fds
1834-
and cwd is None
1838+
and (cwd is None or _HAVE_POSIX_SPAWN_CHDIR)
18351839
and (p2cread == -1 or p2cread > 2)
18361840
and (c2pwrite == -1 or c2pwrite > 2)
18371841
and (errwrite == -1 or errwrite > 2)
@@ -1841,7 +1845,8 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
18411845
and gids is None
18421846
and uid is None
18431847
and umask < 0):
1844-
self._posix_spawn(args, executable, env, restore_signals, close_fds,
1848+
self._posix_spawn(args, executable, env, restore_signals,
1849+
close_fds, cwd,
18451850
p2cread, p2cwrite,
18461851
c2pread, c2pwrite,
18471852
errread, errwrite)

Modules/posixmodule.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6875,6 +6875,9 @@ enum posix_spawn_file_actions_identifier {
68756875
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP
68766876
,POSIX_SPAWN_CLOSEFROM
68776877
#endif
6878+
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NP
6879+
,POSIX_SPAWN_CHDIR
6880+
#endif
68786881
};
68796882

68806883
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
@@ -7132,6 +7135,25 @@ parse_file_actions(PyObject *file_actions,
71327135
}
71337136
break;
71347137
}
7138+
#endif
7139+
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NP
7140+
case POSIX_SPAWN_CHDIR: {
7141+
PyObject *path;
7142+
if (!PyArg_ParseTuple(file_action, "OO&"
7143+
";A chdir file_action tuple must have 2 elements",
7144+
&tag_obj, PyUnicode_FSConverter, &path))
7145+
{
7146+
goto fail;
7147+
}
7148+
errno = posix_spawn_file_actions_addchdir_np(file_actionsp,
7149+
PyBytes_AS_STRING(path));
7150+
if (errno) {
7151+
posix_error();
7152+
Py_DECREF(path);
7153+
goto fail;
7154+
}
7155+
Py_DECREF(path);
7156+
break;
71357157
#endif
71367158
default: {
71377159
PyErr_SetString(PyExc_TypeError,
@@ -16844,6 +16866,9 @@ all_ins(PyObject *m)
1684416866
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP
1684516867
if (PyModule_AddIntMacro(m, POSIX_SPAWN_CLOSEFROM)) return -1;
1684616868
#endif
16869+
#ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR_NP
16870+
if (PyModule_AddIntMacro(m, POSIX_SPAWN_CHDIR)) return -1;
16871+
#endif
1684716872
#endif
1684816873

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

0 commit comments

Comments
 (0)