Skip to content

Clarify documentation for redirected stdout/stderr when using subprocess in Linux #88324

Closed

Description

@tanty
mannequin
BPO 44158
Nosy @tanty, @slateny
Files
  • test.py: Example test
  • 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 = None
    created_at = <Date 2021-05-17.19:46:00.282>
    labels = ['easy', 'type-bug', '3.9', '3.10', '3.11', 'docs']
    title = 'Clarify documentation for redirected stdout/stderr when using subprocess in Linux'
    updated_at = <Date 2022-04-05.09:31:13.894>
    user = 'https://github.com/tanty'

    bugs.python.org fields:

    activity = <Date 2022-04-05.09:31:13.894>
    actor = 'slateny'
    assignee = 'docs@python'
    closed = False
    closed_date = None
    closer = None
    components = ['Documentation']
    creation = <Date 2021-05-17.19:46:00.282>
    creator = 'tanty'
    dependencies = []
    files = ['50048']
    hgrepos = []
    issue_num = 44158
    keywords = ['easy']
    message_count = 2.0
    messages = ['393825', '416764']
    nosy_count = 3.0
    nosy_names = ['docs@python', 'tanty', 'slateny']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue44158'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    Activity

    1. tanty commented on May 17, 2021

      tantymannequin
      MannequinAuthor
      1. Redirect the stderr and stdout with contextlib's redirect_stderr, redirect_stdout or just by replacing the fd in sys.stderr and sys.stdout
      2. We run a subprocess with subprocess.run()
      3. The documentation for subprocess states:

      https://docs.python.org/3/library/subprocess.html#frequently-used-arguments

      "
      With the default settings of None, no redirection will occur; the child’s file handles will be inherited from the parent.
      "

      1. The documentation for contextlib states:

      https://docs.python.org/3/library/contextlib.html?highlight=contextlib#contextlib.redirect_stdout

      "
      Note that the global side effect on sys.stdout means that this context manager is not suitable for use in library code and most threaded applications. It also has no effect on the output of subprocesses.
      "
      5. The stdout and stderr for the subprocess-ed command is not redirected, as stated in the contextlib documentation.

      It took me a while to realize that redirecting stdout and stderr didn't have an effect in the child subprocess.

      It would be great if this could be improved so the subproccess-ed command also inherits the redirection. At the very least, this issue with the redirection should be added as a notice in the subprocess documentation.

      The attached test.py script shows this behavior. The code is very simple and self-explanatory. You can run with:

      # test.py

      or

      # test.py -n

      Somehow, related to: https://bugs.python.org/issue1227748

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on May 17, 2021
    3. added
      docsDocumentation in the Doc dir
      3.11only security fixes
      and removed
      stdlibStandard Library Python modules in the Lib/ directory
      on Jan 15, 2022
    4. 1 remaining item

    5. added and removed
      stdlibStandard Library Python modules in the Lib/ directory
      on Jan 15, 2022
    6. added
      3.11only security fixes
      and removed on Jan 15, 2022
    7. slateny commented on Apr 5, 2022

      @slateny
      Contributor

      Hmm, I'm not quite following - when you say that the subproccess-ed command should also inherit the redirection, which subprocess command are you referring to, and nested or unnested? I gave it a ran and everything seems to run how I'd expected it, so maybe put another way, what's the expected output from the files and stdout/stderr?

    8. transferred this issue fromon Apr 10, 2022
    9. added a commit that references this issue on Jan 20, 2023
    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

      3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifedocsDocumentation in the Doc direasytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions