Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions custom_components/pyscript/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,8 @@ async def ast_functiondef(self, arg, async_func=False):
if len(pyscript_compile.keywords) > 0:
raise TypeError(f"@{dec_name}() takes no keyword arguments")
arg.decorator_list = other_dec
# in a function, the name is a local variable whose value is kept in a cell
# (EvalLocalVar); exec() below replaces the cell, so keep it to put back
local_var = None
if arg.name in self.sym_table and isinstance(self.sym_table[arg.name], EvalLocalVar):
local_var = self.sym_table[arg.name]
Expand All @@ -1234,12 +1236,16 @@ async def executor_wrap(*args, **kwargs):

return executor_wrap

self.sym_table[arg.name] = executor_wrap_factory(func)
func = executor_wrap_factory(func)
else:
raise TypeError("@pyscript_executor() needs a regular, not async, function")
if local_var:

if local_var is not None:
# closures may already hold the cell, so put it back and store func in it
self.sym_table[arg.name] = local_var
self.sym_table[arg.name].set(func)
local_var.set(func)
else:
self.sym_table[arg.name] = func
return

func = EvalFunc(arg, self.code_list, self.code_str, self.global_ctx, async_func)
Expand Down
27 changes: 22 additions & 5 deletions tests/test_unit_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,21 +1217,38 @@ def func_trig():
"""
import threading

# will run in the same thread, and return a different thread ident
# will run in the same thread, and return the same thread ident
@pyscript_compile()
def func1():
return threading.get_ident()

# will run in a different thread, and return a different thread ident
@pyscript_compile()
@pyscript_executor()
def func2():
return threading.get_ident()

[threading.get_ident() == func1(), threading.get_ident() != func2()]
# defined inside a function or a method, will also run in a different thread
def outer():
@pyscript_executor
def inner():
return threading.get_ident()
return inner()

class Worker:
def run(self):
@pyscript_executor
def inner():
return threading.get_ident()
return inner()

[True, True]
[
threading.get_ident() == func1(),
threading.get_ident() != func2(),
threading.get_ident() != outer(),
threading.get_ident() != Worker().run(),
]
""",
[True, True],
[True, True, True, True],
],
[
"""
Expand Down
Loading