Skip to content

input() doesn't print the prompt if stderr is redirected #92715

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

Open
DavidNemeskey opened this issue May 12, 2022 · 4 comments
Open

input() doesn't print the prompt if stderr is redirected #92715

DavidNemeskey opened this issue May 12, 2022 · 4 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@DavidNemeskey
Copy link

Bug report

If stderr is redirected, input() doesn't print the prompt. The same happens when stdout is redirected, but that is to be expected.

Code (in test.py):

x = input('prompt: ')
print(x)

When run:

> python test.py
prompt: text
text

> python test.py >out
text

> python test.py 2>err
text
text

The contents of out and err, respectively:

# out
prompt: text
# err
prompt: 

The first and second cases are as expected. In the last case, the prompt is not printed, even though it should go into stdout, according to the documentation. Instead, it is printed to err.

Your environment

@DavidNemeskey DavidNemeskey added the type-bug An unexpected behavior, bug, or error label May 12, 2022
@DavidNemeskey DavidNemeskey changed the title input() doesn't print anything if stderr is redirected input() doesn't print the prompt if stderr is redirected May 12, 2022
@sweeneyde
Copy link
Member

@DavidNemeskey
Copy link
Author

I checked the first issue superficially, and I am not really sure it is a duplicate. Eryk's answer in the thread referenced in the penultimate comment, on the other hand, might explain what I have experienced (out is not a TTY, while the terminal is).

So in a sense this might be duplicate (of the thread at least), but the second last comment on that is from 6 years ago, so I don't know if that issue is considered being "alive".

@headst1337
Copy link

To fix this you can import readline
For example:

ipanov@archlinux ~/u/sandbox > cat test_stderr.py

input_msg = input('Enter text: ')
print(input_msg)

ipanov@archlinux ~/u/sandbox > python test_stderr.py 2>> out.txt
123
123
ipanov@archlinux ~/u/sandbox >

and with fix:

ipanov@archlinux ~/u/sandbox > cat test_stderr.py

import readline

input_msg = input('Enter text: ')
print(input_msg)

ipanov@archlinux ~/u/sandbox > python test_stderr.py 2>> out.txt
Enter text: 123
123
ipanov@archlinux ~/u/sandbox >

@DavidNemeskey
Copy link
Author

@headst1337 Thank you; it's good to know that readline fixes the problem. However, I think the default behavior is wrong so this is more like a workaround. Also, I couldn't find any mention of this effect in the documentation either, so that should be updated as well, depending on how this ticket is resolved.

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

No branches or pull requests

3 participants