def declare_nonlocal(self, name, pos):
# Pull entry from outer scope into local scope
- if self.lookup_here(name):
- warning(pos, "'%s' redeclared" % name, 0)
+ orig_entry = self.lookup_here(name)
+ if orig_entry and orig_entry.scope is self and not orig_entry.from_closure:
+ error(pos, "'%s' redeclared as nonlocal" % name)
else:
entry = self.lookup(name)
if entry is None or not entry.from_closure:
def declare_nonlocal(self, name, pos):
# Pull entry from outer scope into local scope
- if self.lookup_here(name):
- warning(pos, "'%s' redeclared" % name, 0)
+ orig_entry = self.lookup_here(name)
+ if orig_entry and orig_entry.scope is self and not orig_entry.from_closure:
+ error(pos, "'%s' redeclared as nonlocal" % name)
else:
entry = self.lookup(name)
if entry is None:
--- /dev/null
+
+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
+"""