Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tanty mannequin opened this issue May 17, 2021 · 2 comments · Fixed by #94035
Closed

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

tanty mannequin opened this issue May 17, 2021 · 2 comments · Fixed by #94035
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir easy type-bug An unexpected behavior, bug, or error

Comments

@tanty
Copy link
Mannequin

tanty mannequin commented May 17, 2021

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']

    @tanty
    Copy link
    Mannequin Author

    tanty mannequin commented May 17, 2021

    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

    @tanty tanty mannequin added stdlib Python modules in the Lib dir 3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes type-bug An unexpected behavior, bug, or error labels May 17, 2021
    @iritkatriel iritkatriel added docs Documentation in the Doc dir 3.10 only security fixes 3.11 only security fixes and removed stdlib Python modules in the Lib dir 3.7 (EOL) end of life 3.8 (EOL) end of life labels Jan 15, 2022
    @iritkatriel iritkatriel added 3.11 only security fixes and removed 3.7 (EOL) end of life 3.8 (EOL) end of life labels Jan 15, 2022
    @slateny
    Copy link
    Contributor

    slateny commented Apr 5, 2022

    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?

    gpshead pushed a commit that referenced this issue Jan 20, 2023
    …ng subprocess in Linux (#94035)
    
    * Update description of stdout, stderr, and stdin.
    
    Changes:
    - Move the ``None`` option (which is default) to the front of the list
      of input options
    - Move the ``None`` option description up to make the default behavior
      more clear (No redirection)
    - Remove mention of Child File Descriptors from ``None`` option description
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir easy type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    2 participants