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

BrokenPipeError: [Errno 32] Broken pipe #302

Open
jiasli opened this issue Mar 11, 2021 · 3 comments
Open

BrokenPipeError: [Errno 32] Broken pipe #302

jiasli opened this issue Mar 11, 2021 · 3 comments

Comments

@jiasli
Copy link

jiasli commented Mar 11, 2021

Originally reported at Azure/azure-cli#13413

Colorama fails if output is redirected to head command.

test.py:

import sys
from colorama import init, Fore, Style

init()
for i in range(30):
    print(Fore.RED + 'line{}'.format(i) + Style.RESET_ALL)

Output:

$ python test.py | head --line=1
line0
Traceback (most recent call last):
  File "test.py", line 6, in <module>
    print(Fore.RED + 'line{}'.format(i) + Style.RESET_ALL)
  File "/home/user1/env-test/lib/python3.8/site-packages/colorama/ansitowin32.py", line 41, in write
    self.__convertor.write(text)
  File "/home/user1/env-test/lib/python3.8/site-packages/colorama/ansitowin32.py", line 162, in write
    self.write_and_convert(text)
  File "/home/user1/env-test/lib/python3.8/site-packages/colorama/ansitowin32.py", line 190, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "/home/user1/env-test/lib/python3.8/site-packages/colorama/ansitowin32.py", line 196, in write_plain_text
    self.wrapped.flush()
BrokenPipeError: [Errno 32] Broken pipe
Exception ignored in: <colorama.ansitowin32.StreamWrapper object at 0x7f5fc3a1efd0>
BrokenPipeError: [Errno 32] Broken pipe
@tartley
Copy link
Owner

tartley commented Oct 17, 2022

Thanks for this old bug report. In the described scenario (piping output from colorama into 'head', it seems like the correct behavior would be to catch this exception and silently exit with value 0.

Are there cases where that is not the correct behavior on catching a BrokenPipeError?

@tartley
Copy link
Owner

tartley commented Oct 17, 2022

@njsmith
Copy link
Collaborator

njsmith commented Oct 17, 2022

I don't see how this has anything to do with colorama? The exception is being raised by Python itself; colorama only shows up in the traceback because the output is going through it. There's probably some subtle difference in when buffered output gets flushed depending on whether colorama is enabled or not, that happens to tickle the issue in this case, but the underlying issue is independent.

Anyway with the new just_fix_windows_console API, colorama won't touch stdout when it's a pipe, so switching to that should take care of this in any case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants