Skip to content

Commit adc76f2

Browse files
committed
very initial attempt at native compiled code; see #71
1 parent dc72fec commit adc76f2

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

custom_components/pyscript/eval.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,22 @@ async def ast_classdef(self, arg):
972972

973973
async def ast_functiondef(self, arg):
974974
"""Evaluate function definition."""
975+
for dec in arg.decorator_list:
976+
if isinstance(dec, ast.Name):
977+
if dec.id != "pyscript_compile":
978+
continue
979+
arg.decorator_list = []
980+
local_var = None
981+
if arg.name in self.sym_table and isinstance(self.sym_table[arg.name], EvalLocalVar):
982+
local_var = self.sym_table[arg.name]
983+
code = compile(ast.Module(body=[arg], type_ignores=[]), filename=self.filename, mode="exec")
984+
exec(code, self.global_sym_table, self.sym_table) # pylint: disable=exec-used
985+
if local_var:
986+
func = self.sym_table[arg.name]
987+
self.sym_table[arg.name] = local_var
988+
self.sym_table[arg.name].set(func)
989+
return
990+
975991
func = EvalFunc(arg, self.code_list, self.code_str, self.global_ctx)
976992
await func.eval_defaults(self)
977993
await func.eval_decorators(self)
@@ -1374,7 +1390,7 @@ async def ast_name(self, arg):
13741390
# a two-dot name for state.attr needs to exist
13751391
#
13761392
if num_dots == 1 or (num_dots == 2 and State.exist(arg.id)):
1377-
return await State.get(arg.id)
1393+
return State.get(arg.id)
13781394
#
13791395
# Couldn't find it, so return just the name wrapped in EvalName to
13801396
# distinguish from a string variable value. This is to support

custom_components/pyscript/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def exist(cls, var_name):
249249
return False
250250

251251
@classmethod
252-
async def get(cls, var_name):
252+
def get(cls, var_name):
253253
"""Get a state variable value or attribute from hass."""
254254
parts = var_name.split(".")
255255
if len(parts) != 2 and len(parts) != 3:

tests/test_state.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async def test_service_call(hass):
2121
State.init(hass)
2222
await State.get_service_params()
2323

24-
func = await State.get("test.entity.test")
24+
func = State.get("test.entity.test")
2525
await func(context=Context(id="test"), blocking=True, limit=1, other_service_data="test")
2626
assert call.called
2727
assert call.call_args[0] == (
@@ -32,7 +32,7 @@ async def test_service_call(hass):
3232
assert call.call_args[1] == {"context": Context(id="test"), "blocking": True, "limit": 1}
3333
call.reset_mock()
3434

35-
func = await State.get("test.entity.test")
35+
func = State.get("test.entity.test")
3636
await func(context=Context(id="test"), blocking=False, other_service_data="test")
3737
assert call.called
3838
assert call.call_args[0] == (

0 commit comments

Comments
 (0)