nghung270192 / colorama

Automatically exported from code.google.com/p/colorama
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Spurious ANSI codes in non tty wrapped output. #18

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

With the following script (test.py):

----
from colorama import *
print Fore.red + 'test'
----

Do:

\> python test.py > output.txt

What is the expected output? What do you see instead?

The content of output.txt should be:

----
test
----

But it contains:

----
test
\x1b[0m
----

Of course, "\x1b" means the escape character/byte.

What version of the product are you using? On what operating system?

colorama v 0.1.18

Please provide any additional information below.

The problem seems to be that the "reset_all" call made "atexit" does not checks 
if we're in a tty.

Possible solution:

    def reset_all(self):
        if self.convert:
            self.call_win32('m', (0,))
-        else:
+        elif is_a_tty(self.wrapped):
            self.wrapped.write(Style.RESET_ALL)

Original issue reported on code.google.com by oscar.le...@gmail.com on 7 Oct 2010 at 3:49

GoogleCodeExporter commented 9 years ago
Confirmed, it's a real bug, thanks very much for the report.

I'll aim to fix this soon.

Original comment by tart...@gmail.com on 15 Oct 2010 at 2:03

GoogleCodeExporter commented 9 years ago
Hmm, the fix doesn't seem adequate to me.
I also use colorama 0.1.18, on Arch Linux 2.6.38 x86_64, python 2.7.1
Oscar did not mention which file should be changed, but the string 
'self.wrapped.write(Style.RESET_ALL)' only appeared in one location, so I 
edited reset_all(self) in 
/usr/lib/python2.7/site-packages/colorama/ansitowin32.py with the suggested 
change.
I also removed the corresponding .pyo and .pyc file.

I run my python program without attaching it to a terminal, and it still 
contains bunches of color codes.
Example code:
from colorama import Fore, Back, Style, init
init()
print Back.GREEN + Fore.BLUE + str + Style.RESET_ALL

When I run this as python2 test.py (stdout == tty) it emits the sequences, 
hence i see the colors.
However, when I run it as python2 test.py | cat - (stdout is a pipe)
or: python2 test.py > somefile.txt
it still emits the sequences, which is not what it should do.

Any idea when a new colorama release will include the proper fix?

Original comment by dplaeti...@intec.ugent.be on 20 Apr 2011 at 9:57

GoogleCodeExporter commented 9 years ago
Hey. Acknowledged, and thanks for the comment. What you say makes sense. There 
isn't a schedule, but I have a couple of long weekends coming up - I'll make 
some time for this then.

Original comment by tart...@gmail.com on 20 Apr 2011 at 10:08

GoogleCodeExporter commented 9 years ago
hmm why did google not inform me about your followup.. 
anyway, looking forward to that change, have a nice day.

Dieter

Original comment by dplaeti...@intec.ugent.be on 9 May 2011 at 2:22

GoogleCodeExporter commented 9 years ago
Patch submitted by Oscar Lester was applied by Daniel Griffith. Nice work guys!

Original comment by tart...@gmail.com on 19 Jun 2011 at 6:47

GoogleCodeExporter commented 9 years ago

Original comment by tart...@gmail.com on 19 Jun 2011 at 6:47

GoogleCodeExporter commented 9 years ago
Great! Thanks for accepting the "patch". My last name is "Lesta" actually, but 
Lester sounds cooler :-P.

Take care, and thanks for colorama!

Oscar.

Original comment by oscar.le...@gmail.com on 24 Jun 2011 at 8:51

GoogleCodeExporter commented 9 years ago
Oops! Spelling fixed. :-)

Original comment by tart...@gmail.com on 25 Jun 2011 at 10:27