Get rid of some Python 2 idiosyncrasies

This commit is contained in:
Dave Halter
2020-07-24 02:09:04 +02:00
parent 561f434f39
commit 7780cc1c1b
4 changed files with 7 additions and 31 deletions

View File

@@ -29,10 +29,10 @@ To run the tests for all supported Python versions::
tox
If you want to test only a specific Python version (e.g. Python 2.7), it's as
If you want to test only a specific Python version (e.g. Python 3.9), it's as
easy as::
tox -e py27
tox -e py39
Tests are also run automatically on `Travis CI
<https://travis-ci.org/davidhalter/parso/>`_.

View File

@@ -140,17 +140,10 @@ def load_module(hashed_grammar, file_io, cache_path=None):
def _load_from_file_system(hashed_grammar, path, p_time, cache_path=None):
cache_path = _get_hashed_path(hashed_grammar, path, cache_path=cache_path)
try:
try:
if p_time > os.path.getmtime(cache_path):
# Cache is outdated
return None
except OSError as e:
if e.errno == errno.ENOENT:
# In Python 2 instead of an IOError here we get an OSError.
raise FileNotFoundError
else:
raise
with open(cache_path, 'rb') as f:
gc.disable()

View File

@@ -48,21 +48,8 @@ BOM_UTF8_STRING = BOM_UTF8.decode('utf-8')
_token_collection_cache = {}
if sys.version_info.major >= 3:
# Python 3 has str.isidentifier() to check if a char is a valid identifier
is_identifier = str.isidentifier
else:
# Python 2 doesn't, but it's not that important anymore and if you tokenize
# Python 2 code with this, it's still ok. It's just that parsing Python 3
# code with this function is not 100% correct.
# This just means that Python 2 code matches a few identifiers too much,
# but that doesn't really matter.
def is_identifier(s):
return True
def group(*choices, **kwargs):
capture = kwargs.pop('capture', False) # Python 2, arrghhhhh :(
def group(*choices, capture=False, **kwargs):
assert not kwargs
start = '('
@@ -555,7 +542,7 @@ def tokenize_lines(lines, version_info, start_pos=(1, 0), indents=None, is_first
m = re.match(r'[ \f\t]*$', line[:start])
if m is not None:
yield from dedent_if_necessary(m.end())
if is_identifier(token):
if token.is_identifier():
yield PythonToken(NAME, token, spos, prefix)
else:
yield from _split_illegal_unicode_name(token, spos, prefix)

View File

@@ -310,10 +310,6 @@ class _StringComparisonMixin(object):
return self is other
def __ne__(self, other):
"""Python 2 compatibility."""
return not self.__eq__(other)
def __hash__(self):
return hash(self.value)