Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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.

7 changes: 4 additions & 3 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,10 @@ New Features
Porting to Python 3.14
----------------------

* In the limited C API 3.14 and newer, :c:func:`Py_TYPE` is now implemented as
an opaque function call to hide implementation details.
(Contributed by Victor Stinner in :gh:`120600`.)
* In the limited C API 3.14 and newer, :c:func:`Py_TYPE` and
:c:func:`Py_REFCNT` are now implemented as an opaque function call to hide
implementation details.
(Contributed by Victor Stinner in :gh:`120600` and :gh:`124127`.)


Deprecated
Expand Down
34 changes: 21 additions & 13 deletions Include/refcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,29 @@ check by comparing the reference count field to the immortality reference count.
#endif // Py_GIL_DISABLED


static inline Py_ssize_t Py_REFCNT(PyObject *ob) {
#if !defined(Py_GIL_DISABLED)
return ob->ob_refcnt;
// Py_REFCNT() implementation for the stable ABI
PyAPI_FUNC(Py_ssize_t) Py_REFCNT(PyObject *ob);

#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030e0000
// Stable ABI implements Py_REFCNT() as a function call
// on limited C API version 3.14 and newer.
#else
uint32_t local = _Py_atomic_load_uint32_relaxed(&ob->ob_ref_local);
if (local == _Py_IMMORTAL_REFCNT_LOCAL) {
return _Py_IMMORTAL_REFCNT;
static inline Py_ssize_t _Py_REFCNT(PyObject *ob) {
#if !defined(Py_GIL_DISABLED)
return ob->ob_refcnt;
#else
uint32_t local = _Py_atomic_load_uint32_relaxed(&ob->ob_ref_local);
if (local == _Py_IMMORTAL_REFCNT_LOCAL) {
return _Py_IMMORTAL_REFCNT;
}
Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&ob->ob_ref_shared);
return _Py_STATIC_CAST(Py_ssize_t, local) +
Py_ARITHMETIC_RIGHT_SHIFT(Py_ssize_t, shared, _Py_REF_SHARED_SHIFT);
#endif
}
Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&ob->ob_ref_shared);
return _Py_STATIC_CAST(Py_ssize_t, local) +
Py_ARITHMETIC_RIGHT_SHIFT(Py_ssize_t, shared, _Py_REF_SHARED_SHIFT);
#endif
}
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
# define Py_REFCNT(ob) Py_REFCNT(_PyObject_CAST(ob))
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
Copy link
Member

@encukou encukou Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This #if is now redundant, right?

Copy link
Member Author

@vstinner vstinner Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still needed. (Correct me if I'm wrong.)

# define Py_REFCNT(ob) _Py_REFCNT(_PyObject_CAST(ob))
#endif
#endif


Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_stable_abi_ctypes.py

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
In the limited C API 3.14 and newer, :c:func:`Py_REFCNT` is now implemented
as an opaque function call to hide implementation details. Patch by Victor
Stinner.
2 changes: 2 additions & 0 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,8 @@

[function.Py_TYPE]
added = '3.14'
[function.Py_REFCNT]
added = '3.14'
[function.PyIter_NextItem]
added = '3.14'
[function.PyLong_FromInt32]
Expand Down
12 changes: 11 additions & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -3044,7 +3044,17 @@ Py_GetConstantBorrowed(unsigned int constant_id)

// Py_TYPE() implementation for the stable ABI
#undef Py_TYPE
PyTypeObject* Py_TYPE(PyObject *ob)
PyTypeObject*
Py_TYPE(PyObject *ob)
{
return _Py_TYPE(ob);
}


// Py_REFCNT() implementation for the stable ABI
#undef Py_REFCNT
Py_ssize_t
Py_REFCNT(PyObject *ob)
{
return _Py_REFCNT(ob);
}
1 change: 1 addition & 0 deletions PC/python3dll.c

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

Loading