Skip to content
Open
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
17 changes: 8 additions & 9 deletions custom_components/pyscript/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import traceback
from types import ModuleType, TracebackType
from typing import TYPE_CHECKING, Any
import weakref

import yaml

Expand Down Expand Up @@ -879,10 +878,10 @@ def __get__(self, obj, objtype=None):
"""Support descriptor protocol so class attributes bind to instances."""
if obj is None:
return self
# we use weak references when we bind the method calls to the instance inst;
# otherwise these self references cause the object to not be deleted until
# it is later garbage collected
return EvalFuncVarClassInst(self.func, self.ast_ctx, weakref.ref(obj))
# like a Python bound method, hold a strong reference to the instance, so a method
# of a temporary instance (eg, Foo().method()) still has it when called; this
# doesn't create a cycle, since a new bound method is made on each attribute access
return EvalFuncVarClassInst(self.func, self.ast_ctx, obj)

def __del__(self):
"""On deletion, stop any triggers for this function."""
Expand All @@ -897,19 +896,19 @@ async def __call__(self, *args, **kwargs):
class EvalFuncVarClassInst(EvalFuncVar):
"""Class for a callable pyscript class instance function."""

def __init__(self, func: EvalFunc, ast_ctx: "AstEval", class_inst_weak: weakref.ReferenceType) -> None:
def __init__(self, func: EvalFunc, ast_ctx: "AstEval", class_inst: Any) -> None:
"""Initialize instance with given EvalFunc function."""
super().__init__(func)
self.ast_ctx = ast_ctx
self.class_inst_weak = class_inst_weak
self.class_inst = class_inst

async def call(self, ast_ctx, *args, **kwargs):
"""Call the EvalFunc function."""
return await self.func.call(ast_ctx, self.class_inst_weak(), *args, **kwargs)
return await self.func.call(ast_ctx, self.class_inst, *args, **kwargs)

async def __call__(self, *args, **kwargs):
"""Call the function using our saved ast ctx and class instance."""
return await self.func.call(self.ast_ctx, self.class_inst_weak(), *args, **kwargs)
return await self.func.call(self.ast_ctx, self.class_inst, *args, **kwargs)


class AstEval:
Expand Down
37 changes: 37 additions & 0 deletions tests/test_unit_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,43 @@ def __del__(self):
],
[
"""
class Temp:
def __init__(self):
self.v = 4

def name(self):
return type(self).__name__

def value(self):
return self.v

method = Temp().value
[Temp().name(), Temp().value(), method()]
""",
["Temp", 4, 4],
],
[
"""
x = []
class Temp:
def method(self):
return 1

@pyscript_compile
def __del__(self):
x.append(-1)

Temp().method()
r1 = list(x)
method = Temp().method
r2 = list(x)
del method
[r1, r2, x]
""",
[[-1], [-1], [-1, -1]],
],
[
"""
def func1(m):
def func2():
m[0] += 1
Expand Down
Loading