From 58c32591d04b1f7be641694874834a7786fd464a Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 31 Jul 2017 12:33:47 +0200 Subject: [PATCH] Some preparations to get Python 2 tests working. --- parso/normalizer.py | 7 ++++--- parso/python/normalizer.py | 9 +++++---- parso/python/pep8.py | 8 ++++---- parso/python/tree.py | 4 ++-- parso/tree.py | 12 ++++++------ test/test_normalizer_issues_files.py | 2 +- test/test_pep8.py | 3 ++- test/test_python_errors.py | 4 +--- 8 files changed, 25 insertions(+), 24 deletions(-) diff --git a/parso/normalizer.py b/parso/normalizer.py index 28cafbd..4efbba9 100644 --- a/parso/normalizer.py +++ b/parso/normalizer.py @@ -2,7 +2,8 @@ from contextlib import contextmanager class Normalizer(object): - def __init__(self, config): + def __init__(self, grammar, config): + self._grammar = grammar self._config = config self.issues = [] @@ -44,11 +45,11 @@ class Normalizer(object): class NormalizerConfig(object): normalizer_class = Normalizer - def create_normalizer(self): + def create_normalizer(self, grammar): if self.normalizer_class is None: return None - return self.normalizer_class(self) + return self.normalizer_class(grammar, self) @classmethod def register_rule(cls, rule): diff --git a/parso/python/normalizer.py b/parso/python/normalizer.py index 3694b63..591c4ec 100644 --- a/parso/python/normalizer.py +++ b/parso/python/normalizer.py @@ -224,6 +224,7 @@ class ErrorFinder(Normalizer): def __init__(self, *args, **kwargs): super(ErrorFinder, self).__init__(*args, **kwargs) self._error_dict = {} + self._version = self._grammar._version_int def initialize(self, node): from parso.python.tree import search_ancestor @@ -513,7 +514,10 @@ class ErrorFinder(Normalizer): self._add_syntax_error('invalid syntax', leaf) elif leaf.type == 'name': if leaf.value == '__debug__' and leaf.is_definition(): - message = 'assignment to keyword' + if self._version <= (2, 7): + message = 'assignment to __debug__' + else: + message = 'assignment to keyword' self._add_syntax_error(message, leaf) self._context.add_name(leaf) @@ -657,6 +661,3 @@ class ErrorFinder(Normalizer): class ErrorFinderConfig(NormalizerConfig): normalizer_class = ErrorFinder - - def __init__(self, grammar): - self.grammar = grammar diff --git a/parso/python/pep8.py b/parso/python/pep8.py index 58e4dd3..a3ebed4 100644 --- a/parso/python/pep8.py +++ b/parso/python/pep8.py @@ -149,8 +149,8 @@ def _is_magic_name(name): class PEP8Normalizer(ErrorFinder): - def __init__(self, config): - super(PEP8Normalizer, self).__init__(config) + def __init__(self, *args, **kwargs): + super(PEP8Normalizer, self).__init__(*args, **kwargs) self._previous_part = None self._previous_leaf = None self._on_newline = True @@ -161,10 +161,10 @@ class PEP8Normalizer(ErrorFinder): self._implicit_indentation_possible = False # The top of stack of the indentation nodes. self._indentation_tos = self._last_indentation_tos = \ - IndentationNode(config, indentation='') + IndentationNode(self._config, indentation='') self._in_suite_introducer = False - if ' ' in config.indentation: + if ' ' in self._config.indentation: self._indentation_type = 'spaces' self._wrong_indentation_char = '\t' else: diff --git a/parso/python/tree.py b/parso/python/tree.py index 4540385..46ec8cd 100644 --- a/parso/python/tree.py +++ b/parso/python/tree.py @@ -100,8 +100,8 @@ class PythonMixin(object): return None def _iter_errors(self, grammar): - config = normalizer.ErrorFinderConfig(grammar) - return self._get_normalizer_issues(config) + config = normalizer.ErrorFinderConfig() + return self._get_normalizer_issues(grammar, config) class PythonLeaf(PythonMixin, Leaf): diff --git a/parso/tree.py b/parso/tree.py index ba1bc38..a47b02f 100644 --- a/parso/tree.py +++ b/parso/tree.py @@ -154,24 +154,24 @@ class NodeOrLeaf(object): e.g. a statement. """ - def _get_normalizer(self, normalizer_config): + def _get_normalizer(self, normalizer_config, grammar): if normalizer_config is None: normalizer_config = self.default_normalizer_config if normalizer_config is None: raise ValueError("You need to specify a normalizer, because " "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. 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) - def _get_normalizer_issues(self, normalizer_config=None): - normalizer = self._get_normalizer(normalizer_config) + def _get_normalizer_issues(self, grammar, normalizer_config=None): + normalizer = self._get_normalizer(normalizer_config, grammar) normalizer.walk(self) return normalizer.issues diff --git a/test/test_normalizer_issues_files.py b/test/test_normalizer_issues_files.py index 13b9c73..c6e7955 100644 --- a/test/test_normalizer_issues_files.py +++ b/test/test_normalizer_issues_files.py @@ -60,7 +60,7 @@ def test_normalizer_issue(normalizer_issue_case): grammar = parso.load_grammar(version=normalizer_issue_case.python_version) module = grammar.parse(code) - issues = module._get_normalizer_issues() + issues = module._get_normalizer_issues(grammar) actual = sort(issues) diff = '\n'.join(difflib.ndiff(desired, actual)) diff --git a/test/test_pep8.py b/test/test_pep8.py index e4a243e..81419e9 100644 --- a/test/test_pep8.py +++ b/test/test_pep8.py @@ -2,8 +2,9 @@ import parso def issues(code): + grammar = parso.load_grammar() module = parso.parse(code) - return module._get_normalizer_issues() + return module._get_normalizer_issues(grammar) def test_eof_newline(): diff --git a/test/test_python_errors.py b/test/test_python_errors.py index 923d2dd..5f4e912 100644 --- a/test/test_python_errors.py +++ b/test/test_python_errors.py @@ -9,7 +9,6 @@ from textwrap import dedent import pytest import parso -from parso.python.normalizer import ErrorFinderConfig FAILING_EXAMPLES = [ @@ -221,8 +220,7 @@ if sys.version_info >= (3, 4): def _get_error_list(code, version=None): grammar = parso.load_grammar(version=version) tree = grammar.parse(code) - config = ErrorFinderConfig(grammar=grammar) - return list(tree._get_normalizer_issues(config)) + return list(tree._iter_errors(grammar)) def assert_comparison(code, error_code, positions):