diff options
author | Georg Brandl <georg@python.org> | 2012-02-06 07:49:13 +0100 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2012-02-06 07:49:13 +0100 |
commit | 8ba7c3fd309800a353f517ce6c374b40c47ca028 (patch) | |
tree | 65e86326f13f601f4e4767c10fd2c9efd9ff6002 | |
parent | bb2f7a8bdfce72f688d1c673c087213b73f47447 (diff) | |
download | pygments-8ba7c3fd309800a353f517ce6c374b40c47ca028.tar.gz |
Closes #691: Fix Python 3 terminal highlighting with pygmentize.
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | pygments/cmdline.py | 3 | ||||
-rw-r--r-- | pygments/formatters/terminal.py | 5 | ||||
-rw-r--r-- | pygments/formatters/terminal256.py | 5 |
4 files changed, 13 insertions, 2 deletions
@@ -40,6 +40,8 @@ Version 1.5 * 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 diff --git a/pygments/cmdline.py b/pygments/cmdline.py index 4eece2f9..e5b9b9b2 100644 --- a/pygments/cmdline.py +++ b/pygments/cmdline.py @@ -408,6 +408,9 @@ def main(args=sys.argv): 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: diff --git a/pygments/formatters/terminal.py b/pygments/formatters/terminal.py index 43d03e60..dae00157 100644 --- a/pygments/formatters/terminal.py +++ b/pygments/formatters/terminal.py @@ -9,6 +9,8 @@ :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 @@ -86,7 +88,8 @@ class TerminalFormatter(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) diff --git a/pygments/formatters/terminal256.py b/pygments/formatters/terminal256.py index 3105a651..cbd30be1 100644 --- a/pygments/formatters/terminal256.py +++ b/pygments/formatters/terminal256.py @@ -24,6 +24,8 @@ # 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 @@ -185,7 +187,8 @@ class Terminal256Formatter(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) |