diff --git a/parso/python/normalizer.py b/parso/python/normalizer.py index 4fe8213..577ed45 100644 --- a/parso/python/normalizer.py +++ b/parso/python/normalizer.py @@ -243,11 +243,11 @@ class Context(object): yield self.blocks.pop() - @contextmanager def add_context(self, node): - new_context = Context(node, self._add_syntax_error, parent_context=self) - yield new_context - self._nonlocal_names_in_subscopes += new_context.finalize() + return Context(node, self._add_syntax_error, parent_context=self) + + def close_child_context(self, child_context): + self._nonlocal_names_in_subscopes += child_context.finalize() class ErrorFinder(Normalizer): @@ -313,13 +313,6 @@ class ErrorFinder(Normalizer): self._add_syntax_error("too many statically nested blocks", node) yield return - elif node.type in ('classdef', 'funcdef'): - context = self._context - with self._context.add_context(node) as new_context: - self._context = new_context - yield - self._context = context - return elif node.type == 'import_from' and _is_future_import(node): if not _is_future_import_first(node): message = "from __future__ imports must occur at the beginning of the file" @@ -568,6 +561,10 @@ class ErrorFinder(Normalizer): if node.type == 'suite': self._indentation_count -= 1 + elif node.type in ('classdef', 'funcdef'): + context = self._context + self._context = context.parent_context + self._context.close_child_context(context) def visit_leaf(self, leaf): if leaf.type == 'error_leaf': @@ -695,6 +692,11 @@ class ErrorFinder(Normalizer): # TODO this should probably get a better end_pos including # the next sibling of leaf. self._add_syntax_error(message, leaf) + elif leaf.value == ':': + parent = leaf.parent + if parent.type in ('classdef', 'funcdef'): + self._context = self._context.add_context(parent) + return '' diff --git a/test/normalizer_issue_files/allowed_syntax.py b/test/normalizer_issue_files/allowed_syntax.py index 237b818..9ac0a6d 100644 --- a/test/normalizer_issue_files/allowed_syntax.py +++ b/test/normalizer_issue_files/allowed_syntax.py @@ -44,3 +44,10 @@ except ZeroDivisionError: r'\n' r'\x' b'\n' + + +a = 3 + + +def x(b=a): + global a