Add the issue 'name %s is nonlocal and global

This commit is contained in:
Dave Halter
2017-07-30 13:12:27 +02:00
parent 443cb1ce08
commit d7b7548f8d
2 changed files with 19 additions and 1 deletions

View File

@@ -128,7 +128,20 @@ class Context(object):
self._analyze_names(self._global_names, 'global')
self._analyze_names(self._nonlocal_names, 'nonlocal')
#for self.global_names
# Python2.6 doesn't have dict comprehensions.
nonlocal_name_strs = dict((n.value, n) for n in self._nonlocal_names)
for global_name in self._global_names:
try:
nonlocal_name = nonlocal_name_strs[global_name.value]
except KeyError:
continue
message = "name '%s' is nonlocal and global" % global_name.value
if global_name.start_pos < nonlocal_name.start_pos:
error_name = global_name
else:
error_name = nonlocal_name
self._add_syntax_error(message, error_name)
def _analyze_names(self, globals_or_nonlocals, type_):
def raise_(message):

View File

@@ -220,6 +220,11 @@ def test_indentation_errors(code, positions):
a = 3
nonlocal a
'''),
dedent('''
def x():
global a
nonlocal a
'''),
# IndentationError