[CentralOH] Popen file descriptor difference between Python 2.6 and 2.7+ on Linux

Thomas Winningham winningham at gmail.com
Sat Feb 17 00:38:50 EST 2018


I was replying on Twitter, but I suspect the behavior slowly became
undefined somewhere between 2.7 and 3 ... but after 3.2 i can do this and
it works:
------------------------
from subprocess import Popen, PIPE

data = Popen(
    ["cat"],
    stdin=open('/tmp/test', 'rb'),
    stdout=PIPE,
    stderr=PIPE,
    universal_newlines=True,
    shell=False).stdout

fd_name = '/dev/fd/%d' % data.fileno()

fddata = Popen(
    ["cat",fd_name],
    stdout=PIPE,
    universal_newlines=True,
    pass_fds=(data.fileno(),)).stdout

print(fddata.read())
------------------------
the "pass_fds" option was added in 3.2 per the subprocess documentation


On Fri, Feb 16, 2018 at 11:32 PM, Neil Ludban <nludban at columbus.rr.com>
wrote:

> On Fri, 16 Feb 2018 22:34:35 -0500
> Eric Floehr <eric at intellovations.com> wrote:
> > I have a really odd problem. I have some code that requires a Linux file
> > descriptor (/dev/fd/N) where N is some number. The following example code
> > demonstrates effectively what is being done.
> >
> > In order to run the code create a file called "/tmp/test" with some text
> in
> > it.
> >
> > The code works in Python 2.6, but in Python 2.7 or later, I get:
> >
> > cat: /dev/fd/4: No such file or directory
>
> Smells like a back-porting of adding the O_CLOEXEC flag...
>
>      Unless O_CLOEXEC flag was specified, the new descriptor is set to
> remain
>      open across execve(2) system calls; see close(2), fcntl(2) and
> O_CLOEXEC
>      description.
>
> https://www.python.org/dev/peps/pep-0446/
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20180217/01016d88/attachment-0001.html>


More information about the CentralOH mailing list