Skip to content

gh-128509: Add sys._is_immortal for identifying immortal objects #128510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jan 31, 2025
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
3 changes: 3 additions & 0 deletions Doc/glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@ Glossary
and therefore it is never deallocated while the interpreter is running.
For example, :const:`True` and :const:`None` are immortal in CPython.

Immortal objects can be identified via :func:`sys._is_immortal`, or
via :c:func:`PyUnstable_IsImmortal` in the C API.

immutable
An object with a fixed value. Immutable objects include numbers, strings and
tuples. Such an object cannot be altered. A new object has to
Expand Down
23 changes: 23 additions & 0 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,11 @@ always available. Unless explicitly noted otherwise, all variables are read-only
reflect the actual number of references. Consequently, do not rely
on the returned value to be accurate, other than a value of 0 or 1.

.. impl-detail::

:term:`Immortal <immortal>` objects with a large reference count can be
identified via :func:`_is_immortal`.

.. versionchanged:: 3.12
Immortal objects have very large refcounts that do not match
the actual number of references to the object.
Expand Down Expand Up @@ -1264,6 +1269,24 @@ always available. Unless explicitly noted otherwise, all variables are read-only

.. versionadded:: 3.12

.. function:: _is_immortal(op)

Return :const:`True` if the given object is :term:`immortal`, :const:`False`
otherwise.

.. note::

Objects that are immortal (and thus return ``True`` upon being passed
to this function) are not guaranteed to be immortal in future versions,
and vice versa for mortal objects.

.. versionadded:: next

.. impl-detail::

This function should be used for specialized purposes only.
It is not guaranteed to exist in all implementations of Python.

.. function:: _is_interned(string)

Return :const:`True` if the given string is "interned", :const:`False`
Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,13 @@ sys
which only exists in specialized builds of Python, may now return objects
from other interpreters than the one it's called in.

* Add :func:`sys._is_immortal` for determining if an object is :term:`immortal`.
(Contributed by Peter Bierma in :gh:`128509`.)

* On FreeBSD, :data:`sys.platform` doesn't contain the major version anymore.
It is always ``'freebsd'``, instead of ``'freebsd13'`` or ``'freebsd14'``.


sys.monitoring
--------------

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :func:`sys._is_immortal` for identifying :term:`immortal` objects at
runtime.
32 changes: 31 additions & 1 deletion Python/clinic/sysmodule.c.h

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

18 changes: 18 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,23 @@ sys__is_interned_impl(PyObject *module, PyObject *string)
return PyUnicode_CHECK_INTERNED(string);
}

/*[clinic input]
sys._is_immortal -> bool

op: object
/

Return True if the given object is "immortal" per PEP 683.

This function should be used for specialized purposes only.
[clinic start generated code]*/

static int
sys__is_immortal_impl(PyObject *module, PyObject *op)
/*[clinic end generated code: output=c2f5d6a80efb8d1a input=4609c9bf5481db76]*/
{
return PyUnstable_IsImmortal(op);
}

/*
* Cached interned string objects used for calling the profile and
Expand Down Expand Up @@ -2588,6 +2605,7 @@ static PyMethodDef sys_methods[] = {
SYS__GETFRAMEMODULENAME_METHODDEF
SYS_GETWINDOWSVERSION_METHODDEF
SYS__ENABLELEGACYWINDOWSFSENCODING_METHODDEF
SYS__IS_IMMORTAL_METHODDEF
SYS_INTERN_METHODDEF
SYS__IS_INTERNED_METHODDEF
SYS_IS_FINALIZING_METHODDEF
Expand Down
Loading