Pewarta Proxy

📋 Server Request & Proxy Logs

Loading active proxy settings...
Developed by Khairul Adha (adhexa.id)
Mengambil log server...

⚙️ Pengaturan Proxy Target

Skip to content

[Bug]: weakref.ref() and weakref.proxy() with a callback accept objects that do not support weak references #1256

Open

Description

@jseop-lim

Describe the bug

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)

JDK version

No response

Context configuration

No response

Steps to reproduce

import weakref

def callback(ref):
    pass

for obj in ({}, None):
    try:
        print(type(weakref.proxy(obj, callback)).__name__)
    except TypeError as e:
        print(type(e).__name__, e)

GraalPy master a3009f9:

ProxyType
ProxyType

Expected behavior

CPython 3.13.14 (same on 3.12.13):

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions