Skip to content
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

subprocess.Popen doesn't redirect child process' stderr to stdout #126848

Open
yurivict opened this issue Nov 14, 2024 · 1 comment
Open

subprocess.Popen doesn't redirect child process' stderr to stdout #126848

yurivict opened this issue Nov 14, 2024 · 1 comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@yurivict
Copy link

yurivict commented Nov 14, 2024

Bug report

Bug description:

Testcase:

import time
import subprocess

with open('log', 'w') as log:

    p = subprocess.Popen(['python', 'sp-child.py'],
                         stdout=log,
                         stderr=subprocess.STDOUT)
    time.sleep(10)
    p.communicate()

Additional file sp-child.py:

import sys

print('I am a child!')
print(f' ... in-terminal: stdout={sys.stdout.isatty()} stderr={sys.stderr.isatty()}')

The subprocess.Popen argument requests child's stderr to be redirected to stdout and this doesn't happen.

The child process prints:

I am a child!
 ... in-terminal: stdout=False stderr=False

CPython versions tested on:

3.11

Operating systems tested on:

Linux

@yurivict yurivict added the type-bug An unexpected behavior, bug, or error label Nov 14, 2024
@picnixz picnixz added the stdlib Python modules in the Lib dir label Nov 14, 2024
@brianschubert
Copy link
Contributor

Hi yurivict, could you clarify what you are expecting to happen, and how that differs from what actually happens?

Are you perhaps expecting stderr=subprocess.STDOUT to cause the child process' stderr to go the parent process' stdout (and therefore sys.stderr.isatty() would be True, not False)? If so, that's not quite what that means. stderr=subprocess.STDOUT means that the child process' stderr will be redirected to the same handle as its stdout, similar to using 2>&1 in a shell. In this case, that means that both stdout and stderr will be directed to the log file. If you want the child process' stderr to be directed to the parent process' stdout, I believe you can pass stderr=sys.stdout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants