mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-10 22:51:59 +08:00
Get rid of some Python 2 idiosyncrasies
This commit is contained in:
@@ -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/>`_.
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user