Skip to content

Commit 485ad40

Browse files
committed
pythongh-111506: Implement Py_SET_REFCNT() as opaque function in limited C API
In the limited C API version 3.13. Py_SET_REFCNT() is now implemented as an opaque function call. Add _Py_SetRefcnt() to the stable ABI.
1 parent bca3305 commit 485ad40

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

Include/object.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,23 @@ static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
329329
#endif
330330

331331

332+
// Py_SET_REFCNT() implementation for stable ABI
333+
PyAPI_FUNC(void) _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt);
334+
332335
static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
336+
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030d0000
337+
// Stable ABI implements Py_SET_REFCNT() as a function call
338+
// on limited C API version 3.13 and newer.
339+
_Py_SetRefcnt(ob, refcnt);
340+
#else
333341
// This immortal check is for code that is unaware of immortal objects.
334342
// The runtime tracks these objects and we should avoid as much
335343
// as possible having extensions inadvertently change the refcnt
336344
// of an immortalized object.
337345
if (_Py_IsImmortal(ob)) {
338346
return;
339347
}
340-
#if !defined(Py_NOGIL)
348+
#ifndef Py_NOGIL
341349
ob->ob_refcnt = refcnt;
342350
#else
343351
if (_Py_IsOwnedByCurrentThread(ob)) {
@@ -354,7 +362,8 @@ static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
354362
ob->ob_ref_local = 0;
355363
ob->ob_ref_shared = _Py_REF_SHARED(refcnt, _Py_REF_MERGED);
356364
}
357-
#endif
365+
#endif // Py_NOGIL
366+
#endif // Py_LIMITED_API+0 < 0x030d0000
358367
}
359368
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
360369
# define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), (refcnt))

Misc/stable_abi.toml

+2
Original file line numberDiff line numberDiff line change
@@ -2480,3 +2480,5 @@
24802480
added = '3.13'
24812481
[function.PyUnicode_AsUTF8]
24822482
added = '3.13'
2483+
[function._Py_SetRefcnt]
2484+
added = '3.13'

Objects/object.c

+8
Original file line numberDiff line numberDiff line change
@@ -2931,3 +2931,11 @@ int Py_IsFalse(PyObject *x)
29312931
{
29322932
return Py_Is(x, Py_False);
29332933
}
2934+
2935+
2936+
// Py_SET_REFCNT() implementation for stable ABI
2937+
void
2938+
_Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt)
2939+
{
2940+
Py_SET_REFCNT(ob, refcnt);
2941+
}

0 commit comments

Comments
 (0)