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): ...@@ -28,53 +28,35 @@ class TestPy3KWarnings(unittest.TestCase):
exec "`2`" in {} exec "`2`" in {}
self.assertWarning(None, w, expected) self.assertWarning(None, w, expected)
def test_bool_assign(self): def test_forbidden_names(self):
# So we don't screw up our globals # So we don't screw up our globals
def safe_exec(expr): def safe_exec(expr):
def f(**kwargs): pass def f(**kwargs): pass
exec expr in {'f' : f} 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: with check_warnings() as w:
safe_exec("True = False") for keyword, expected in tests:
self.assertWarning(None, w, expected) safe_exec("{0} = False".format(keyword))
w.reset() self.assertWarning(None, w, expected)
safe_exec("False = True") w.reset()
self.assertWarning(None, w, expected) try:
w.reset() safe_exec("obj.{0} = True".format(keyword))
try: except NameError:
safe_exec("obj.False = True") pass
except NameError: pass self.assertWarning(None, w, expected)
self.assertWarning(None, w, expected) w.reset()
w.reset() safe_exec("def {0}(): pass".format(keyword))
try: self.assertWarning(None, w, expected)
safe_exec("obj.True = False") w.reset()
except NameError: pass safe_exec("class {0}: pass".format(keyword))
self.assertWarning(None, w, expected) self.assertWarning(None, w, expected)
w.reset() w.reset()
safe_exec("def False(): pass") safe_exec("def f({0}=43): pass".format(keyword))
self.assertWarning(None, w, expected) self.assertWarning(None, w, expected)
w.reset() 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")
self.assertWarning(None, w, expected)
w.reset()
safe_exec("def f(True=43): pass")
self.assertWarning(None, w, expected)
w.reset()
safe_exec("def f(False=None): pass")
self.assertWarning(None, w, expected)
w.reset()
safe_exec("f(False=True)")
self.assertWarning(None, w, expected)
w.reset()
safe_exec("f(True=1)")
self.assertWarning(None, w, expected)
def test_type_inequality_comparisons(self): def test_type_inequality_comparisons(self):
......
...@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1 ...@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1
Core and Builtins 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 - 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 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) 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) ...@@ -131,9 +131,14 @@ forbidden_check(struct compiling *c, const node *n, const char *x)
{ {
if (!strcmp(x, "None")) if (!strcmp(x, "None"))
return ast_error(n, "assignment to None"); return ast_error(n, "assignment to None");
if (Py_Py3kWarningFlag && !(strcmp(x, "True") && strcmp(x, "False")) && if (Py_Py3kWarningFlag) {
!ast_warn(c, n, "assignment to True or False is forbidden in 3.x")) if (!(strcmp(x, "True") && strcmp(x, "False")) &&
return 0; !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; 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