summaryrefslogtreecommitdiff
path: root/tests/basics/scope_implicit.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-10-26 16:48:07 +1100
committerDamien George <damien.p.george@gmail.com>2018-10-28 00:33:08 +1100
commit9201f46cc8e92231f0f6c92e4d56befbc150f72c (patch)
tree425eeb6f9031a94acf2b79cb56a0a35a902da173 /tests/basics/scope_implicit.py
parentc2074e7b66a042492604fbf9ea80b71cdf848e93 (diff)
py/compile: Fix case of eager implicit conversion of local to nonlocal.
This ensures that implicit variables are only converted to implicit closed-over variables (nonlocals) at the very end of the function scope. If variables are closed-over when first used (read from, as was done prior to this commit) then this can be incorrect because the variable may be assigned to later on in the function which means they are just a plain local, not closed over. Fixes issue #4272.
Diffstat (limited to 'tests/basics/scope_implicit.py')
-rw-r--r--tests/basics/scope_implicit.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/tests/basics/scope_implicit.py b/tests/basics/scope_implicit.py
new file mode 100644
index 000000000..aecda7715
--- /dev/null
+++ b/tests/basics/scope_implicit.py
@@ -0,0 +1,31 @@
+# test implicit scoping rules
+
+# implicit nonlocal, with variable defined after closure
+def f():
+ def g():
+ return x # implicit nonlocal
+ x = 3 # variable defined after function that closes over it
+ return g
+print(f()())
+
+# implicit nonlocal at inner level, with variable defined after closure
+def f():
+ def g():
+ def h():
+ return x # implicit nonlocal
+ return h
+ x = 4 # variable defined after function that closes over it
+ return g
+print(f()()())
+
+# local variable which should not be implicitly made nonlocal
+def f():
+ x = 0
+ def g():
+ x # local because next statement assigns to it
+ x = 1
+ g()
+try:
+ f()
+except NameError:
+ print('NameError')