Skip to content

Commit

Permalink
pythongh-111506: Implement Py_SET_REFCNT() as inline function
Browse files Browse the repository at this point in the history
Convert Py_SET_REFCNT() and _Py_IsImmortal() static inline functions
to inline functions.

Py_SET_REFCNT() function is now exported as "Py_SET_REFCNT" name in
the stable ABI, instead of "_Py_SetRefcnt".

_Py_IsImmortal() has to be converted to an inline function, since
non-static Py_SET_REFCNT() function cannot call a static
_Py_IsImmortal() function. _Py_IsImmortal() is not part of the stable
ABI.
  • Loading branch information
vstinner committed Dec 6, 2023
1 parent f8c0198 commit 076136e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
1 change: 1 addition & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ static inline Py_ssize_t Py_SIZE(PyObject *ob) {
# define Py_SIZE(ob) Py_SIZE(_PyObject_CAST(ob))
#endif

static inline Py_ALWAYS_INLINE int _Py_IsImmortal(PyObject *op)
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030d0000
inline Py_ALWAYS_INLINE int _Py_IsImmortal(PyObject *op)
{
#if defined(Py_GIL_DISABLED)
return (op->ob_ref_local == _Py_IMMORTAL_REFCNT_LOCAL);
Expand All @@ -343,6 +344,8 @@ static inline Py_ALWAYS_INLINE int _Py_IsImmortal(PyObject *op)
#endif
}
#define _Py_IsImmortal(op) _Py_IsImmortal(_PyObject_CAST(op))
#endif // !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030d0000


static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
return Py_TYPE(ob) == type;
Expand All @@ -352,15 +355,12 @@ static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
#endif


// Py_SET_REFCNT() implementation for stable ABI
PyAPI_FUNC(void) _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt);

static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
// Stable ABI implements Py_SET_REFCNT() as an opaque function call in the
// limited C API version 3.13 and newer.
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030d0000
// Stable ABI implements Py_SET_REFCNT() as a function call
// on limited C API version 3.13 and newer.
_Py_SetRefcnt(ob, refcnt);
PyAPI_DATA(void) Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt);
#else
inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
// This immortal check is for code that is unaware of immortal objects.
// The runtime tracks these objects and we should avoid as much
// as possible having extensions inadvertently change the refcnt
Expand Down Expand Up @@ -394,11 +394,11 @@ static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
ob->ob_ref_shared = _Py_REF_SHARED(refcnt, _Py_REF_MERGED);
}
#endif // Py_GIL_DISABLED
#endif // Py_LIMITED_API+0 < 0x030d0000
}
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
# define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), (refcnt))
#endif
#endif // !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030d0000


static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_stable_abi_ctypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,5 @@
added = '3.13'
[function.PySys_AuditTuple]
added = '3.13'
[function._Py_SetRefcnt]
[function.Py_SET_REFCNT]
added = '3.13'
abi_only = true
8 changes: 8 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2938,3 +2938,11 @@ _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt)
{
Py_SET_REFCNT(ob, refcnt);
}

// Export _Py_IsImmortal() as a regular function
#undef _Py_IsImmortal
extern inline PyAPI_FUNC(int) _Py_IsImmortal(PyObject *op);

// Export Py_SET_REFCNT() as a regular function in stable ABI
#undef Py_SET_REFCNT
extern inline PyAPI_FUNC(void) Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt);
2 changes: 1 addition & 1 deletion PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 076136e

Please sign in to comment.