Skip to content

Commit bfce98f

Browse files
committed
lambda function are now compiled so they are regular functions
1 parent a7cb189 commit bfce98f

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

custom_components/pyscript/eval.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,13 +1042,21 @@ async def ast_functiondef(self, arg):
10421042
sym_table[name] = func_var
10431043

10441044
async def ast_lambda(self, arg):
1045-
"""Evaluate lambda definition."""
1046-
funcdef = ast.FunctionDef(
1047-
args=arg.args, body=[ast.Return(value=arg.body)], name="lambda", decorator_list=None,
1045+
"""Evaluate lambda definition by compiling a regular function."""
1046+
name = "__lambda_defn_temp__"
1047+
await self.aeval(
1048+
ast.FunctionDef(
1049+
args=arg.args,
1050+
body=[ast.Return(value=arg.body, lineno=arg.body.lineno, col_offset=arg.body.col_offset)],
1051+
name=name,
1052+
decorator_list=[ast.Name(id="pyscript_compile", ctx=ast.Load())],
1053+
lineno=arg.col_offset,
1054+
col_offset=arg.col_offset,
1055+
)
10481056
)
1049-
func = EvalFunc(funcdef, self.code_list, self.code_str, self.global_ctx)
1050-
await func.eval_defaults(self)
1051-
return EvalFuncVar(func)
1057+
func = self.sym_table[name]
1058+
del self.sym_table[name]
1059+
return func
10521060

10531061
async def ast_asyncfunctiondef(self, arg):
10541062
"""Evaluate async function definition."""

docs/reference.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,12 +788,14 @@ Other Function Decorators
788788

789789
By default in pyscript all functions are async, so they cannot be used in ``task.executor``,
790790
as callbacks or methods in python packages that expect regular functions, or used with built-in
791-
functions like ``map`` or special class methods that are called by python internals (eg,
792-
``__getattr__`` or ``__del__``).
791+
functions like ``filter``, ``map`` or special class methods that are called by python internals
792+
(eg, ``__getattr__`` or ``__del__``).
793793

794794
The ``@pyscript_compile`` decorator causes the function to be treated as native Python and
795795
compiled, which results in a regular python function being defined, and it will run at full
796-
compiled speed.
796+
compiled speed. A ``lambda`` function is automatically compiled so it behaves like a regular
797+
python ``lambda`` function (which means the lambda function body cannot contain pyscript
798+
features).
797799

798800
For example:
799801

tests/test_unit_eval.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
["d = {'x': 123}; d['x'] += 10; d", {"x": 133}],
124124
["d = [20, 30]; d[1] += 10; d", [20, 40]],
125125
["func = lambda m=2: 2 * m; [func(), func(3), func(m=4)]", [4, 6, 8]],
126+
["thres = 1; list(filter(lambda x: x < thres, range(-5, 5)))", [-5, -4, -3, -2, -1, 0]],
126127
["y = 5; y = y + (x := 2 * y); [x, y]", [10, 15]],
127128
["Foo = type('Foo', (), {'x': 100}); Foo.x = 10; Foo.x", 10],
128129
["Foo = type('Foo', (), {'x': 100}); Foo.x += 10; Foo.x", 110],

0 commit comments

Comments
 (0)