Skip to content

subprocess.Popen does not handle file-like objects without file descriptors #74175

Closed

Description

@rtpg
mannequin
BPO 29989
Nosy @terryjreedy, @vadmium, @MojoVampire, @zhangyangyu, @rtpg
Superseder
  • bpo-19992: subprocess documentation not explicit about fileno()
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-04-10.04:33:38.646>
    created_at = <Date 2017-04-05.02:38:53.726>
    labels = ['type-bug', 'invalid']
    title = 'subprocess.Popen does not handle file-like objects without file descriptors'
    updated_at = <Date 2017-04-10.07:20:19.185>
    user = 'https://github.com/rtpg'

    bugs.python.org fields:

    activity = <Date 2017-04-10.07:20:19.185>
    actor = 'terry.reedy'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-04-10.04:33:38.646>
    closer = 'rtpg'
    components = []
    creation = <Date 2017-04-05.02:38:53.726>
    creator = 'rtpg'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 29989
    keywords = []
    message_count = 8.0
    messages = ['291151', '291217', '291220', '291296', '291308', '291403', '291407', '291409']
    nosy_count = 5.0
    nosy_names = ['terry.reedy', 'martin.panter', 'josh.r', 'xiang.zhang', 'rtpg']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = '19992'
    type = 'behavior'
    url = 'https://bugs.python.org/issue29989'
    versions = ['Python 3.6']

    Activity

    1. rtpg commented on Apr 5, 2017

      rtpgmannequin
      MannequinAuthor

      From the documentation of the io module:

      fileno()
      Return the underlying file descriptor (an integer) of the stream if it exists. An OSError is raised if the IO object does not use a file descriptor.

      However, when passing a file-like object without a file descriptor (that raises OSError when calling f.fileno()) to POpen (for stdout, for example), the raised exception is not handled properly.

      (However, on inspection of subprocess code, returning -1 will cause the code to handle this properly)

      I'm not sure whether this is an issue in the io module documentation or in the subprocess code.

      the core issue seems to be in POpen.get_handles, that seems to expect that -1 is used to signal "no file descriptor available".

    2. added
      type-crash
      on Apr 5, 2017
    3. zhangyangyu commented on Apr 6, 2017

      @zhangyangyu
      Member

      Hmm, what do you mean by 'the raised exception is not handled properly'? Currently the exception is just propagated to the user to signal the argument seems to be wrong, right?

    4. rtpg commented on Apr 6, 2017

      rtpgmannequin
      MannequinAuthor

      the subprocess module has code to handle file objects that don't use file descriptors. It's not that file descriptors are necessary for the parameter, but that the way POpen is trying to detect "no file descriptor support" is through "fileno returns -1" rather than "fileno raises OSError"

    5. terryjreedy commented on Apr 7, 2017

      @terryjreedy
      Member

      'crash' means OS message rather than Python exiting with exception traceback and message.

      Can you post a minimal reproducer? What OS? subprocess.Popen._get_handles is different on POSIX and windows, though both seem to call f.fileno() without try-except.

      All filenoes are initialized to -1, so it seems to me that either
      a. all accesses should be wrapped with try-except: pass, or
      b. subprocess doc should say that file-like objects must include a fileno method returning -1.

      I am puzzled though. The 2.7 doc for (builtin)file.fileno() says
      "
      Note
      File-like objects which do not have a real file descriptor should not provide this method! "
      "
      Rather than return -1

      In must be that the subprocess test does not test with a 'file-like object without a file descriptor'

    6. added
      type-bug
      and removed
      type-crash
      on Apr 7, 2017
    7. vadmium commented on Apr 8, 2017

      @vadmium
      Member

      Raphael: Can you point to the implementation code that handles file objects without a file descriptor (or give a demonstration of it)? I suspect there is no such support and you are mistaken.

      Perhaps we can instead clarify in the “subprocess” documentation that “fileno” is required. bpo-19992 already proposes this.

      bpo-1260171 and bpo-10482 are also related, about streaming data between Python file objects in the parent and pipes connected to the child.

    8. rtpg commented on Apr 10, 2017

      rtpgmannequin
      MannequinAuthor

      I'm sorry for the confusion here, it turns out I was misinterpreting the results of my program and my workaround was not working. Like others have said, the subprocess code seems to rely on the existence of file descriptors

      I would be pretty partial to the idea of having the subprocess's internal code catch OSError and raise a clearer error message like "subprocess can only operate on files with file descriptor support".

      Having "complete" file object support would seem like a major win, but looking at the internals/OS support it seems like a hard thing to set up.

    9. vadmium commented on Apr 10, 2017

      @vadmium
      Member

      The current exception seems to give a reasonable hint:

      >>> subprocess.Popen((executable,), stdout=BytesIO())
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/usr/lib/python3.5/subprocess.py", line 914, in __init__
          errread, errwrite) = self._get_handles(stdin, stdout, stderr)
        File "/usr/lib/python3.5/subprocess.py", line 1400, in _get_handles
          c2pwrite = stdout.fileno()
      io.UnsupportedOperation: fileno

      Perhaps we can close this in favour of fixing the documentation? (bpo-19992)

    10. rtpg commented on Apr 10, 2017

      rtpgmannequin
      MannequinAuthor

      that error is what is raised by StringIO (subclassing OSError), other file-like objects could just be raising OSError (according to the file object spec described in the documentation for the io module).

      Fixing documentation would already be helpful though. I will close this issue

    11. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      type-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions