mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-08 21:54:54 +08:00
Fix opening contexts at the right position.
This commit is contained in:
@@ -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 ''
|
||||
|
||||
|
||||
@@ -44,3 +44,10 @@ except ZeroDivisionError:
|
||||
r'\n'
|
||||
r'\x'
|
||||
b'\n'
|
||||
|
||||
|
||||
a = 3
|
||||
|
||||
|
||||
def x(b=a):
|
||||
global a
|
||||
|
||||
Reference in New Issue
Block a user