-
-
Notifications
You must be signed in to change notification settings - Fork 254
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
Windows: Style.RESET_ALL does not work if stdout is redirected. #200
Comments
Maybe this is in part caused by the fact that colorama's colorama/colorama/initialise.py Lines 18 to 20 in d7a5382
|
@cjerdonek I don't think this is it (although this could be a problem too, maybe). The code you mentioned is only called at program exit. This problem is caused by Lines 44 to 46 in d7a5382
Unfortunately the fix is not trivial, because there is an additional problem. The "_default" attribute (which we reset to) is taken from stdout explicitly: Lines 24 to 25 in d7a5382
when stdout redirection is on, the attributes are 0. Resetting to this value will cause stderr to become black. The problem here is that we use a single instance of WinTerm that holds the color and default attributes for both stdout and stderr, without correctly separating the two.
|
Thanks, @wiggin15.
Yes, this is why I hedged and said "in part caused by the fact..." My thinking was that if the "during program" fix would take a lot more work (which it sounds like it might), maybe there's a simpler fix that can at least restore things to the initial state on exit -- even if the state during the execution isn't fixed. That way people's terminal at least won't be corrupted "permanently," which is a more serious problem IMO than just the color being off during the running of the program. |
Note: when I said "permanently," I had in mind this issue-- |
I pushed a fix to my personal forked repository that separates the This change also causes a strange side-effect. Consider the following code: print(colorama.Fore.GREEN + 'stderr should be with green foreground and regular background', file=sys.stderr)
print(colorama.Back.GREEN + 'stdout should be with green background and regular foreground') In this case we don't use "reset" but print to two separate streams (stderr and stdout). My fix will separate the attributes of the two streams so each will get the correct colors - but from what I can see, Unix terminals will not separate the colors, and stdout will get the green foreground from stderr... I didn't expect this and we may need to look into this a bit. Maybe the streams shouldn't be separated like this. @cjerdonek Thanks for reporting the problem with resetting stderr on program exit. I think it would be a good idea to open a separate issue for that. |
My instinct is that people should be invoking colorama in a way that doesn't depend on stdout and stderr going to the same (or a different) place. The reason is that, for example, if someone redirects stdout or stderr to a different location, I don't think the program has any way to tell. In particular, if someone wants to reset stdout and stderr, it won't necessarily suffice only to reset stdout. A proper usage should reset both.
I created issue #218 for this. |
SeaHOH@966474a It works fine, except redirect to |
Coming to this issue from the Azure CLI. My team runs into this issue daily - seems like a lot of productivity lost for console colors. Is there a mitigation for this issue? I would be happy if I could disable colorama via environment variable or some function call at the start of my terminal session. Since az cli owns the dependency I'm not sure how much control I can take. |
Is there any hope of having this fixed in a release in the near future? It has been open for 4 years; it would be helpful to know if it is going to be fixed or not; either is fine, but depending on the answer I would either implement a workaround or switch to a different library. |
I guess that's a "no". |
The following code demonstrates the problem with stdout redirection on Windows.
Note that coloring is used on stderr only.
Try to run this code with and without stdout redirection and note the difference:
python foo.py
python foo.py >nul
In the first case the first line will be printed in green and the second - using the default cmd color, which is the expected behavior.
In the second case both lines will be printed in green.
This issue pretty much disallows using colors in logging because normally log messages go to stderr while stdout is used for program output, which sometimes may be redirected to a file.
The text was updated successfully, but these errors were encountered: