Some preparations to get Python 2 tests working.

This commit is contained in:
Dave Halter
2017-07-31 12:33:47 +02:00
parent 5fbbb225dd
commit 58c32591d0
8 changed files with 25 additions and 24 deletions

View File

@@ -2,7 +2,8 @@ from contextlib import contextmanager
class Normalizer(object): class Normalizer(object):
def __init__(self, config): def __init__(self, grammar, config):
self._grammar = grammar
self._config = config self._config = config
self.issues = [] self.issues = []
@@ -44,11 +45,11 @@ class Normalizer(object):
class NormalizerConfig(object): class NormalizerConfig(object):
normalizer_class = Normalizer normalizer_class = Normalizer
def create_normalizer(self): def create_normalizer(self, grammar):
if self.normalizer_class is None: if self.normalizer_class is None:
return None return None
return self.normalizer_class(self) return self.normalizer_class(grammar, self)
@classmethod @classmethod
def register_rule(cls, rule): def register_rule(cls, rule):

View File

@@ -224,6 +224,7 @@ class ErrorFinder(Normalizer):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(ErrorFinder, self).__init__(*args, **kwargs) super(ErrorFinder, self).__init__(*args, **kwargs)
self._error_dict = {} self._error_dict = {}
self._version = self._grammar._version_int
def initialize(self, node): def initialize(self, node):
from parso.python.tree import search_ancestor from parso.python.tree import search_ancestor
@@ -513,6 +514,9 @@ class ErrorFinder(Normalizer):
self._add_syntax_error('invalid syntax', leaf) self._add_syntax_error('invalid syntax', leaf)
elif leaf.type == 'name': elif leaf.type == 'name':
if leaf.value == '__debug__' and leaf.is_definition(): if leaf.value == '__debug__' and leaf.is_definition():
if self._version <= (2, 7):
message = 'assignment to __debug__'
else:
message = 'assignment to keyword' message = 'assignment to keyword'
self._add_syntax_error(message, leaf) self._add_syntax_error(message, leaf)
@@ -657,6 +661,3 @@ class ErrorFinder(Normalizer):
class ErrorFinderConfig(NormalizerConfig): class ErrorFinderConfig(NormalizerConfig):
normalizer_class = ErrorFinder normalizer_class = ErrorFinder
def __init__(self, grammar):
self.grammar = grammar

View File

@@ -149,8 +149,8 @@ def _is_magic_name(name):
class PEP8Normalizer(ErrorFinder): class PEP8Normalizer(ErrorFinder):
def __init__(self, config): def __init__(self, *args, **kwargs):
super(PEP8Normalizer, self).__init__(config) super(PEP8Normalizer, self).__init__(*args, **kwargs)
self._previous_part = None self._previous_part = None
self._previous_leaf = None self._previous_leaf = None
self._on_newline = True self._on_newline = True
@@ -161,10 +161,10 @@ class PEP8Normalizer(ErrorFinder):
self._implicit_indentation_possible = False self._implicit_indentation_possible = False
# The top of stack of the indentation nodes. # The top of stack of the indentation nodes.
self._indentation_tos = self._last_indentation_tos = \ self._indentation_tos = self._last_indentation_tos = \
IndentationNode(config, indentation='') IndentationNode(self._config, indentation='')
self._in_suite_introducer = False self._in_suite_introducer = False
if ' ' in config.indentation: if ' ' in self._config.indentation:
self._indentation_type = 'spaces' self._indentation_type = 'spaces'
self._wrong_indentation_char = '\t' self._wrong_indentation_char = '\t'
else: else:

View File

@@ -100,8 +100,8 @@ class PythonMixin(object):
return None return None
def _iter_errors(self, grammar): def _iter_errors(self, grammar):
config = normalizer.ErrorFinderConfig(grammar) config = normalizer.ErrorFinderConfig()
return self._get_normalizer_issues(config) return self._get_normalizer_issues(grammar, config)
class PythonLeaf(PythonMixin, Leaf): class PythonLeaf(PythonMixin, Leaf):

View File

@@ -154,24 +154,24 @@ class NodeOrLeaf(object):
e.g. a statement. e.g. a statement.
""" """
def _get_normalizer(self, normalizer_config): def _get_normalizer(self, normalizer_config, grammar):
if normalizer_config is None: if normalizer_config is None:
normalizer_config = self.default_normalizer_config normalizer_config = self.default_normalizer_config
if normalizer_config is None: if normalizer_config is None:
raise ValueError("You need to specify a normalizer, because " raise ValueError("You need to specify a normalizer, because "
"there's no default normalizer for this tree.") "there's no default normalizer for this tree.")
return normalizer_config.create_normalizer() return normalizer_config.create_normalizer(grammar)
def _normalize(self, normalizer_config=None): def _normalize(self, grammar, normalizer_config=None):
""" """
TODO this is not public, yet. TODO this is not public, yet.
The returned code will be normalized, e.g. PEP8 for Python. The returned code will be normalized, e.g. PEP8 for Python.
""" """
normalizer = self._get_normalizer(normalizer_config) normalizer = self._get_normalizer(normalizer_config, grammar)
return normalizer.walk(self) return normalizer.walk(self)
def _get_normalizer_issues(self, normalizer_config=None): def _get_normalizer_issues(self, grammar, normalizer_config=None):
normalizer = self._get_normalizer(normalizer_config) normalizer = self._get_normalizer(normalizer_config, grammar)
normalizer.walk(self) normalizer.walk(self)
return normalizer.issues return normalizer.issues

View File

@@ -60,7 +60,7 @@ def test_normalizer_issue(normalizer_issue_case):
grammar = parso.load_grammar(version=normalizer_issue_case.python_version) grammar = parso.load_grammar(version=normalizer_issue_case.python_version)
module = grammar.parse(code) module = grammar.parse(code)
issues = module._get_normalizer_issues() issues = module._get_normalizer_issues(grammar)
actual = sort(issues) actual = sort(issues)
diff = '\n'.join(difflib.ndiff(desired, actual)) diff = '\n'.join(difflib.ndiff(desired, actual))

View File

@@ -2,8 +2,9 @@ import parso
def issues(code): def issues(code):
grammar = parso.load_grammar()
module = parso.parse(code) module = parso.parse(code)
return module._get_normalizer_issues() return module._get_normalizer_issues(grammar)
def test_eof_newline(): def test_eof_newline():

View File

@@ -9,7 +9,6 @@ from textwrap import dedent
import pytest import pytest
import parso import parso
from parso.python.normalizer import ErrorFinderConfig
FAILING_EXAMPLES = [ FAILING_EXAMPLES = [
@@ -221,8 +220,7 @@ if sys.version_info >= (3, 4):
def _get_error_list(code, version=None): def _get_error_list(code, version=None):
grammar = parso.load_grammar(version=version) grammar = parso.load_grammar(version=version)
tree = grammar.parse(code) tree = grammar.parse(code)
config = ErrorFinderConfig(grammar=grammar) return list(tree._iter_errors(grammar))
return list(tree._get_normalizer_issues(config))
def assert_comparison(code, error_code, positions): def assert_comparison(code, error_code, positions):