question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Broken when piped to.

See original GitHub issue

When piping to python script that uses prompt() everything breaks:

File "/home/user/lib/python3.6/site-packages/prompt_toolkit/input.py", line 67, in __init__
    assert self.stdin.isatty()
AssertionError

To reproduce:

#test.py
from prompt_toolkit import prompt
text = prompt('> ')

Then:

$ echo "foo" | python test.py
<...>
File "/home/user/lib/python3.6/site-packages/prompt_toolkit/input.py", line 67, in __init__
    assert self.stdin.isatty()
AssertionError

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
jonathanslenderscommented, Apr 17, 2017

Yes, a Posix pipe is not the same as a PTY (pseudo terminal). Both are I/O devices, but they have different properties.

  • For instance, for a pipe between two processes, you want to have a big buffer, because otherwise, you end up switching between these process all the time. For user interaction, you want immediate feedback, so no buffering at all.
  • Further, prompt_toolkit will discard the input I/O when too much was read from stdin and the input gets accepted (enter is pressed). You will loose this input data if you would pipe certain input. For user interaction, this doesn’t matter, because after pressing “enter”, the user waits to see the feedback. (The reason this works that way, is because it’s the only way to make processing pasted text on stdin efficient. - There is no way to read until a “newline” character, and there is also no way to push stdin, back into the input device. And reading one character at a time during paste events is slow.)
  • A pipe does not respond to a CPR (cursor position request). There is actually no cursor.
  • A pipe does not have raw or canonical input mode.

Probably there are other differences. There is a lot of non-obvious low level I/O underneath. But in any case, prompt_toolkit won’t support pipes as stdin. Maybe with some hacks it will work partly, but seriously, it’ll take only a few lines of code to read stdin yourself, if the input doesn’t appear to be a tty.

if not sys.stdin.isatty():
    data = sys.stdin.read()
    ...
2reactions
jonathanslenderscommented, Apr 16, 2017

Hi @Granitosaurus,

Thanks for reporting the issue. This is however something which won’t be fixed. (I guess I definitely need to given better error message as feedback.)

The reason is that prompt_toolkit is meant for user interaction, not for machine-interaction like a pipe. If you want to support pipe input on the other hand, it’s best to test for sys.stdin.isatty() yourself, and if that is true, then use sys.stdin.read/readline to retrieve the input.

The same is true for piping the output to something else. If you’d pipe stdout to a file, the result will be a meaningless sequence of ANSI escape characters. If sys.stdout.isatty() is true, don’t use prompt_toolkit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What causes the Broken Pipe Error? - Stack Overflow
The error condition is detected at some point. With a small write, you are inside the MTU of the system, so the message...
Read more >
Broken Pipe Error in Python - GeeksforGeeks
A broken Pipe Error is generally an Input/Output Error, which is occurred at the Linux System level. The error has occurred during the...
Read more >
9 Things You Should Do When You Find A Burst Or Broken Pipe
1. Turn off the Water · 2. Cut the Electricity · 3. Remove Valuables From the Room · 4. Call a Professional ·...
Read more >
How to fix Broken Pipe Error in Linux - net2
Broken pipe occurs when a process prematurely exits from either end and the other process has not yet closed the pipe. Example use...
Read more >
How to Repair a Broken Pipe in Your Home
1. Locate the shutoff valve to your water main and close the valve. 2. The burst pipe could be a number of places...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found