Skip to content

Commit 6edfa85

Browse files
committed
Add test case for nonlocal variables
1 parent d9297ac commit 6edfa85

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

tests/expressions/test_interpreter_coverage6.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,29 @@ def inner():
16501650
assert interpreter.local_env["result4"] == 1
16511651
assert interpreter.local_env["result5"] == 2
16521652

1653+
# Test variable inheritance and updates
1654+
code = """
1655+
x = 1
1656+
def level1():
1657+
y = 2
1658+
def level2():
1659+
nonlocal y # TODO: We should not use nonlocal here. CPython does not require it.
1660+
z = 3
1661+
def level3():
1662+
nonlocal y, z
1663+
y = 4
1664+
z = 5
1665+
return x, y, z
1666+
result1 = level3()
1667+
return result1, y, z
1668+
result2 = level2()
1669+
return result2, y
1670+
1671+
result = level1()
1672+
"""
1673+
interpreter.execute(parser.parse(code))
1674+
assert interpreter.local_env["result"] == (((1, 4, 5), 4, 5), 4)
1675+
16531676
# Test exception handling with environment restoration
16541677
code = """
16551678
x = 1

0 commit comments

Comments
 (0)