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