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.

classification
Title: subprocess.Popen support for redirection of arbitrary file descriptors
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, r.david.murray, ropoctorix, vstinner
Priority: normal Keywords:

Created on 2013-07-24 14:00 by ropoctorix, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg193646 - (view) Author: Robert O'Callahan (ropoctorix) Date: 2013-07-24 14:00
Popen() ought to support redirection to/from more file descriptors than 0, 1, and 2 when spawning processes. A clear use case is here: http://stackoverflow.com/questions/6050187/write-to-file-descriptor-3-of-a-python-subprocess-popen-object

Instead of messing around with os.open() and os.close() calls, my proposed API for Popen would be to accept keyword arguments fdN that would take the same type of values as the stdin, stdout, stderr arguments. Conflicting fd0 and stdin arguments would throw an exception.

So the smelly code in the above SO question would be changed to:


cmd='gpg --passphrase-fd {fd} -c'.format(fd=fd)
with open('passphrase.txt','r') as fd3_fh:
    with open('filename.txt','r') as stdin_fh:
        with open('filename.gpg','w') as stdout_fh:        
            proc=subprocess.Popen(shlex.split(cmd),
                                  stdin=stdin_fh,
                                  stdout=stdout_fh,
                                  fd3=fd3_fh)
            proc.communicate()
msg195100 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-08-13 22:23
Why not using the pass_fds parameter for your use case?
msg222893 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-12 22:48
@Robert can we have a response to Victor's question please.
msg240395 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-04-09 21:50
I can't figure out what this code is doing that pass_fds doesn't cover, so I'm closing it.
History
Date User Action Args
2022-04-11 14:57:48adminsetgithub: 62744
2015-04-09 21:50:28r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg240395

resolution: works for me
stage: resolved
2014-07-12 22:48:45BreamoreBoysetnosy: + BreamoreBoy

messages: + msg222893
versions: + Python 3.5
2013-08-13 22:23:18vstinnersetnosy: + vstinner
messages: + msg195100
2013-07-24 14:00:00ropoctorixcreate