22
22
.. _Pygments tip:
23
23
http://bitbucket.org/birkenfeld/pygments-main/get/tip.zip#egg=Pygments-dev
24
24
25
- :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
25
+ :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
26
26
:license: BSD, see LICENSE for details.
27
27
"""
28
+ import sys
29
+
30
+ from pygments .util import StringIO , BytesIO
28
31
29
- __version__ = '1.6 '
32
+ __version__ = '2.2.0 '
30
33
__docformat__ = 'restructuredtext'
31
34
32
35
__all__ = ['lex' , 'format' , 'highlight' ]
33
36
34
37
35
- import sys
36
-
37
- from pygments .util import StringIO , BytesIO
38
-
39
-
40
38
def lex (code , lexer ):
41
39
"""
42
40
Lex ``code`` with ``lexer`` and return an iterable of tokens.
43
41
"""
44
42
try :
45
43
return lexer .get_tokens (code )
46
- except TypeError , err :
47
- if isinstance (err .args [0 ], str ) and \
48
- 'unbound method get_tokens' in err .args [0 ]:
44
+ except TypeError as err :
45
+ if (isinstance (err .args [0 ], str ) and
46
+ ('unbound method get_tokens' in err .args [0 ] or
47
+ 'missing 1 required positional argument' in err .args [0 ])):
49
48
raise TypeError ('lex() argument must be a lexer instance, '
50
49
'not a class' )
51
50
raise
52
51
53
52
54
- def format (tokens , formatter , outfile = None ):
53
+ def format (tokens , formatter , outfile = None ): # pylint: disable=redefined-builtin
55
54
"""
56
55
Format a tokenlist ``tokens`` with the formatter ``formatter``.
57
56
@@ -61,15 +60,15 @@ def format(tokens, formatter, outfile=None):
61
60
"""
62
61
try :
63
62
if not outfile :
64
- #print formatter, 'using', formatter.encoding
65
- realoutfile = formatter .encoding and BytesIO () or StringIO ()
63
+ realoutfile = getattr (formatter , 'encoding' , None ) and BytesIO () or StringIO ()
66
64
formatter .format (tokens , realoutfile )
67
65
return realoutfile .getvalue ()
68
66
else :
69
67
formatter .format (tokens , outfile )
70
- except TypeError , err :
71
- if isinstance (err .args [0 ], str ) and \
72
- 'unbound method format' in err .args [0 ]:
68
+ except TypeError as err :
69
+ if (isinstance (err .args [0 ], str ) and
70
+ ('unbound method format' in err .args [0 ] or
71
+ 'missing 1 required positional argument' in err .args [0 ])):
73
72
raise TypeError ('format() argument must be a formatter instance, '
74
73
'not a class' )
75
74
raise
@@ -86,6 +85,6 @@ def highlight(code, lexer, formatter, outfile=None):
86
85
return format (lex (code , lexer ), formatter , outfile )
87
86
88
87
89
- if __name__ == '__main__' :
88
+ if __name__ == '__main__' : # pragma: no cover
90
89
from pygments .cmdline import main
91
90
sys .exit (main (sys .argv ))
0 commit comments