mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 21:34:32 +08:00
Fix opening contexts at the right position.
This commit is contained in:
@@ -243,11 +243,11 @@ class Context(object):
|
|||||||
yield
|
yield
|
||||||
self.blocks.pop()
|
self.blocks.pop()
|
||||||
|
|
||||||
@contextmanager
|
|
||||||
def add_context(self, node):
|
def add_context(self, node):
|
||||||
new_context = Context(node, self._add_syntax_error, parent_context=self)
|
return Context(node, self._add_syntax_error, parent_context=self)
|
||||||
yield new_context
|
|
||||||
self._nonlocal_names_in_subscopes += new_context.finalize()
|
def close_child_context(self, child_context):
|
||||||
|
self._nonlocal_names_in_subscopes += child_context.finalize()
|
||||||
|
|
||||||
|
|
||||||
class ErrorFinder(Normalizer):
|
class ErrorFinder(Normalizer):
|
||||||
@@ -313,13 +313,6 @@ class ErrorFinder(Normalizer):
|
|||||||
self._add_syntax_error("too many statically nested blocks", node)
|
self._add_syntax_error("too many statically nested blocks", node)
|
||||||
yield
|
yield
|
||||||
return
|
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):
|
elif node.type == 'import_from' and _is_future_import(node):
|
||||||
if not _is_future_import_first(node):
|
if not _is_future_import_first(node):
|
||||||
message = "from __future__ imports must occur at the beginning of the file"
|
message = "from __future__ imports must occur at the beginning of the file"
|
||||||
@@ -568,6 +561,10 @@ class ErrorFinder(Normalizer):
|
|||||||
|
|
||||||
if node.type == 'suite':
|
if node.type == 'suite':
|
||||||
self._indentation_count -= 1
|
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):
|
def visit_leaf(self, leaf):
|
||||||
if leaf.type == 'error_leaf':
|
if leaf.type == 'error_leaf':
|
||||||
@@ -695,6 +692,11 @@ class ErrorFinder(Normalizer):
|
|||||||
# TODO this should probably get a better end_pos including
|
# TODO this should probably get a better end_pos including
|
||||||
# the next sibling of leaf.
|
# the next sibling of leaf.
|
||||||
self._add_syntax_error(message, 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 ''
|
return ''
|
||||||
|
|
||||||
|
|||||||
@@ -44,3 +44,10 @@ except ZeroDivisionError:
|
|||||||
r'\n'
|
r'\n'
|
||||||
r'\x'
|
r'\x'
|
||||||
b'\n'
|
b'\n'
|
||||||
|
|
||||||
|
|
||||||
|
a = 3
|
||||||
|
|
||||||
|
|
||||||
|
def x(b=a):
|
||||||
|
global a
|
||||||
|
|||||||
Reference in New Issue
Block a user