summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_syntax.py
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2007-02-27 06:50:52 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2007-02-27 06:50:52 (GMT)
commit81e9502df69394821416309c7c4b5357af51f4d5 (patch)
treead38831cbebfb32890c0c57cb8b36f653300c69f /Lib/test/test_syntax.py
parent8b41c3dc28a16da97af50cc5f7b884db2cea7b0c (diff)
downloadcpython-81e9502df69394821416309c7c4b5357af51f4d5.zip
cpython-81e9502df69394821416309c7c4b5357af51f4d5.tar.gz
cpython-81e9502df69394821416309c7c4b5357af51f4d5.tar.bz2
Provisional implementation of PEP 3104.
Add nonlocal_stmt to Grammar and Nonlocal node to AST. They both parallel the definitions for globals. The symbol table treats variables declared as nonlocal just like variables that are free implicitly. This change is missing the language spec changes, but makes some decisions about what the spec should say via the unittests. The PEP is silent on a number of decisions, so we should review those before claiming that nonlocal is complete. Thomas Wouters made the grammer and ast changes. Jeremy Hylton added the symbol table changes and the tests. Pete Shinners and Neal Norwitz helped review the code.
Diffstat (limited to 'Lib/test/test_syntax.py')
-rw-r--r--Lib/test/test_syntax.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index 8999e3a..f37b666 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -367,6 +367,46 @@ build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
...
SystemError: too many statically nested blocks
+Misuse of the nonlocal statement can lead to a few unique syntax errors.
+
+ >>> def f(x):
+ ... nonlocal x
+ Traceback (most recent call last):
+ ...
+ SyntaxError: name 'x' is local and nonlocal
+
+ >>> def f():
+ ... global x
+ ... nonlocal x
+ Traceback (most recent call last):
+ ...
+ SyntaxError: name 'x' is nonlocal and global
+
+ >>> def f():
+ ... nonlocal x
+ Traceback (most recent call last):
+ ...
+ SyntaxError: no binding for nonlocal 'x' found
+
+TODO(jhylton): Figure out how to test SyntaxWarning with doctest.
+
+## >>> def f(x):
+## ... def f():
+## ... print(x)
+## ... nonlocal x
+## Traceback (most recent call last):
+## ...
+## SyntaxWarning: name 'x' is assigned to before nonlocal declaration
+
+## >>> def f():
+## ... x = 1
+## ... nonlocal x
+## Traceback (most recent call last):
+## ...
+## SyntaxWarning: name 'x' is assigned to before nonlocal declaration
+
+
+
"""
import re