Commit 399b1fe8 authored by Benjamin Peterson's avatar Benjamin Peterson

give a py3k warning when 'nonlocal' is used as a variable name

parent c756dcdd
......@@ -28,53 +28,35 @@ class TestPy3KWarnings(unittest.TestCase):
exec "`2`" in {}
self.assertWarning(None, w, expected)
def test_bool_assign(self):
def test_forbidden_names(self):
# So we don't screw up our globals
def safe_exec(expr):
def f(**kwargs): pass
exec expr in {'f' : f}
expected = "assignment to True or False is forbidden in 3.x"
tests = [("True", "assignment to True or False is forbidden in 3.x"),
("False", "assignment to True or False is forbidden in 3.x"),
("nonlocal", "nonlocal is a keyword in 3.x")]
with check_warnings() as w:
safe_exec("True = False")
self.assertWarning(None, w, expected)
w.reset()
safe_exec("False = True")
self.assertWarning(None, w, expected)
w.reset()
try:
safe_exec("obj.False = True")
except NameError: pass
for keyword, expected in tests:
safe_exec("{0} = False".format(keyword))
self.assertWarning(None, w, expected)
w.reset()
try:
safe_exec("obj.True = False")
except NameError: pass
self.assertWarning(None, w, expected)
w.reset()
safe_exec("def False(): pass")
self.assertWarning(None, w, expected)
w.reset()
safe_exec("def True(): pass")
self.assertWarning(None, w, expected)
w.reset()
safe_exec("class False: pass")
self.assertWarning(None, w, expected)
w.reset()
safe_exec("class True: pass")
safe_exec("obj.{0} = True".format(keyword))
except NameError:
pass
self.assertWarning(None, w, expected)
w.reset()
safe_exec("def f(True=43): pass")
safe_exec("def {0}(): pass".format(keyword))
self.assertWarning(None, w, expected)
w.reset()
safe_exec("def f(False=None): pass")
safe_exec("class {0}: pass".format(keyword))
self.assertWarning(None, w, expected)
w.reset()
safe_exec("f(False=True)")
safe_exec("def f({0}=43): pass".format(keyword))
self.assertWarning(None, w, expected)
w.reset()
safe_exec("f(True=1)")
self.assertWarning(None, w, expected)
def test_type_inequality_comparisons(self):
......
......@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1
Core and Builtins
-----------------
- Using ``nonlocal`` as a variable name will now raise a Py3k SyntaxWarning
because it is a reserved work in 3.x.
- On windows, os.chdir given unicode was not working if GetCurrentDirectoryW
returned a path longer than MAX_PATH. (But It's doubtful this code path is
really executed because I cannot move to such directory on win2k)
......
......@@ -131,9 +131,14 @@ forbidden_check(struct compiling *c, const node *n, const char *x)
{
if (!strcmp(x, "None"))
return ast_error(n, "assignment to None");
if (Py_Py3kWarningFlag && !(strcmp(x, "True") && strcmp(x, "False")) &&
if (Py_Py3kWarningFlag) {
if (!(strcmp(x, "True") && strcmp(x, "False")) &&
!ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
return 0;
if (!strcmp(x, "nonlocal") &&
!ast_warn(c, n, "nonlocal is a keyword in 3.x"))
return 0;
}
return 1;
}
......
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