Add sys._is_immortal() and inspect.isimmortal()

It would be good if we can access the information of immortability (PEP 683) from the pure Python layer directly as it seems that the implementation of immortal objects with fixed refcounts has become stabilized. I propose adding new functions that check objects’ immortability to the standard library: sys._is_immortal() and inspect.isimmortal(). They will help us to write a script that makes a brief check of refcount excluding immortal objects.

Here are the expected behaviors:

>>> import sys
>>> sys._is_immortal(None)
True
>>> sys._is_immortal([1, 2, 3, 4])
False

>>> import inspect
>>> inspect.isimmortal(True) # same as sys._is_immortal()
True
>>> inspect.isimmortal(1000)
False

The implementation is simple; just wraps the _Py_IsImmortal() macro (like this). The names of the functions follow convention. There are still some other candidates like
sys.is_immortal (without sunder), sys._is_immortal_object (long but more explicit) etc… Another possibility is adding it to gc module rather than sys.

3 Likes