You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: weakref.ref() and weakref.proxy() with a callback accept objects that do not support weak references #1256
weakref.ref(obj, callback) and weakref.proxy(obj, callback) succeed for dict, list, int, str, None and other types without weak reference support. Without a callback, or with None, both raise TypeError as CPython does.
Operating system
macOS
CPU architecture
ARM64
GraalPy version
master a3009f9 (Python 3.13.14); also reproduced on 25.0.2 (Python 3.12.8) and 25.4.4.1.1 (Python 3.13.14)
TypeError cannot create weak reference to 'dict' object
TypeError cannot create weak reference to 'NoneType' object
Stack trace
Additional context
The proxy of None raises ReferenceError: weakly-referenced object no longer exists on first use, because a referent of None reads as a dead reference.
Root Cause
ReferenceTypeNode.refType rejects a builtin class whose getWeaklistoffset() is 0 (ReferenceTypeBuiltins.java#L140-L148), but it only matches a None callback. refTypeWithCallback, which weakref.proxy() reaches through NewProxyTypeNode, creates the reference without that check (#L160-L167). CPython's PyWeakref_NewRef and PyWeakref_NewProxy both go through get_or_create_weakref, which checks _PyType_SUPPORTS_WEAKREFS before it looks at the callback (weakrefobject.c#L405-L415).
Fix Suggestion
Add the same getWeaklistoffset() == 0 check to refTypeWithCallback. getBuiltinTypeWeaklistoffset (WeakRefModuleBuiltins.java#L93-L137) needs entries for collections.deque, array.array, mmap.mmap, _contextvars.Context, _queue.SimpleQueue, _struct.Struct and the typing.TypeVar family first, since these support weak references in CPython and today work on GraalPy only when a callback is given.
Activity