Fix another small issue with global/nonlocal names.

This commit is contained in:
Dave Halter
2017-07-30 12:55:58 +02:00
parent 2420f57a5c
commit 443cb1ce08
2 changed files with 16 additions and 6 deletions

View File

@@ -128,6 +128,8 @@ class Context(object):
self._analyze_names(self._global_names, 'global') self._analyze_names(self._global_names, 'global')
self._analyze_names(self._nonlocal_names, 'nonlocal') self._analyze_names(self._nonlocal_names, 'nonlocal')
#for self.global_names
def _analyze_names(self, globals_or_nonlocals, type_): def _analyze_names(self, globals_or_nonlocals, type_):
def raise_(message): def raise_(message):
self._add_syntax_error(message % (base_name.value, type_), base_name) self._add_syntax_error(message % (base_name.value, type_), base_name)
@@ -136,11 +138,10 @@ class Context(object):
if self.node.type == 'funcdef': if self.node.type == 'funcdef':
params = self.node.params params = self.node.params
found_global_or_nonlocal = False
for base_name in globals_or_nonlocals: for base_name in globals_or_nonlocals:
search = base_name.value found_global_or_nonlocal = False
# Somehow Python does it the reversed way. # Somehow Python does it the reversed way.
for name in reversed(self._used_name_dict.get(search, [])): for name in reversed(self._used_name_dict.get(base_name.value, [])):
if name.start_pos > base_name.start_pos: if name.start_pos > base_name.start_pos:
# All following names don't have to be checked. # All following names don't have to be checked.
found_global_or_nonlocal = True found_global_or_nonlocal = True

View File

@@ -202,16 +202,25 @@ def test_indentation_errors(code, positions):
def glob(): def glob():
global x global x
x: foo = 3 x: foo = 3
'''), '''),
# global/nonlocal + param # global/nonlocal + param
dedent(''' dedent('''
def glob(x): def glob(x):
global x global x
'''), '''),
dedent(''' dedent('''
def glob(x): def glob(x):
nonlocal x nonlocal x
'''), '''),
dedent('''
def x():
a =3
def z():
nonlocal a
a = 3
nonlocal a
'''),
# IndentationError # IndentationError
' foo', ' foo',