Skip to content

Commit 9ba281d

Browse files
gh-128509: Add sys._is_immortal for identifying immortal objects (#128510)
Co-authored-by: Kumar Aditya <[email protected]>
1 parent 60a8541 commit 9ba281d

File tree

6 files changed

+81
-1
lines changed

6 files changed

+81
-1
lines changed

Doc/glossary.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,9 @@ Glossary
658658
and therefore it is never deallocated while the interpreter is running.
659659
For example, :const:`True` and :const:`None` are immortal in CPython.
660660

661+
Immortal objects can be identified via :func:`sys._is_immortal`, or
662+
via :c:func:`PyUnstable_IsImmortal` in the C API.
663+
661664
immutable
662665
An object with a fixed value. Immutable objects include numbers, strings and
663666
tuples. Such an object cannot be altered. A new object has to

Doc/library/sys.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,11 @@ always available. Unless explicitly noted otherwise, all variables are read-only
855855
reflect the actual number of references. Consequently, do not rely
856856
on the returned value to be accurate, other than a value of 0 or 1.
857857

858+
.. impl-detail::
859+
860+
:term:`Immortal <immortal>` objects with a large reference count can be
861+
identified via :func:`_is_immortal`.
862+
858863
.. versionchanged:: 3.12
859864
Immortal objects have very large refcounts that do not match
860865
the actual number of references to the object.
@@ -1264,6 +1269,24 @@ always available. Unless explicitly noted otherwise, all variables are read-only
12641269

12651270
.. versionadded:: 3.12
12661271

1272+
.. function:: _is_immortal(op)
1273+
1274+
Return :const:`True` if the given object is :term:`immortal`, :const:`False`
1275+
otherwise.
1276+
1277+
.. note::
1278+
1279+
Objects that are immortal (and thus return ``True`` upon being passed
1280+
to this function) are not guaranteed to be immortal in future versions,
1281+
and vice versa for mortal objects.
1282+
1283+
.. versionadded:: next
1284+
1285+
.. impl-detail::
1286+
1287+
This function should be used for specialized purposes only.
1288+
It is not guaranteed to exist in all implementations of Python.
1289+
12671290
.. function:: _is_interned(string)
12681291

12691292
Return :const:`True` if the given string is "interned", :const:`False`

Doc/whatsnew/3.14.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,13 @@ sys
649649
which only exists in specialized builds of Python, may now return objects
650650
from other interpreters than the one it's called in.
651651

652+
* Add :func:`sys._is_immortal` for determining if an object is :term:`immortal`.
653+
(Contributed by Peter Bierma in :gh:`128509`.)
654+
652655
* On FreeBSD, :data:`sys.platform` doesn't contain the major version anymore.
653656
It is always ``'freebsd'``, instead of ``'freebsd13'`` or ``'freebsd14'``.
654657

658+
655659
sys.monitoring
656660
--------------
657661

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :func:`sys._is_immortal` for identifying :term:`immortal` objects at
2+
runtime.

Python/clinic/sysmodule.c.h

Lines changed: 31 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/sysmodule.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,23 @@ sys__is_interned_impl(PyObject *module, PyObject *string)
972972
return PyUnicode_CHECK_INTERNED(string);
973973
}
974974

975+
/*[clinic input]
976+
sys._is_immortal -> bool
977+
978+
op: object
979+
/
980+
981+
Return True if the given object is "immortal" per PEP 683.
982+
983+
This function should be used for specialized purposes only.
984+
[clinic start generated code]*/
985+
986+
static int
987+
sys__is_immortal_impl(PyObject *module, PyObject *op)
988+
/*[clinic end generated code: output=c2f5d6a80efb8d1a input=4609c9bf5481db76]*/
989+
{
990+
return PyUnstable_IsImmortal(op);
991+
}
975992

976993
/*
977994
* Cached interned string objects used for calling the profile and
@@ -2588,6 +2605,7 @@ static PyMethodDef sys_methods[] = {
25882605
SYS__GETFRAMEMODULENAME_METHODDEF
25892606
SYS_GETWINDOWSVERSION_METHODDEF
25902607
SYS__ENABLELEGACYWINDOWSFSENCODING_METHODDEF
2608+
SYS__IS_IMMORTAL_METHODDEF
25912609
SYS_INTERN_METHODDEF
25922610
SYS__IS_INTERNED_METHODDEF
25932611
SYS_IS_FINALIZING_METHODDEF

0 commit comments

Comments
 (0)