diff --git a/mypyc/codegen/emitclass.py b/mypyc/codegen/emitclass.py index a9d973425aa1..30bd66095e5d 100644 --- a/mypyc/codegen/emitclass.py +++ b/mypyc/codegen/emitclass.py @@ -1178,10 +1178,8 @@ def generate_dealloc_for_class( if cl.has_weakref_list(): # Invalidate all weak references to the object, and call their callbacks. # This must happen after the finalizer has run, and before any of the - # attributes are cleared. - # - # TODO: Weak references created *inside* __del__ are not cleared. CPython - # handles these with a second _PyWeakref_ClearWeakRefsNoCallbacks pass. + # attributes are cleared. This also clears any weak references created by + # the finalizer (__del__). if cl.has_weakref_slot(): emitter.emit_line("CPy_ClearWeakrefs((PyObject *)self, &self->weakreflist);") else: diff --git a/mypyc/irbuild/specialize.py b/mypyc/irbuild/specialize.py index d33f15a68bee..bbbe76761398 100644 --- a/mypyc/irbuild/specialize.py +++ b/mypyc/irbuild/specialize.py @@ -1953,6 +1953,8 @@ def check_weakref_target(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Native classes only support weak references if requested explicitly, so a weakref.ref() or weakref.proxy() call would fail at runtime. This is best effort: we only detect calls where the argument has a precise native class type. + Interpreted subclasses always support weak references, so the call may succeed + if the class allows interpreted subclasses. This doesn't generate any code, so translation continues as usual. """ @@ -1962,6 +1964,7 @@ def check_weakref_target(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> isinstance(rtype, RInstance) and not rtype.class_ir.is_trait and not rtype.class_ir.supports_weakrefs() + and not rtype.class_ir.allow_interpreted_subclasses ): builder.error( "Cannot create weak reference to instance of native class " diff --git a/mypyc/test-data/irbuild-weakref.test b/mypyc/test-data/irbuild-weakref.test index e2b7970b29bf..9fe820f8527c 100644 --- a/mypyc/test-data/irbuild-weakref.test +++ b/mypyc/test-data/irbuild-weakref.test @@ -125,7 +125,13 @@ class NonNative: class T: pass -def f(a: Plain, b: Supported, c: SupportedSub, d: NonNative, e: T, g: object) -> None: +@mypyc_attr(allow_interpreted_subclasses=True) +class Open: + pass + +def f( + a: Plain, b: Supported, c: SupportedSub, d: NonNative, e: T, g: object, h: Open +) -> None: weakref.ref(a) # E: Cannot create weak reference to instance of native class "__main__.Plain" \ # N: Add "@mypy_extensions.mypyc_attr(weakrefs=True)" to the class definition to enable support for weak references weakref.proxy(a) # E: Cannot create weak reference to instance of native class "__main__.Plain" \ @@ -136,6 +142,8 @@ def f(a: Plain, b: Supported, c: SupportedSub, d: NonNative, e: T, g: object) -> weakref.ref(d) weakref.ref(e) weakref.ref(g) + # Could be an instance of an interpreted subclass, which supports weak references + weakref.ref(h) [out] [case testWeakrefAttrErrors] diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index b696af790b1d..8a8a30736d5e 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -87,8 +87,11 @@ class FreeList: class WithDel: def __del__(self) -> None: deleted.append(1) + # A weak reference created during finalization must also be cleared. + refs_from_del.append(weakref.ref(self)) deleted: list[int] = [] +refs_from_del: list[weakref.ref[WithDel]] = [] def test_ref() -> None: obj: Optional[C] = C(3) @@ -171,6 +174,8 @@ def test_del() -> None: gc.collect() assert deleted == [1] assert r() is None + assert len(refs_from_del) == 1 + assert refs_from_del[0]() is None def test_no_dangling_refs() -> None: # Regression test: weakrefs to dead objects used to resurrect freed memory. @@ -291,6 +296,14 @@ class Open: def __init__(self) -> None: self.x = 1 +@mypyc_attr(allow_interpreted_subclasses=True) +class OpenNoWeakrefs: + pass + +def ref_open_no_weakrefs(o: OpenNoWeakrefs) -> weakref.ref[OpenNoWeakrefs]: + # No compile error, since 'o' may be an instance of an interpreted subclass + return weakref.ref(o) + def test_implicit_weakref_support() -> None: obj: Optional[Native] = Native() r = weakref.ref(obj) @@ -323,7 +336,11 @@ class Base: [file driver.py] import gc import weakref -from native import Open, test_implicit_weakref_support, test_no_dangling_refs +from native import ( + Open, OpenNoWeakrefs, ref_open_no_weakrefs, test_implicit_weakref_support, + test_no_dangling_refs, +) +from testutil import assertRaises test_implicit_weakref_support() test_no_dangling_refs() @@ -339,3 +356,16 @@ assert r() is obj del obj gc.collect() assert r() is None + +# Interpreted subclasses support weak references even if the native base class doesn't. +class ISubNoWeakrefs(OpenNoWeakrefs): + pass + +obj2 = ISubNoWeakrefs() +r2 = ref_open_no_weakrefs(obj2) +assert r2() is obj2 +del obj2 +gc.collect() +assert r2() is None +with assertRaises(TypeError, "cannot create weak reference to 'OpenNoWeakrefs' object"): + ref_open_no_weakrefs(OpenNoWeakrefs())