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
Should init() be wrapping stdout on Linux? #209
Comments
Aha, I at least see why my "strange result" was happening - I'm writing encoded binary to file, and must have passed the magic combination of bytes that colorama interprets as a request to change the window title. Also, I admit it's odd to be importing colorama in this situation; it's being eagerly init'ed by a dependency-of-a-dependency. |
I'm curious about this behavior on Linux as well. I was making a script with an option to force colored output when writing to a non-TTY, and happened to import Structlog which in turn calls colorama.init() That caused all my ANSI-codes to get stripped no matter what and wasted a few hours before I discovered my sys.stdout was being overwritten when piping/redirecting output. Is this really intended? |
@gwtwod Wrapping stdout on Linux when redirection is enabled is indeed intended. On Windows we strip ANSI codes from redirected output because ANSI codes won't work on that output, so we also do that for Linux - and many times, it is a desired behavior. If it is not, it's possible to pass @DanielFEvans the problem with |
I pushed a fix to my personal forked repository - wiggin15@4832ef9 |
@wiggin15 I am working on adding aarch64 support in colorama, following two test cases are failing in my local environment:
Please shed some light on this, what could possibaly be the reason for the test failures. |
@ossdev07 I think you should open a separate ticket for this problem. Also, please share the command you are running (is it |
Hello, I think that when we call Ok, I can set UPDATE 1:actually, UPDATE 2:Currently I init colorama like this:
But please, make it possible to do without dirty hacks. For example, it may look like this:
|
@barabanus I guess you can manually wrap from colorama import AnsiToWin32
sys.stdout = AnsiToWin32(sys.stdout, convert=True, strip=False, autoreset=False).stream |
Thank you for your advice, but obviously, it's too wordy. Also, it's not like I care at all about windows users to add specific wrapper. So I would prefer something like |
When using colorama in a non-interactive terminal (e.g. stdout being piped to file) on Linux, colorama.init() is hooking into sys.stdout, resulting in occasional strange results.
Looking at the documentation, there are a few lines that state:
"Colorama makes this work on Windows, too, by wrapping stdout [...] On other platforms, Colorama does nothing."
Example
colorama_test.py
script:If I run
python colorama_test.py
in a bash terminal, colorama makes no change to sys.stdout:However, if I run
python colorama_test.py > out.txt
, sys.stdout is wrapped:Should this be happening?
For completeness, the "occasional strange results" I see occur when writing to stdout via
stdout.write
; partial traceback is:The text was updated successfully, but these errors were encountered: