Repository navigation
Add an API for determining if an object is immortal #128509
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancementextension-modulesC modules in the Modules dirC modules in the Modules dir
on Jan 5, 2025 Immortality is an implementation detail, but it still does affect reference counting, and given that it's not very safe to rely on very specific reference counts, we don't want the only way to check for immortality to be via sys.getrefcount. So, given that we already have sys._is_interned, it seems fitting to add a sys._is_immortal alongside it.
What is the use-case to check if something is immortal at python level? Adding it for consistency reason alone isn't enough as it a very specific impl detail of CPython. I would like to know the use case before exposing it at Python level.
Reacted by Erlend E. Aasland, Peter Bierma and Petr ViktorinDifferentiating between "a lot of references" and an immortal object (e.g. for debugging). It's not a good idea to have users rely on an object having a specific number of references to declare it immortal, because we change the immortal reference count, and objects with deferred reference counts also have "very large" reference counts. (FWIW, immortality might not even be done with a fixed refcount someday--that's not something we can deprecate at a
sys.getrefcountlevel.)Maybe we could start by adding a C API for that?
PyUnstable_IsImmortal()or evenPy_IsImmortal()?What is the use-case to check if something is immortal at python level?
Writing unit tests I would say.
Since
sys.getrefcount()is public, IMO it makes sense to addsys._is_immortal(). We should not and cannot hide this "implementation detail". It's not a minor detail, it does confuse a lot of people (me included!).Reacted by Bartosz Sławecki- added a commit that references this issue
on Jan 27, 2025 - added a commit that references this issue
on Jan 31, 2025 - added 4 commits that reference this issue
on Sep 13, 2026
Feature or enhancement
Proposal:
Immortal objects might lead to some unexpected results for users that are (unfortunately) messing with reference count details, as seen in #127191; but currently, there's no way to determine what objects are actually immortal.
Immortality is an implementation detail, but it still does affect reference counting, and given that it's not very safe to rely on very specific reference counts, we don't want the only way to check for immortality to be via
sys.getrefcount. So, given that we already have sys._is_interned, it seems fitting to add asys._is_immortalalongside it.If we go with this, I think it's also a good idea to note it in some of the documentation. For example,
sys.getrefcountsays this:Here, it might be a good idea to mention my proposed
sys._is_immortal, so there's less ambiguity about the returned value.cc @erlend-aasland
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
sys._is_immortalfor identifying immortal objects #128510PyUnstable_IsImmortalfor finding immortal objects #129182