From 7fe51f1ca06003e7b46838a7b399f5bbab0ab884 Mon Sep 17 00:00:00 2001 From: Craig Barratt <19445341+craigbarratt@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:28:00 -0800 Subject: [PATCH] hold a strong reference to the instance in bound methods A bound method held only a weak reference to its instance, so a method called on a temporary instance, such as Foo().method(), got self as None once the instance was freed before the call. The weak reference dates from when every method was bound and stored on the instance at construction, which would have made a reference cycle. Since #790, a new bound method is made on each attribute access and isn't stored on the instance, so hold a strong reference, as Python's bound methods do. Co-Authored-By: Claude Opus 5.5 --- custom_components/pyscript/eval.py | 17 +++++++------- tests/test_unit_eval.py | 37 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/custom_components/pyscript/eval.py b/custom_components/pyscript/eval.py index 76abac3..88d24d4 100644 --- a/custom_components/pyscript/eval.py +++ b/custom_components/pyscript/eval.py @@ -15,7 +15,6 @@ import traceback from types import ModuleType, TracebackType from typing import TYPE_CHECKING, Any -import weakref import yaml @@ -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.""" @@ -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: diff --git a/tests/test_unit_eval.py b/tests/test_unit_eval.py index c9d1441..0ef8d9b 100644 --- a/tests/test_unit_eval.py +++ b/tests/test_unit_eval.py @@ -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