Georg Brandl avatar Georg Brandl committed c728eb7

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

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

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:

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)
 

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)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.