Commits

Georg Brandl committed c728eb7

Closes #691: Fix Python 3 terminal highlighting with pygmentize.

  • Participants
  • Parent commits c96d75f

Comments (0)

Files changed (4)

   * Bro (PR#5)
   * NewLISP (PR#26)
 
+- Fix Python 3 terminal highlighting with pygmentize (#691).
+
 - In the LaTeX formatter, escape special &, < and > chars (#648).
 
 - In the LaTeX formatter, fix display problems for styles with token

File pygments/cmdline.py

                                          None) or 'ascii'
                 fmter.encoding = getattr(sys.stdout, 'encoding',
                                          None) or 'ascii'
+    elif not outfn and sys.version_info > (3,):
+        # output to terminal with encoding -> use .buffer
+        outfile = sys.stdout.buffer
 
     # ... and do it!
     try:

File pygments/formatters/terminal.py

     :license: BSD, see LICENSE for details.
 """
 
+import sys
+
 from pygments.formatter import Formatter
 from pygments.token import Keyword, Name, Comment, String, Error, \
      Number, Operator, Generic, Token, Whitespace
         # hack: if the output is a terminal and has an encoding set,
         # use that to avoid unicode encode problems
         if not self.encoding and hasattr(outfile, "encoding") and \
-           hasattr(outfile, "isatty") and outfile.isatty():
+           hasattr(outfile, "isatty") and outfile.isatty() and \
+           sys.version_info < (3,):
             self.encoding = outfile.encoding
         return Formatter.format(self, tokensource, outfile)
 

File pygments/formatters/terminal256.py

 #    black-on-while, so colors like "white background" need to be converted
 #    to "white background, black foreground", etc...
 
+import sys
+
 from pygments.formatter import Formatter
 
 
         # hack: if the output is a terminal and has an encoding set,
         # use that to avoid unicode encode problems
         if not self.encoding and hasattr(outfile, "encoding") and \
-           hasattr(outfile, "isatty") and outfile.isatty():
+           hasattr(outfile, "isatty") and outfile.isatty() and \
+           sys.version_info < (3,):
             self.encoding = outfile.encoding
         return Formatter.format(self, tokensource, outfile)