Skip to content

Commit 5ac1935

Browse files
committed
don't resolve global variables until runtime
1 parent c1e725f commit 5ac1935

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

custom_components/pyscript/eval.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,10 @@ async def resolve_nonlocals(self, ast_ctx):
545545
self.local_sym_table[var_name] = sym_table[var_name]
546546
break
547547
else:
548-
val = await ast_ctx.ast_name(ast.Name(id=var_name, ctx=ast.Load()))
549-
if isinstance(val, EvalName) and got_dot < 0:
550-
raise SyntaxError(f"no binding for nonlocal '{var_name}' found")
548+
if var_name in nonlocal_names:
549+
val = await ast_ctx.ast_name(ast.Name(id=var_name, ctx=ast.Load()))
550+
if isinstance(val, EvalName) and got_dot < 0:
551+
raise SyntaxError(f"no binding for nonlocal '{var_name}' found")
551552

552553
def get_decorators(self):
553554
"""Return the function decorators."""

tests/test_unit_eval.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,16 @@ def foo(arg=6):
338338
],
339339
[
340340
"""
341+
def func():
342+
global x
343+
x = 134
344+
func()
345+
x
346+
""",
347+
134,
348+
],
349+
[
350+
"""
341351
def foo0(arg=6):
342352
bar = 100
343353
bar2 = 1000
@@ -404,6 +414,19 @@ def foo1(arg=6):
404414
],
405415
[
406416
"""
417+
def f():
418+
def b():
419+
return s+g
420+
s = "hello "
421+
return b
422+
b = f()
423+
g = "world"
424+
b()
425+
""",
426+
"hello world",
427+
],
428+
[
429+
"""
407430
@dec_test("abc")
408431
def foo(cnt=4):
409432
sum = 0

0 commit comments

Comments
 (0)