Commit 748f27fe authored by Stefan Behnel's avatar Stefan Behnel

error tests for nonlocal, fixes to properly detect these errors (Python gives...

error tests for nonlocal, fixes to properly detect these errors (Python gives warnings for redefs, but I find errors more appropriate)
parent 7afe86bf
...@@ -1274,8 +1274,9 @@ class LocalScope(Scope): ...@@ -1274,8 +1274,9 @@ class LocalScope(Scope):
def declare_nonlocal(self, name, pos): def declare_nonlocal(self, name, pos):
# Pull entry from outer scope into local scope # Pull entry from outer scope into local scope
if self.lookup_here(name): orig_entry = self.lookup_here(name)
warning(pos, "'%s' redeclared" % name, 0) if orig_entry and orig_entry.scope is self and not orig_entry.from_closure:
error(pos, "'%s' redeclared as nonlocal" % name)
else: else:
entry = self.lookup(name) entry = self.lookup(name)
if entry is None or not entry.from_closure: if entry is None or not entry.from_closure:
...@@ -1452,8 +1453,9 @@ class PyClassScope(ClassScope): ...@@ -1452,8 +1453,9 @@ class PyClassScope(ClassScope):
def declare_nonlocal(self, name, pos): def declare_nonlocal(self, name, pos):
# Pull entry from outer scope into local scope # Pull entry from outer scope into local scope
if self.lookup_here(name): orig_entry = self.lookup_here(name)
warning(pos, "'%s' redeclared" % name, 0) if orig_entry and orig_entry.scope is self and not orig_entry.from_closure:
error(pos, "'%s' redeclared as nonlocal" % name)
else: else:
entry = self.lookup(name) entry = self.lookup(name)
if entry is None: if entry is None:
......
def test_non_existant():
nonlocal no_such_name
no_such_name = 1
def redef():
x = 1
def f():
x = 2
nonlocal x
global_name = 5
def ref_to_global():
nonlocal global_name
global_name = 6
def global_in_class_scope():
class Test():
nonlocal global_name
global_name = 6
def redef_in_class_scope():
x = 1
class Test():
x = 2
nonlocal x
_ERRORS = u"""
3:4: no binding for nonlocal 'no_such_name' found
10:8: 'x' redeclared as nonlocal
15:4: no binding for nonlocal 'global_name' found
27:8: 'x' redeclared as nonlocal
"""
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment