Skip to content
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

Add an API for determining if an object is immortal #128509

Open
ZeroIntensity opened this issue Jan 5, 2025 · 2 comments
Open

Add an API for determining if an object is immortal #128509

ZeroIntensity opened this issue Jan 5, 2025 · 2 comments
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@ZeroIntensity
Copy link
Member

ZeroIntensity commented Jan 5, 2025

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 a sys._is_immortal alongside 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.getrefcount says this:

some objects are immortal and have a very high refcount that does not reflect the actual number of references.

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

@ZeroIntensity ZeroIntensity added type-feature A feature request or enhancement extension-modules C modules in the Modules dir labels Jan 5, 2025
@kumaraditya303
Copy link
Contributor

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.

@ZeroIntensity
Copy link
Member Author

ZeroIntensity commented Jan 5, 2025

Differentiating 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.getrefcount level.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants