Skip to content

Commit

Permalink
Remove Python 2 compatibility (#1348)
Browse files Browse the repository at this point in the history
* Remove Python 2 compatibility

* remove 2/3 shims in pygments.util
* update setup.py metadata

* Remove unneeded object inheritance.

* Remove unneeded future imports.
  • Loading branch information
birkenfeld committed Feb 29, 2020
1 parent 14fc057 commit 35544e2
Show file tree
Hide file tree
Showing 53 changed files with 128 additions and 260 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
max-parallel: 4
matrix:
python-version: [2.7, 3.5, 3.6, 3.7, 3.8, pypy2, pypy3]
python-version: [3.5, 3.6, 3.7, 3.8, pypy3]

steps:
- uses: actions/checkout@v1
Expand Down
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Version 2.6
-----------
(not released yet)

- Running Pygments on Python 2.x is no longer supported.
(The Python 2 lexer still exists.)

- Added lexers:

* Linux kernel logs (PR#1310)
Expand Down
3 changes: 1 addition & 2 deletions pygments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
:license: BSD, see LICENSE for details.
"""
import sys

from pygments.util import StringIO, BytesIO
from io import StringIO, BytesIO

__version__ = '2.5.2'
__docformat__ = 'restructuredtext'
Expand Down
29 changes: 7 additions & 22 deletions pygments/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
:license: BSD, see LICENSE for details.
"""

from __future__ import print_function

import os
import sys
import getopt
from textwrap import dedent

from pygments import __version__, highlight
from pygments.util import ClassNotFound, OptionError, docstring_headline, \
guess_decode, guess_decode_from_terminal, terminal_encoding
guess_decode, guess_decode_from_terminal, terminal_encoding, \
UnclosingTextIOWrapper
from pygments.lexers import get_all_lexers, get_lexer_by_name, guess_lexer, \
load_lexer_from_file, get_lexer_for_filename, find_lexer_class_for_filename
from pygments.lexers.special import TextLexer
Expand Down Expand Up @@ -397,11 +396,7 @@ def main_inner(popts, args, usage):
elif '-s' not in opts: # treat stdin as full file (-s support is later)
# read code from terminal, always in binary mode since we want to
# decode ourselves and be tolerant with it
if sys.version_info > (3,):
# Python 3: we have to use .buffer to get a binary stream
code = sys.stdin.buffer.read()
else:
code = sys.stdin.read()
code = sys.stdin.buffer.read() # use .buffer to get a binary stream
if not inencoding:
code, inencoding = guess_decode_from_terminal(code, sys.stdin)
# else the lexer will do the decoding
Expand Down Expand Up @@ -466,11 +461,7 @@ def main_inner(popts, args, usage):
fmter = Terminal256Formatter(**parsed_opts)
else:
fmter = TerminalFormatter(**parsed_opts)
if sys.version_info > (3,):
# Python 3: we have to use .buffer to get a binary stream
outfile = sys.stdout.buffer
else:
outfile = sys.stdout
outfile = sys.stdout.buffer

# determine output encoding if not explicitly selected
if not outencoding:
Expand All @@ -485,10 +476,8 @@ def main_inner(popts, args, usage):
if not outfn and sys.platform in ('win32', 'cygwin') and \
fmter.name in ('Terminal', 'Terminal256'): # pragma: no cover
# unfortunately colorama doesn't support binary streams on Py3
if sys.version_info > (3,):
from pygments.util import UnclosingTextIOWrapper
outfile = UnclosingTextIOWrapper(outfile, encoding=fmter.encoding)
fmter.encoding = None
outfile = UnclosingTextIOWrapper(outfile, encoding=fmter.encoding)
fmter.encoding = None
try:
import colorama.initialise
except ImportError:
Expand All @@ -515,11 +504,7 @@ def main_inner(popts, args, usage):
# line by line processing of stdin (eg: for 'tail -f')...
try:
while 1:
if sys.version_info > (3,):
# Python 3: we have to use .buffer to get a binary stream
line = sys.stdin.buffer.readline()
else:
line = sys.stdin.readline()
line = sys.stdin.buffer.readline()
if not line:
break
if not inencoding:
Expand Down
2 changes: 1 addition & 1 deletion pygments/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def lowercase(self, lexer, stream, options):
})


class Filter(object):
class Filter:
"""
Default filter. Subclass this class or use the `simplefilter`
decorator to create own filters.
Expand Down
9 changes: 5 additions & 4 deletions pygments/filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
string_to_tokentype
from pygments.filter import Filter
from pygments.util import get_list_opt, get_int_opt, get_bool_opt, \
get_choice_opt, ClassNotFound, OptionError, text_type, string_types
get_choice_opt, ClassNotFound, OptionError
from pygments.plugin import find_plugin_filters


Expand Down Expand Up @@ -113,7 +113,7 @@ def __init__(self, **options):
Filter.__init__(self, **options)
case = get_choice_opt(options, 'case',
['lower', 'upper', 'capitalize'], 'lower')
self.convert = getattr(text_type, case)
self.convert = getattr(str, case)

def filter(self, lexer, stream):
for ttype, value in stream:
Expand Down Expand Up @@ -233,7 +233,7 @@ def __init__(self, **options):
('tabs', u'»'),
('newlines', u'¶')]:
opt = options.get(name, False)
if isinstance(opt, string_types) and len(opt) == 1:
if isinstance(opt, str) and len(opt) == 1:
setattr(self, name, opt)
else:
setattr(self, name, (opt and default or ''))
Expand All @@ -250,6 +250,7 @@ def filter(self, lexer, stream):
tabs = self.tabs or u'\t'
newlines = self.newlines or u'\n'
regex = re.compile(r'\s')

def replacefunc(wschar):
if wschar == ' ':
return spaces
Expand Down Expand Up @@ -302,7 +303,7 @@ def gobble(self, value, left):

def filter(self, lexer, stream):
n = self.n
left = n # How many characters left to gobble.
left = n # How many characters left to gobble.
for ttype, value in stream:
# Remove ``left`` tokens from first line, ``n`` from all others.
parts = value.split('\n')
Expand Down
6 changes: 3 additions & 3 deletions pygments/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@

import codecs

from pygments.util import get_bool_opt, string_types
from pygments.util import get_bool_opt
from pygments.styles import get_style_by_name

__all__ = ['Formatter']


def _lookup_style(style):
if isinstance(style, string_types):
if isinstance(style, str):
return get_style_by_name(style)
return style


class Formatter(object):
class Formatter:
"""
Converts a token stream to text.
Expand Down
8 changes: 4 additions & 4 deletions pygments/formatters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from pygments.formatters._mapping import FORMATTERS
from pygments.plugin import find_plugin_formatters
from pygments.util import ClassNotFound, itervalues
from pygments.util import ClassNotFound

__all__ = ['get_formatter_by_name', 'get_formatter_for_filename',
'get_all_formatters', 'load_formatter_from_file'] + list(FORMATTERS)
Expand Down Expand Up @@ -45,7 +45,7 @@ def _load_formatters(module_name):
def get_all_formatters():
"""Return a generator for all formatter classes."""
# NB: this returns formatter classes, not info like get_all_lexers().
for info in itervalues(FORMATTERS):
for info in FORMATTERS.values():
if info[1] not in _formatter_cache:
_load_formatters(info[0])
yield _formatter_cache[info[1]]
Expand All @@ -58,7 +58,7 @@ def find_formatter_class(alias):
Returns None if not found.
"""
for module_name, name, aliases, _, _ in itervalues(FORMATTERS):
for module_name, name, aliases, _, _ in FORMATTERS.values():
if alias in aliases:
if name not in _formatter_cache:
_load_formatters(module_name)
Expand Down Expand Up @@ -121,7 +121,7 @@ def get_formatter_for_filename(fn, **options):
Raises ClassNotFound if not found.
"""
fn = basename(fn)
for modname, name, _, filenames, _ in itervalues(FORMATTERS):
for modname, name, _, filenames, _ in FORMATTERS.values():
for filename in filenames:
if _fn_matches(fn, filename):
if name not in _formatter_cache:
Expand Down
2 changes: 0 additions & 2 deletions pygments/formatters/_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
:license: BSD, see LICENSE for details.
"""

from __future__ import print_function

FORMATTERS = {
'BBCodeFormatter': ('pygments.formatters.bbcode', 'BBCode', ('bbcode', 'bb'), (), 'Format tokens with BBcodes. These formatting codes are used by many bulletin boards, so you can highlight your sourcecode with pygments before posting it there.'),
'BmpImageFormatter': ('pygments.formatters.img', 'img_bmp', ('bmp', 'bitmap'), ('*.bmp',), 'Create a bitmap image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.'),
Expand Down
12 changes: 6 additions & 6 deletions pygments/formatters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
:license: BSD, see LICENSE for details.
"""

from __future__ import print_function

import os
import sys
import os.path
from io import StringIO

from pygments.formatter import Formatter
from pygments.token import Token, Text, STANDARD_TYPES
from pygments.util import get_bool_opt, get_int_opt, get_list_opt, \
StringIO, string_types, iteritems
from pygments.util import get_bool_opt, get_int_opt, get_list_opt

try:
import ctags
Expand All @@ -41,12 +39,14 @@ def escape_html(text, table=_escape_html_table):
"""Escape &, <, > as well as single and double quotes for HTML."""
return text.translate(table)


def webify(color):
if color.startswith('calc') or color.startswith('var'):
return color
else:
return '#' + color


def _get_ttype_class(ttype):
fname = STANDARD_TYPES.get(ttype)
if fname:
Expand Down Expand Up @@ -497,7 +497,7 @@ def get_style_defs(self, arg=None):
"""
if arg is None:
arg = ('cssclass' in self.options and '.'+self.cssclass or '')
if isinstance(arg, string_types):
if isinstance(arg, str):
args = [arg]
else:
args = list(arg)
Expand All @@ -511,7 +511,7 @@ def prefix(cls):
return ', '.join(tmp)

styles = [(level, ttype, cls, style)
for cls, (style, ttype, level) in iteritems(self.class2style)
for cls, (style, ttype, level) in self.class2style.items()
if cls and style]
styles.sort()
lines = ['%s { %s } /* %s */' % (prefix(cls), style, repr(ttype)[6:])
Expand Down
6 changes: 3 additions & 3 deletions pygments/formatters/img.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from pygments.formatter import Formatter
from pygments.util import get_bool_opt, get_int_opt, get_list_opt, \
get_choice_opt, xrange
get_choice_opt

import subprocess

Expand Down Expand Up @@ -59,7 +59,7 @@ class FontNotFound(Exception):
"""When there are no usable fonts specified"""


class FontManager(object):
class FontManager:
"""
Manages a set of fonts: normal, italic, bold, etc...
"""
Expand Down Expand Up @@ -504,7 +504,7 @@ def _draw_line_numbers(self):
"""
if not self.line_numbers:
return
for p in xrange(self.maxlineno):
for p in range(self.maxlineno):
n = p + self.line_number_start
if (n % self.line_number_step) == 0:
self._draw_linenumber(p, n)
Expand Down
9 changes: 4 additions & 5 deletions pygments/formatters/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
:license: BSD, see LICENSE for details.
"""

from __future__ import division
from io import StringIO

from pygments.formatter import Formatter
from pygments.lexer import Lexer
from pygments.token import Token, STANDARD_TYPES
from pygments.util import get_bool_opt, get_int_opt, StringIO, xrange, \
iteritems
from pygments.util import get_bool_opt, get_int_opt


__all__ = ['LatexFormatter']
Expand Down Expand Up @@ -322,7 +321,7 @@ def get_style_defs(self, arg=''):
"""
cp = self.commandprefix
styles = []
for name, definition in iteritems(self.cmd2def):
for name, definition in self.cmd2def.items():
styles.append(r'\expandafter\def\csname %s@tok@%s\endcsname{%s}' %
(cp, name, definition))
return STYLE_TEMPLATE % {'cp': self.commandprefix,
Expand Down Expand Up @@ -354,7 +353,7 @@ def format_unencoded(self, tokensource, outfile):
if self.texcomments:
# Try to guess comment starting lexeme and escape it ...
start = value[0:1]
for i in xrange(1, len(value)):
for i in range(1, len(value)):
if start[0] != value[i]:
break
start += value[i]
Expand Down
6 changes: 0 additions & 6 deletions pygments/formatters/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ def __init__(self, **options):
self._lineno = 0

def format(self, tokensource, outfile):
# 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() and \
sys.version_info < (3,):
self.encoding = outfile.encoding
return Formatter.format(self, tokensource, outfile)

def _write_lineno(self, outfile):
Expand Down
6 changes: 0 additions & 6 deletions pygments/formatters/terminal256.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,6 @@ def _setup_styles(self):
escape.reset_string())

def format(self, tokensource, outfile):
# 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() and \
sys.version_info < (3,):
self.encoding = outfile.encoding
return Formatter.format(self, tokensource, outfile)

def format_unencoded(self, tokensource, outfile):
Expand Down

0 comments on commit 35544e2

Please sign in to comment.