Closed
Description
PyWeakref_GET_OBJECT() and PyWeakref_GetObject() return a borrowed reference to the object, or a borrowed reference to None if the object has been finalized. This API is error-prone and not easy to use.
This API was discussed in 2016: https://mail.python.org/archives/list/python-dev@python.org/thread/6BIPI4MCMAZFLJNIZB4JLTBW2COCACQQ/
I propose adding a new PyWeakref_GetRef() C API function which returns a new strong reference to the object, or NULL if the object has been finalized.
Linked PRs
- gh-105927: Refactor weakrefobject.c #105928
- gh-105927: Add _PyWeakref_GET_REF() internal function #105929
- gh-105927: Add PyWeakref_GetRef() function #105932
- gh-105927: _abc and _thread use PyWeakref_GetRef() #105961
- gh-105927: type_from_ref() uses _PyWeakref_GET_REF() #105963
- gh-105927: _ctypes use PyWeakref_GetRef() #105964
- gh-105927: _ssl uses _PyWeakref_GET_REF() #105965
- gh-105927: Fix test_weakref_capi() refleak #105966
- gh-105927: finalize_modules_clear_weaklist() uses _PyWeakref_GET_REF() #105971
- gh-105927: Add _PyWeakref_IS_DEAD() function #105992
- gh-105927: Avoid calling PyWeakref_GET_OBJECT() #105997
- gh-105927: PyImport_AddModule() uses _PyWeakref_GET_REF() #106001
- gh-105927: _ssl GET_SOCKET() uses _PyWeakref_GET_REF() #106002
- gh-105927: Deprecate PyWeakref_GetObject() function #106006
- gh-105927: Remove _PyWeakref_GetWeakrefCount() #106007
- gh-105927: PyWeakref_GetRef() returns 1 on success #106561
- gh-105927: Limit PyWeakref_GetRef() to limited C API 3.13 #117091