Skip to content
Merged
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
6 changes: 2 additions & 4 deletions mypyc/codegen/emitclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions mypyc/irbuild/specialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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 "
Expand Down
10 changes: 9 additions & 1 deletion mypyc/test-data/irbuild-weakref.test
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand All @@ -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]
Expand Down
32 changes: 31 additions & 1 deletion mypyc/test-data/run-weakref.test
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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())
Loading