Skip to content

Commit 5e8afde

Browse files
committed
inner functions and classes added to global sym table if declared as global
1 parent 4cc285c commit 5e8afde

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

custom_components/pyscript/eval.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,11 @@ async def ast_while(self, arg):
961961
async def ast_classdef(self, arg):
962962
"""Evaluate class definition."""
963963
bases = [(await self.aeval(base)) for base in arg.bases]
964-
self.sym_table[arg.name] = EvalLocalVar(arg.name)
964+
if self.curr_func and arg.name in self.curr_func.global_names:
965+
sym_table_assign = self.global_sym_table
966+
else:
967+
sym_table_assign = self.sym_table
968+
sym_table_assign[arg.name] = EvalLocalVar(arg.name)
965969
sym_table = {}
966970
self.sym_table_stack.append(self.sym_table)
967971
self.sym_table = sym_table
@@ -977,7 +981,7 @@ async def ast_classdef(self, arg):
977981
if "__init__" in sym_table:
978982
sym_table["__init__evalfunc_wrap__"] = sym_table["__init__"]
979983
del sym_table["__init__"]
980-
self.sym_table[arg.name].set(type(arg.name, tuple(bases), sym_table))
984+
sym_table_assign[arg.name].set(type(arg.name, tuple(bases), sym_table))
981985

982986
async def ast_functiondef(self, arg):
983987
"""Evaluate function definition."""
@@ -1020,10 +1024,14 @@ async def ast_functiondef(self, arg):
10201024
else:
10211025
func_var = func
10221026

1023-
if name in self.sym_table and isinstance(self.sym_table[name], EvalLocalVar):
1024-
self.sym_table[name].set(func_var)
1027+
if self.curr_func and name in self.curr_func.global_names:
1028+
sym_table = self.global_sym_table
1029+
else:
1030+
sym_table = self.sym_table
1031+
if name in sym_table and isinstance(sym_table[name], EvalLocalVar):
1032+
sym_table[name].set(func_var)
10251033
else:
1026-
self.sym_table[name] = func_var
1034+
sym_table[name] = func_var
10271035

10281036
async def ast_lambda(self, arg):
10291037
"""Evaluate lambda definition."""

docs/new_features.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ Bug fixes since 1.1.0 include:
5353
the service, reported by @wsw70 (#121)
5454
- Added error message for invalid ``@time_active`` argument, by @dlashua (#118).
5555
- The ``scripts`` subdirectory is now recursively traversed for ``requirements.txt`` files.
56+
- Inner functions and classes (defined inside a function) are added to global symbol table
57+
if declared as global.

tests/test_decorators.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ def func4():
153153
154154
@state_trigger("pyscript.var1 == '5'")
155155
def func5(value=None):
156-
global seq_num
157-
global startup_test_save
156+
global seq_num, startup_test
158157
159158
seq_num += 1
160159
pyscript.done = [seq_num, int(value)]
@@ -166,8 +165,6 @@ def startup_test():
166165
seq_num += 1
167166
pyscript.done = [seq_num, int(value)]
168167
169-
startup_test_save = startup_test
170-
171168
def add_state_trig(value):
172169
def dec_add_state_trig(func):
173170
nonlocal value

tests/test_unit_eval.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,31 @@ def foo2():
657657
],
658658
[
659659
"""
660+
def foo():
661+
global f_bar
662+
def f_bar():
663+
return "hello"
664+
665+
foo()
666+
f_bar()
667+
""",
668+
"hello",
669+
],
670+
[
671+
"""
672+
def foo():
673+
global inner_class
674+
class inner_class:
675+
def method1():
676+
return 1234
677+
678+
foo()
679+
inner_class.method1()
680+
""",
681+
1234,
682+
],
683+
[
684+
"""
660685
def foo(x=30, *args, y = 123, **kwargs):
661686
return [x, y, args, kwargs]
662687
[foo(a = 10, b = 3), foo(40, 7, 8, 9, a = 10, y = 3), foo(x=42)]

0 commit comments

Comments
 (0)