@@ -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 )
0 commit comments