mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-08 05:34:51 +08:00
Refactor a bit for Python 2.
This commit is contained in:
@@ -111,6 +111,7 @@ class Checker():
|
|||||||
def __init__(self, version, is_passing):
|
def __init__(self, version, is_passing):
|
||||||
self.version = version
|
self.version = version
|
||||||
self._is_passing = is_passing
|
self._is_passing = is_passing
|
||||||
|
self.grammar = parso.load_grammar(version=self.version)
|
||||||
|
|
||||||
def parse(self, code):
|
def parse(self, code):
|
||||||
if self._is_passing:
|
if self._is_passing:
|
||||||
@@ -125,7 +126,7 @@ class Checker():
|
|||||||
print(module.children)
|
print(module.children)
|
||||||
|
|
||||||
def get_error(self, code):
|
def get_error(self, code):
|
||||||
errors = list(parso.parse(code, version=self.version)._iter_errors())
|
errors = list(self.grammar.parse(code)._iter_errors(self.grammar))
|
||||||
assert bool(errors) != self._is_passing
|
assert bool(errors) != self._is_passing
|
||||||
if errors:
|
if errors:
|
||||||
return errors[0]
|
return errors[0]
|
||||||
@@ -138,7 +139,7 @@ class Checker():
|
|||||||
|
|
||||||
def assert_no_error_in_passing(self, code):
|
def assert_no_error_in_passing(self, code):
|
||||||
if self._is_passing:
|
if self._is_passing:
|
||||||
assert not list(parso.parse(code, version=self.version)._iter_errors())
|
assert not list(self.grammar.parse(code)._iter_errors(self.grammar))
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
from parso.normalizer import Normalizer, NormalizerConfig, Issue
|
from parso.normalizer import Normalizer, NormalizerConfig, Issue
|
||||||
@@ -656,3 +657,6 @@ class ErrorFinder(Normalizer):
|
|||||||
|
|
||||||
class ErrorFinderConfig(NormalizerConfig):
|
class ErrorFinderConfig(NormalizerConfig):
|
||||||
normalizer_class = ErrorFinder
|
normalizer_class = ErrorFinder
|
||||||
|
|
||||||
|
def __init__(self, grammar):
|
||||||
|
self.grammar = grammar
|
||||||
|
|||||||
@@ -99,8 +99,8 @@ class PythonMixin(object):
|
|||||||
return result
|
return result
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _iter_errors(self):
|
def _iter_errors(self, grammar):
|
||||||
config = normalizer.ErrorFinderConfig()
|
config = normalizer.ErrorFinderConfig(grammar)
|
||||||
return self._get_normalizer_issues(config)
|
return self._get_normalizer_issues(config)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -39,38 +39,3 @@ except:
|
|||||||
pass
|
pass
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def c():
|
|
||||||
a = 3
|
|
||||||
|
|
||||||
def d():
|
|
||||||
class X():
|
|
||||||
nonlocal a
|
|
||||||
|
|
||||||
|
|
||||||
def x():
|
|
||||||
a = 3
|
|
||||||
|
|
||||||
def y():
|
|
||||||
nonlocal a
|
|
||||||
|
|
||||||
|
|
||||||
def x():
|
|
||||||
def y():
|
|
||||||
nonlocal a
|
|
||||||
|
|
||||||
a = 3
|
|
||||||
|
|
||||||
|
|
||||||
def x():
|
|
||||||
a = 3
|
|
||||||
|
|
||||||
def y():
|
|
||||||
class z():
|
|
||||||
nonlocal a
|
|
||||||
|
|
||||||
|
|
||||||
a = *args, *args
|
|
||||||
error[(*args, *args)] = 3
|
|
||||||
*args, *args
|
|
||||||
|
|||||||
@@ -8,3 +8,38 @@ foo[3]: int
|
|||||||
def glob():
|
def glob():
|
||||||
global x
|
global x
|
||||||
y: foo = x
|
y: foo = x
|
||||||
|
|
||||||
|
|
||||||
|
def c():
|
||||||
|
a = 3
|
||||||
|
|
||||||
|
def d():
|
||||||
|
class X():
|
||||||
|
nonlocal a
|
||||||
|
|
||||||
|
|
||||||
|
def x():
|
||||||
|
a = 3
|
||||||
|
|
||||||
|
def y():
|
||||||
|
nonlocal a
|
||||||
|
|
||||||
|
|
||||||
|
def x():
|
||||||
|
def y():
|
||||||
|
nonlocal a
|
||||||
|
|
||||||
|
a = 3
|
||||||
|
|
||||||
|
|
||||||
|
def x():
|
||||||
|
a = 3
|
||||||
|
|
||||||
|
def y():
|
||||||
|
class z():
|
||||||
|
nonlocal a
|
||||||
|
|
||||||
|
|
||||||
|
a = *args, *args
|
||||||
|
error[(*args, *args)] = 3
|
||||||
|
*args, *args
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
Testing if parso finds syntax errors and indentation errors.
|
Testing if parso finds syntax errors and indentation errors.
|
||||||
"""
|
"""
|
||||||
@@ -218,8 +219,9 @@ if sys.version_info >= (3, 4):
|
|||||||
|
|
||||||
|
|
||||||
def _get_error_list(code, version=None):
|
def _get_error_list(code, version=None):
|
||||||
tree = parso.parse(code, version=version)
|
grammar = parso.load_grammar(version=version)
|
||||||
config = ErrorFinderConfig()
|
tree = grammar.parse(code)
|
||||||
|
config = ErrorFinderConfig(grammar=grammar)
|
||||||
return list(tree._get_normalizer_issues(config))
|
return list(tree._get_normalizer_issues(config))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user