Skip to content

Commit

Permalink
Remove support for Python 2 (#101)
Browse files Browse the repository at this point in the history
* Remove Python 2 support

* Don't test Python 2

* Remove dead code after PY2 removal

*  Bump version to 4.0.dev0

* Prepare changelog for a 4.0.0 release
  • Loading branch information
facelessuser authored Dec 2, 2018
1 parent 63d90d8 commit 3255f04
Show file tree
Hide file tree
Showing 19 changed files with 287 additions and 599 deletions.
6 changes: 3 additions & 3 deletions .pyspelling.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ matrix:
- title
- alt
ignores:
- ':matches(code, pre)'
- 'a:matches(.magiclink-compare, .magiclink-commit, .magiclink-repository)'
- ':is(code, pre)'
- 'a:is(.magiclink-compare, .magiclink-commit, .magiclink-repository)'
- 'span.keys'
- ':matches(.MathJax_Preview, .md-nav__link, .md-footer-custom-text, .md-source__repository, .headerlink, .md-icon)'
- ':is(.MathJax_Preview, .md-nav__link, .md-footer-custom-text, .md-source__repository, .headerlink, .md-icon)'
- pyspelling.filters.url:

- name: markdown
Expand Down
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ language: python

matrix:
include:
- python: 2.7
env: TOXENV=py27
- python: 3.4
env: TOXENV=py34
- python: 3.5
Expand Down
2 changes: 0 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
build: false
environment:
matrix:
- PYTHON: "C:/Python27"
TOXENV: "py27"
- PYTHON: "C:/Python34"
TOXENV: "py34"
- PYTHON: "C:/Python35"
Expand Down
2 changes: 1 addition & 1 deletion backrefs/__meta__.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,5 @@ def parse_version(ver, pre=False):
return Version(major, minor, micro, release, pre, post, dev)


__version_info__ = Version(2, 0, 0, "final")
__version_info__ = Version(4, 0, 0, "final")
__version__ = __version_info__._get_canonical()
131 changes: 39 additions & 92 deletions backrefs/_bre_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
from __future__ import unicode_literals
import re as _re
import copyreg as _copyreg
from . import util as _util
import sre_parse as _sre_parse
import unicodedata as _unicodedata
Expand All @@ -32,10 +33,7 @@
_CURLY_BRACKETS = frozenset(('{', '}'))
_PROPERTY_STRIP = frozenset((' ', '-', '_'))
_PROPERTY = _WORD | _DIGIT | _PROPERTY_STRIP
if _util.PY3:
_GLOBAL_FLAGS = frozenset(('a', 'u', 'L'))
else:
_GLOBAL_FLAGS = frozenset(('u', 'L'))
_GLOBAL_FLAGS = frozenset(('a', 'u', 'L'))
_SCOPED_FLAGS = frozenset(('i', 'm', 's', 'u', 'x'))

_CURLY_BRACKETS_ORD = frozenset((0x7b, 0x7d))
Expand All @@ -56,7 +54,7 @@
"\\\\": '\\'
}

_FMT_CONV_TYPE = ('a', 'r', 's') if _util.PY3 else ('r', 's')
_FMT_CONV_TYPE = ('a', 'r', 's')


class LoopException(Exception):
Expand All @@ -81,7 +79,7 @@ class _SearchParser(object):
def __init__(self, search, re_verbose=False, re_unicode=None):
"""Initialize."""

if isinstance(search, _util.bytes_type):
if isinstance(search, bytes):
self.is_bytes = True
else:
self.is_bytes = False
Expand Down Expand Up @@ -164,7 +162,7 @@ def flags(self, text, scoped=False):
"""Analyze flags."""

global_retry = False
if _util.PY3 and ('a' in text or 'L' in text) and self.unicode:
if ('a' in text or 'L' in text) and self.unicode:
self.unicode = False
if not _SCOPED_FLAG_SUPPORT or not scoped:
self.temp_global_flag_swap["unicode"] = True
Expand Down Expand Up @@ -239,49 +237,6 @@ def get_named_unicode(self, i):

return ''.join(value)

def get_wide_unicode(self, i):
"""Get narrow Unicode."""

value = []
for x in range(3):
c = next(i)
if c == '0':
value.append(c)
else: # pragma: no cover
raise SyntaxError('Invalid wide Unicode character at %d!' % (i.index - 1))

c = next(i)
if c in ('0', '1'):
value.append(c)
else: # pragma: no cover
raise SyntaxError('Invalid wide Unicode character at %d!' % (i.index - 1))

for x in range(4):
c = next(i)
if c.lower() in _HEX:
value.append(c)
else: # pragma: no cover
raise SyntaxError('Invalid wide Unicode character at %d!' % (i.index - 1))
return ''.join(value)

def get_narrow_unicode(self, i):
"""Get narrow Unicode."""

value = []
for x in range(4):
c = next(i)
if c.lower() in _HEX:
value.append(c)
else: # pragma: no cover
raise SyntaxError('Invalid Unicode character at %d!' % (i.index - 1))
return ''.join(value)

def get_unicode(self, i, wide=False):
"""Get Unicode character."""

value = int(self.get_wide_unicode(i) if wide else self.get_narrow_unicode(i), 16)
return ('\\%03o' % value) if value <= 0xFF else _util.uchr(value)

def reference(self, t, i, in_group=False):
"""Handle references."""

Expand Down Expand Up @@ -311,11 +266,6 @@ def reference(self, t, i, in_group=False):
elif t == "C":
current.extend(self.letter_case_props(_UPPER, in_group, negate=True))
self.found_property = True

elif _util.PY2 and not self.is_bytes and t == "U":
current.append(self.get_unicode(i, True))
elif _util.PY2 and not self.is_bytes and t == "u":
current.append(self.get_unicode(i))
elif t == 'p':
prop = self.get_unicode_property(i)
current.extend(self.unicode_props(prop[0], prop[1], in_group=in_group))
Expand Down Expand Up @@ -606,15 +556,15 @@ def posix_props(self, prop, in_group=False):
def unicode_name(self, name, in_group=False):
"""Insert Unicode value by its name."""

value = _util.uord(_unicodedata.lookup(name))
value = ord(_unicodedata.lookup(name))
if (self.is_bytes and value > 0xFF):
value = ""
if not in_group and value == "":
return '[^%s]' % ('\x00-\xff' if self.is_bytes else _uniprops.UNICODE_RANGE)
elif value == "":
return value
else:
return ['\\%03o' % value if value <= 0xFF else _util.uchr(value)]
return ['\\%03o' % value if value <= 0xFF else chr(value)]

def unicode_props(self, props, value, in_group=False, negate=False):
"""
Expand Down Expand Up @@ -696,11 +646,8 @@ def parse(self):
"unicode": False,
"verbose": False
}
if _util.PY3:
self.ascii = self.re_unicode is not None and not self.re_unicode
else:
self.ascii = False
if _util.PY3 and not self.unicode and not self.ascii:
self.ascii = self.re_unicode is not None and not self.re_unicode
if not self.unicode and not self.ascii:
self.unicode = True

new_pattern = []
Expand Down Expand Up @@ -803,7 +750,7 @@ def get_format(self, c, i):
# Try and covert to integer index
field = ''.join(value).strip()
try:
value = [(_util.FMT_FIELD, _util.string_type(int(field, 10)))]
value = [(_util.FMT_FIELD, str(int(field, 10)))]
except ValueError:
value = [(_util.FMT_FIELD, field)]
pass
Expand Down Expand Up @@ -967,16 +914,16 @@ def parse_octal(self, text, i):
else:
single = self.get_single_stack()
if self.span_stack:
text = self.convert_case(_util.uchr(value), self.span_stack[-1])
value = _util.uord(self.convert_case(text, single)) if single is not None else _util.uord(text)
text = self.convert_case(chr(value), self.span_stack[-1])
value = ord(self.convert_case(text, single)) if single is not None else ord(text)
elif single:
value = _util.uord(self.convert_case(_util.uchr(value), single))
value = ord(self.convert_case(chr(value), single))
if self.use_format and value in _CURLY_BRACKETS_ORD:
self.handle_format(_util.uchr(value), i)
self.handle_format(chr(value), i)
elif value <= 0xFF:
self.result.append('\\%03o' % value)
else:
self.result.append(_util.uchr(value))
self.result.append(chr(value))

def get_named_unicode(self, i):
"""Get named Unicode."""
Expand All @@ -998,19 +945,19 @@ def get_named_unicode(self, i):
def parse_named_unicode(self, i):
"""Parse named Unicode."""

value = _util.uord(_unicodedata.lookup(self.get_named_unicode(i)))
value = ord(_unicodedata.lookup(self.get_named_unicode(i)))
single = self.get_single_stack()
if self.span_stack:
text = self.convert_case(_util.uchr(value), self.span_stack[-1])
value = _util.uord(self.convert_case(text, single)) if single is not None else _util.uord(text)
text = self.convert_case(chr(value), self.span_stack[-1])
value = ord(self.convert_case(text, single)) if single is not None else ord(text)
elif single:
value = _util.uord(self.convert_case(_util.uchr(value), single))
value = ord(self.convert_case(chr(value), single))
if self.use_format and value in _CURLY_BRACKETS_ORD:
self.handle_format(_util.uchr(value), i)
self.handle_format(chr(value), i)
elif value <= 0xFF:
self.result.append('\\%03o' % value)
else:
self.result.append(_util.uchr(value))
self.result.append(chr(value))

def get_wide_unicode(self, i):
"""Get narrow Unicode."""
Expand Down Expand Up @@ -1056,16 +1003,16 @@ def parse_unicode(self, i, wide=False):
value = int(text, 16)
single = self.get_single_stack()
if self.span_stack:
text = self.convert_case(_util.uchr(value), self.span_stack[-1])
value = _util.uord(self.convert_case(text, single)) if single is not None else _util.uord(text)
text = self.convert_case(chr(value), self.span_stack[-1])
value = ord(self.convert_case(text, single)) if single is not None else ord(text)
elif single:
value = _util.uord(self.convert_case(_util.uchr(value), single))
value = ord(self.convert_case(chr(value), single))
if self.use_format and value in _CURLY_BRACKETS_ORD:
self.handle_format(_util.uchr(value), i)
self.handle_format(chr(value), i)
elif value <= 0xFF:
self.result.append('\\%03o' % value)
else:
self.result.append(_util.uchr(value))
self.result.append(chr(value))

def get_byte(self, i):
"""Get byte."""
Expand All @@ -1086,11 +1033,11 @@ def parse_bytes(self, i):
single = self.get_single_stack()
if self.span_stack:
text = self.convert_case(chr(value), self.span_stack[-1])
value = _util.uord(self.convert_case(text, single)) if single is not None else _util.uord(text)
value = ord(self.convert_case(text, single)) if single is not None else ord(text)
elif single:
value = _util.uord(self.convert_case(chr(value), single))
value = ord(self.convert_case(chr(value), single))
if self.use_format and value in _CURLY_BRACKETS_ORD:
self.handle_format(_util.uchr(value), i)
self.handle_format(chr(value), i)
else:
self.result.append('\\%03o' % value)

Expand Down Expand Up @@ -1159,17 +1106,17 @@ def format_references(self, t, i):
if value > 0xFF and self.is_bytes:
# Re fails on octal greater than `0o377` or `0xFF`
raise ValueError("octal escape value outside of range 0-0o377!")
value = _util.uchr(value)
value = chr(value)
elif t in _STANDARD_ESCAPES or t == '\\':
value = _BACK_SLASH_TRANSLATION['\\' + t]
elif not self.is_bytes and t == "U":
value = _util.uchr(int(self.get_wide_unicode(i), 16))
value = chr(int(self.get_wide_unicode(i), 16))
elif not self.is_bytes and t == "u":
value = _util.uchr(int(self.get_narrow_unicode(i), 16))
value = chr(int(self.get_narrow_unicode(i), 16))
elif not self.is_bytes and t == "N":
value = _unicodedata.lookup(self.get_named_unicode(i))
elif t == "x":
value = _util.uchr(int(self.get_byte(i), 16))
value = chr(int(self.get_byte(i), 16))
else:
i.rewind(1)
value = '\\'
Expand Down Expand Up @@ -1340,12 +1287,12 @@ def handle_format_group(self, field, text):
# Handle auto incrementing group indexes
if field == '':
if self.auto:
field = _util.string_type(self.auto_index)
field = str(self.auto_index)
text[0] = (_util.FMT_FIELD, field)
self.auto_index += 1
elif not self.manual and not self.auto:
self.auto = True
field = _util.string_type(self.auto_index)
field = str(self.auto_index)
text[0] = (_util.FMT_FIELD, field)
self.auto_index += 1
else:
Expand Down Expand Up @@ -1397,11 +1344,11 @@ def get_base_template(self):
def parse(self, pattern, template, use_format=False):
"""Parse template."""

if isinstance(template, _util.bytes_type):
if isinstance(template, bytes):
self.is_bytes = True
else:
self.is_bytes = False
if isinstance(pattern.pattern, _util.bytes_type) != self.is_bytes:
if isinstance(pattern.pattern, bytes) != self.is_bytes:
raise TypeError('Pattern string type must match replace template string type!')
self._original = template
self.use_format = use_format
Expand Down Expand Up @@ -1513,7 +1460,7 @@ def expand(self, m):
raise ValueError("Match is None!")

sep = m.string[:0]
if isinstance(sep, _util.bytes_type) != self._bytes:
if isinstance(sep, bytes) != self._bytes:
raise TypeError('Match string type does not match expander string type!')
text = []
# Expand string
Expand Down Expand Up @@ -1557,4 +1504,4 @@ def _pickle(r):
return ReplaceTemplate, (r.groups, r.group_slots, r.literals, r.pattern_hash, r.use_format, r._bytes)


_util.copyreg.pickle(ReplaceTemplate, _pickle)
_copyreg.pickle(ReplaceTemplate, _pickle)
Loading

0 comments on commit 3255f04

Please sign in to comment.