This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ydroneaud
Recipients ydroneaud
Date 2022-03-15.14:55:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1647356154.7.0.125333178117.issue47027@roundup.psfhosted.org>
In-reply-to
Content
subprocess.run() and subprocess.Popen() accepts a cwd= parameter to change directory before running the subprocess.

Unfortunately it's not possible to use a file descriptor to run the subprocess in a directory already opened.

For example:

    import os
    import subprocess
    with os.open('/usr/bin', os.O_RDONLY) as f:
        subprocess.run(["ls", "-l"], cwd=f, check=True)

fails with

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.9/subprocess.py", line 505, in run
        with Popen(*popenargs, **kwargs) as process:
      File "/usr/lib/python3.9/subprocess.py", line 951, in __init__
        self._execute_child(args, executable, preexec_fn, close_fds,
      File "/usr/lib/python3.9/subprocess.py", line 1754, in _execute_child
        self.pid = _posixsubprocess.fork_exec(

Using a file descriptor instead of path is useful to address TOCTOU issues.

Maybe a mean to convert a file descriptor to a Path-like object would do the trick.
History
Date User Action Args
2022-03-15 14:55:54ydroneaudsetrecipients: + ydroneaud
2022-03-15 14:55:54ydroneaudsetmessageid: <1647356154.7.0.125333178117.issue47027@roundup.psfhosted.org>
2022-03-15 14:55:54ydroneaudlinkissue47027 messages
2022-03-15 14:55:54ydroneaudcreate