diff options
author | Georg Brandl <georg@python.org> | 2020-02-29 15:45:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-29 15:45:08 +0100 |
commit | 35544e2fc6eed0ce4a27ec7285aac71ff0ddc473 (patch) | |
tree | 4390507bf0d4d5a4596cfc57c575da12f9da40f9 /pygments/util.py | |
parent | 14fc057d300102d88a07eda5558f238d49dd23f6 (diff) | |
download | pygments-git-35544e2fc6eed0ce4a27ec7285aac71ff0ddc473.tar.gz |
Remove Python 2 compatibility (#1348)
* Remove Python 2 compatibility
* remove 2/3 shims in pygments.util
* update setup.py metadata
* Remove unneeded object inheritance.
* Remove unneeded future imports.
Diffstat (limited to 'pygments/util.py')
-rw-r--r-- | pygments/util.py | 67 |
1 files changed, 15 insertions, 52 deletions
diff --git a/pygments/util.py b/pygments/util.py index 59669d2b..92e4f259 100644 --- a/pygments/util.py +++ b/pygments/util.py @@ -11,6 +11,7 @@ import re import sys +from io import TextIOWrapper split_path_re = re.compile(r'[/\\ ]') @@ -53,7 +54,7 @@ def get_bool_opt(options, optname, default=None): return string elif isinstance(string, int): return bool(string) - elif not isinstance(string, string_types): + elif not isinstance(string, str): raise OptionError('Invalid type %r for option %s; use ' '1/0, yes/no, true/false, on/off' % ( string, optname)) @@ -83,7 +84,7 @@ def get_int_opt(options, optname, default=None): def get_list_opt(options, optname, default=None): val = options.get(optname, default) - if isinstance(val, string_types): + if isinstance(val, str): return val.split() elif isinstance(val, (list, tuple)): return list(val) @@ -224,7 +225,7 @@ def unirange(a, b): if sys.maxunicode > 0xffff: # wide build - return u'[%s-%s]' % (unichr(a), unichr(b)) + return u'[%s-%s]' % (chr(a), chr(b)) else: # narrow build stores surrogates, and the 're' module handles them # (incorrectly) as characters. Since there is still ordering among @@ -232,24 +233,23 @@ def unirange(a, b): # background in http://bugs.python.org/issue3665 and # http://bugs.python.org/issue12749 # - # Additionally, the lower constants are using unichr rather than + # Additionally, the lower constants are using chr rather than # literals because jython [which uses the wide path] can't load this # file if they are literals. ah, al = _surrogatepair(a) bh, bl = _surrogatepair(b) if ah == bh: - return u'(?:%s[%s-%s])' % (unichr(ah), unichr(al), unichr(bl)) + return u'(?:%s[%s-%s])' % (chr(ah), chr(al), chr(bl)) else: buf = [] - buf.append(u'%s[%s-%s]' % - (unichr(ah), unichr(al), - ah == bh and unichr(bl) or unichr(0xdfff))) + buf.append(u'%s[%s-%s]' % (chr(ah), chr(al), + ah == bh and chr(bl) or chr(0xdfff))) if ah - bh > 1: buf.append(u'[%s-%s][%s-%s]' % - unichr(ah+1), unichr(bh-1), unichr(0xdc00), unichr(0xdfff)) + chr(ah+1), chr(bh-1), chr(0xdc00), chr(0xdfff)) if ah != bh: buf.append(u'%s[%s-%s]' % - (unichr(bh), unichr(0xdc00), unichr(bl))) + (chr(bh), chr(0xdc00), chr(bl))) return u'(?:' + u'|'.join(buf) + u')' @@ -289,7 +289,7 @@ def duplicates_removed(it, already_seen=()): return lst -class Future(object): +class Future: """Generic class to defer some work. Handled specially in RegexLexerMeta, to support regex string construction at @@ -345,44 +345,7 @@ def terminal_encoding(term): return locale.getpreferredencoding() -# Python 2/3 compatibility - -if sys.version_info < (3, 0): - unichr = unichr - xrange = xrange - string_types = (str, unicode) - text_type = unicode - u_prefix = 'u' - iteritems = dict.iteritems - itervalues = dict.itervalues - import StringIO - import cStringIO - # unfortunately, io.StringIO in Python 2 doesn't accept str at all - StringIO = StringIO.StringIO - BytesIO = cStringIO.StringIO -else: - unichr = chr - xrange = range - string_types = (str,) - text_type = str - u_prefix = '' - iteritems = dict.items - itervalues = dict.values - from io import StringIO, BytesIO, TextIOWrapper - - class UnclosingTextIOWrapper(TextIOWrapper): - # Don't close underlying buffer on destruction. - def close(self): - self.flush() - - -def add_metaclass(metaclass): - """Class decorator for creating a class with a metaclass.""" - def wrapper(cls): - orig_vars = cls.__dict__.copy() - orig_vars.pop('__dict__', None) - orig_vars.pop('__weakref__', None) - for slots_var in orig_vars.get('__slots__', ()): - orig_vars.pop(slots_var) - return metaclass(cls.__name__, cls.__bases__, orig_vars) - return wrapper +class UnclosingTextIOWrapper(TextIOWrapper): + # Don't close underlying buffer on destruction. + def close(self): + self.flush() |