Skip to content

Commit 58ac265

Browse files
committed
Add PyUnstable_IsImmortal
1 parent 2ed5ee9 commit 58ac265

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Doc/c-api/object.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,3 +613,19 @@ Object Protocol
613613
614614
.. versionadded:: 3.14
615615
616+
.. c:function:: int PyUnstable_IsImmortal(PyObject *obj)
617+
618+
This function returns ``1`` if *obj* is :term:`immortal`, and ``0`` otherwise.
619+
620+
Immortal objects don't care about reference counting, thus they no-op calls to :c:func:`Py_INCREF`
621+
and :c:func:`Py_DECREF`. Some immutable objects such as literal strings, small integers, or special tuples
622+
might be made immortal as an optimization by the interpreter.
623+
624+
On the :term:`free-threaded <free threading>` build, some objects that support deferred reference counting
625+
(see :c:func:`PyUnstable_Object_EnableDeferredRefcount`) might also be immortalized.
626+
627+
.. note::
628+
629+
Objects that are immortal in one version are not guarunteed to be immortal in another.
630+
631+
.. versionadded:: next

Objects/object.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3155,3 +3155,11 @@ Py_REFCNT(PyObject *ob)
31553155
{
31563156
return _Py_REFCNT(ob);
31573157
}
3158+
3159+
int
3160+
PyUnstable_IsImmortal(PyObject *op)
3161+
{
3162+
/* Checking a reference count requires a thread state */
3163+
_Py_AssertHoldsTstate();
3164+
return _Py_IsImmortal(op);
3165+
}

0 commit comments

Comments
 (0)