aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDamien George2015-09-07 16:55:02 +0100
committerDamien George2015-09-07 16:55:02 +0100
commit558a016e2c02157aa610ae55e72e14a18e43abc5 (patch)
treed4152f14ff7baa9a6759bbd407a45e14e8c640f3 /tests
parent3a2171e4061f3a6e3f00c28edf78ff9473355887 (diff)
py/compile: Refine SyntaxError for repeated use of global/nonlocal.
Diffstat (limited to 'tests')
-rw-r--r--tests/basics/scope.py21
-rw-r--r--tests/basics/syntaxerror.py12
2 files changed, 31 insertions, 2 deletions
diff --git a/tests/basics/scope.py b/tests/basics/scope.py
new file mode 100644
index 000000000..3aecc0b8d
--- /dev/null
+++ b/tests/basics/scope.py
@@ -0,0 +1,21 @@
+# test scoping rules
+
+# explicit global variable
+a = 1
+def f():
+ global a
+ global a, a # should be able to redefine as global
+ a = 2
+f()
+print(a)
+
+# explicit nonlocal variable
+def f():
+ a = 1
+ def g():
+ nonlocal a
+ nonlocal a, a # should be able to redefine as nonlocal
+ a = 2
+ g()
+ return a
+print(f())
diff --git a/tests/basics/syntaxerror.py b/tests/basics/syntaxerror.py
index f7ccabcbb..d6a3e3043 100644
--- a/tests/basics/syntaxerror.py
+++ b/tests/basics/syntaxerror.py
@@ -77,8 +77,7 @@ test_syntax("return")
test_syntax("yield")
test_syntax("nonlocal a")
-# errors on uPy but shouldn't
-#test_syntax("global a; global a")
+# error on uPy, warning on CPy
#test_syntax("def f():\n a = 1\n global a")
# default except must be last
@@ -109,3 +108,12 @@ test_syntax("def f(a, a): pass")
# nonlocal must exist in outer function/class scope
test_syntax("def f():\n def g():\n nonlocal a")
+
+# param can't be redefined as global
+test_syntax('def f(x):\n global x')
+
+# param can't be redefined as nonlocal
+test_syntax('def f(x):\n nonlocal x')
+
+# can define variable to be both nonlocal and global
+test_syntax('def f():\n nonlocal x\n global x')