Skip to content

Commit 4ca8e17

Browse files
craigbarrattraman325
authored andcommitted
fixed global context setting on function call; fixes #58
1 parent 47ee774 commit 4ca8e17

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

custom_components/pyscript/eval.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,11 +665,18 @@ async def call(self, ast_ctx, *args, **kwargs):
665665
# switch to the global symbol table in the global context
666666
# where the function was defined
667667
#
668-
prev_sym_table = [ast_ctx.global_sym_table, ast_ctx.sym_table, ast_ctx.sym_table_stack]
668+
prev_sym_table = [
669+
ast_ctx.global_sym_table,
670+
ast_ctx.sym_table,
671+
ast_ctx.sym_table_stack,
672+
ast_ctx.global_ctx,
673+
]
669674
ast_ctx.global_sym_table = self.global_ctx.get_global_sym_table()
670675
ast_ctx.sym_table_stack = [ast_ctx.global_sym_table]
676+
ast_ctx.global_ctx = self.global_ctx
671677
else:
672678
ast_ctx.sym_table_stack.append(ast_ctx.sym_table)
679+
prev_sym_table = None
673680
ast_ctx.sym_table = sym_table
674681
code_str, code_list = ast_ctx.code_str, ast_ctx.code_list
675682
ast_ctx.code_str, ast_ctx.code_list = self.code_str, self.code_list
@@ -689,8 +696,13 @@ async def call(self, ast_ctx, *args, **kwargs):
689696
break
690697
ast_ctx.curr_func = prev_func
691698
ast_ctx.code_str, ast_ctx.code_list = code_str, code_list
692-
if ast_ctx.global_ctx != self.global_ctx:
693-
ast_ctx.global_sym_table, ast_ctx.sym_table, ast_ctx.sym_table_stack = prev_sym_table
699+
if prev_sym_table is not None:
700+
(
701+
ast_ctx.global_sym_table,
702+
ast_ctx.sym_table,
703+
ast_ctx.sym_table_stack,
704+
ast_ctx.global_ctx,
705+
) = prev_sym_table
694706
else:
695707
ast_ctx.sym_table = ast_ctx.sym_table_stack.pop()
696708
return val

0 commit comments

Comments
 (0)