Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
entirely remove PyWeakRef_GET_OBJECT
  • Loading branch information
picnixz committed May 8, 2025
commit 9a4b77e4793e1f307c70e11538c17bcc8ed6b57f
18 changes: 0 additions & 18 deletions Include/cpython/weakrefobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,3 @@ PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);

// Test if a weak reference is dead.
PyAPI_FUNC(int) PyWeakref_IsDead(PyObject *ref);

/* removed in 3.15, kept for stable ABI compatibility */
Py_DEPRECATED(3.13) static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_obj)
{
PyWeakReference *ref = _PyWeakref_CAST(ref_obj);
PyObject *obj = ref->wr_object;
// Explanation for the Py_REFCNT() check: when a weakref's target is part
// of a long chain of deallocations which triggers the trashcan mechanism,
// clearing the weakrefs can be delayed long after the target's refcount
// has dropped to zero. In the meantime, code accessing the weakref will
// be able to "see" the target object even though it is supposed to be
// unreachable. See issue gh-60806.
if (Py_REFCNT(obj) > 0) {
return obj;
}
return Py_None;
}
#define PyWeakref_GET_OBJECT(ref) PyWeakref_GET_OBJECT(_PyObject_CAST(ref))
13 changes: 0 additions & 13 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2249,25 +2249,12 @@ test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
ref = PyWeakref_GetObject(weakref); // borrowed ref
assert(ref == obj);

// test PyWeakref_GET_OBJECT(), reference is alive
_Py_COMP_DIAG_PUSH
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
ref = PyWeakref_GET_OBJECT(weakref); // borrowed ref
_Py_COMP_DIAG_POP
assert(ref == obj);

// delete the referenced object: clear the weakref
assert(Py_REFCNT(obj) == 1);
Py_DECREF(obj);

assert(PyWeakref_IsDead(weakref));

// test PyWeakref_GET_OBJECT(), reference is dead
_Py_COMP_DIAG_PUSH
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
assert(PyWeakref_GET_OBJECT(weakref) == Py_None);
_Py_COMP_DIAG_POP

// test PyWeakref_GetRef(), reference is dead
ref = UNINITIALIZED_PTR;
assert(PyWeakref_GetRef(weakref, &ref) == 0);
Expand Down