summaryrefslogtreecommitdiff
path: root/pygments/formatters/other.py
diff options
context:
space:
mode:
authorgbrandl <devnull@localhost>2009-03-31 10:26:55 -0500
committergbrandl <devnull@localhost>2009-03-31 10:26:55 -0500
commit7b95efab48d9ec79e995bf4d6db10fd049e3395a (patch)
tree48fc9b840dab83976a85af85d1c705dbaa051a0b /pygments/formatters/other.py
parentf12c878ed096137c91658a0f62f0070e08c2afea (diff)
downloadpygments-7b95efab48d9ec79e995bf4d6db10fd049e3395a.tar.gz
Port Pygments to Python 3.1.
Diffstat (limited to 'pygments/formatters/other.py')
-rw-r--r--pygments/formatters/other.py21
1 files changed, 16 insertions, 5 deletions
diff --git a/pygments/formatters/other.py b/pygments/formatters/other.py
index 1ddba006..4b195244 100644
--- a/pygments/formatters/other.py
+++ b/pygments/formatters/other.py
@@ -10,7 +10,7 @@
"""
from pygments.formatter import Formatter
-from pygments.util import get_choice_opt
+from pygments.util import OptionError, get_choice_opt, b
from pygments.token import Token
from pygments.console import colorize
@@ -42,7 +42,7 @@ class RawTokenFormatter(Formatter):
be converted to a token stream with the `RawTokenLexer`, described in the
`lexer list <lexers.txt>`_.
- Only one option is accepted:
+ Only two options are accepted:
`compress`
If set to ``'gz'`` or ``'bz2'``, compress the output with the given
@@ -61,6 +61,10 @@ class RawTokenFormatter(Formatter):
def __init__(self, **options):
Formatter.__init__(self, **options)
+ if self.encoding:
+ raise OptionError('the raw formatter does not support the '
+ 'encoding option')
+ self.encoding = 'ascii' # let pygments.format() do the right thing
self.compress = get_choice_opt(options, 'compress',
['', 'none', 'gz', 'bz2'], '')
self.error_color = options.get('error_color', None)
@@ -74,21 +78,28 @@ class RawTokenFormatter(Formatter):
self.error_color)
def format(self, tokensource, outfile):
+ try:
+ outfile.write(b(''))
+ except TypeError:
+ raise TypeError('The raw tokens formatter needs a binary '
+ 'output file')
if self.compress == 'gz':
import gzip
outfile = gzip.GzipFile('', 'wb', 9, outfile)
- write = outfile.write
+ def write(text):
+ outfile.write(text.encode())
flush = outfile.flush
elif self.compress == 'bz2':
import bz2
compressor = bz2.BZ2Compressor(9)
def write(text):
- outfile.write(compressor.compress(text))
+ outfile.write(compressor.compress(text.encode()))
def flush():
outfile.write(compressor.flush())
outfile.flush()
else:
- write = outfile.write
+ def write(text):
+ outfile.write(text.encode())
flush = outfile.flush
lasttype = None