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
Add Py_SETREF() and Py_XSETREF() macros to the limited C API 3.12 #99574
Py_CLEAR() is already part of the limited C API (since Python 3.2) and is very similar to Py_SETREF(). Py_CLEAR() rely on the C preprocessor to get a reference to a variable, so the variable can be set to NULL without having to pass a pointer to the variable. The usage is Py_CLEAR(var), rather than Py_CLEAR(&var). The Py_SETREF() macro does the same, but the value is passed by the caller, it's not always NULL.
Multiple projects use these macros. Code search in PyPI top 5000 projects using (Py_SETREF|Py_XSETREF) regex. 16 projects (177 matching lines in total):
See also issue #98724 and its PR #99100 which discuss Py_CLEAR(), Py_SETREF() and Py_XSETREF() in length.
I would prefer exposing functions (taking a reference to a variable, &var) which can be used when C macros cannot be used, for example in Rust or other programming languages. But adding functions is a different which can be addressed separately.
Activity
vstinner commentedon Nov 18, 2022
Obviously, since Py_SETREF() and Py_XSETREF() are macros, they are not part of the stable ABI. I only propose to add them to the limited C API.
serhiy-storchaka commentedon Nov 21, 2022
Since the previous attempt to add them failed, should not it be discussed in more public place?
vstinner commentedon Oct 5, 2026
Follow-up in 2026 (Python 3.16): issue gh-158842.