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