8000 gh-133644: remove `PyWeakref_GetObject` and `PyWeakref_GET_OBJECT` by picnixz · Pull Request #133657 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-133644: remove PyWeakref_GetObject and PyWeakref_GET_OBJECT #133657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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
Loading
0