8000 gh-128509: Add `sys._is_immortal` for identifying immortal objects by ZeroIntensity · Pull Request #128510 · python/cpython · GitHub
[go: up one dir, main page]

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 11 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
2 changes: 2 additions & 0 deletions Doc/glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,8 @@ 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`.

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
22 changes: 22 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,23 @@ 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
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,9 @@ 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`.)

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

Expand Down
14 changes: 10 additions & 4 deletions Lib/test/test_capi/test_immortal.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import unittest
from test.support import import_helper
import sys

_testcapi = import_helper.import_module('_testcapi')
_testinternalcapi = import_helper.import_module('_testinternalcapi')


class TestUnstableCAPI(unittest.TestCase):
def test_immortal(self):
class TestImmortalAPI(unittest.TestCase):
def immortal_checking(self, func):
# Not extensive
known_immortals = (True, False, None, 0, ())
for immortal in known_immortals:
with self.subTest(immortal=immortal):
self.assertTrue(_testcapi.is_immortal(immortal))
self.assertTrue(func(immortal))

# Some arbitrary mutable objects
non_immortals = (object(), self, [object()])
for non_immortal in non_immortals:
with self.subTest(non_immortal=non_immortal):
self.assertFalse(_testcapi.is_immortal(non_immortal))
self.assertFalse(func(non_immortal))

def test_unstable_c_api(self):
self.immortal_checking(_testcapi.is_immortal)
# CRASHES _testcapi.is_immortal(NULL)

def test_sys(self):
self.immortal_checking(sys._is_immortal)
Copy link
Member

Choose a reason for hiding this comment

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

sys functions must be checked in test_sys.

Copy link
Member Author

Choose a reason for hiding this comment

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

I intentionally moved the test from test_sys to test_immortal so we could re-use the test case, but I've reverted it now. Are you ok with the redundancy?

Copy link
Member

Choose a reason for hiding this comment

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

I'm ok with redundancy. test_capi can be skipped if _testcapi is missing. It's not the case for test_sys. Both tests are useful.



class TestInternalCAPI(unittest.TestCase):

Expand Down
9E88
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.
30 changes: 29 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.

16 changes: 16 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,21 @@ sys_intern_impl(PyObject *module, PyObject *s)
}
}

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

op: object
/

Return True if the given object is "immortal" per PEP 683.
[clinic start generated code]*/

static int
sys__is_immortal_impl(PyObject *module, PyObject *op)
/*[clinic end generated code: output=c2f5d6a80efb8d1a input=83733fc356c78475]*/
{
return _Py_IsImmortal(op);
Copy link
Member

Choose a reason for hiding this comment

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

Why not using PyUnstable_IsImmortal()?

Copy link
Member Author

Choose a reason for hiding this comment

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

The two are pretty similar. Would you prefer I use the unstable API?

Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to use the public unstable API, yes.

}

Copy link
Member

Choose a reason for hiding this comment

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

unrelated change, I suggest to leave this empty line :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch!

/*[clinic input]
sys._is_interned -> bool
Expand Down Expand Up @@ -2590,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
0