GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add an API for determining if an object is immortal · Issue #128509 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Add an API for determining if an object is immortal#128509
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement

Description

@ZeroIntensity
ZeroIntensity
opened on Jan 5, 2025
Issue body actions

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

  • gh-128509: Add sys._is_immortal for identifying immortal objects #128510
  • gh-128509: Add PyUnstable_IsImmortal for finding immortal objects #129182
  • gh-128509: Add tests on sys._is_immortal() #157376
  • gh-128509: Use bytes singleton in marshal #157398
  • [3.14] gh-128509: Use bytes singletons in marshal (GH-157398) #157432
  • [3.15] gh-128509: Use bytes singletons in marshal (GH-157398) #157433
  • [3.15] gh-128509: Add tests on sys._is_immortal() (GH-157376) #157445
  • [3.14] gh-128509: Add tests on sys._is_immortal() (GH-157376) #157446

Activity

  1. ZeroIntensity
    added
    type-featureA feature request or enhancementA feature request or enhancement
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Jan 5, 2025
  2. kumaraditya303 commented on Jan 5, 2025

    @kumaraditya303
    kumaraditya303
    on Jan 5, 2025
    Contributor
    More actions

    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 Viktorin
  3. ZeroIntensity commented on Jan 5, 2025

    @ZeroIntensity
    ZeroIntensity
    on Jan 5, 2025
    MemberAuthor
    More actions

    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.)

  4. vstinner commented on Jan 22, 2025

    @vstinner
    vstinner
    on Jan 22, 2025
    Member
    More actions

    Maybe we could start by adding a C API for that? PyUnstable_IsImmortal() or even Py_IsImmortal()?

  5. vstinner commented on Jan 22, 2025

    @vstinner
    vstinner
    on Jan 22, 2025
    Member
    More actions

    @kumaraditya303:

    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 add sys._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
  6. encukou
    added a commit that references this issue on Jan 27, 2025

    gh-128509: Add `PyUnstable_IsImmortal` for finding immortal objects (G…

    3fb5f6e
  7. kumaraditya303
    added a commit that references this issue on Jan 31, 2025

    gh-128509: Add `sys._is_immortal` for identifying immortal objects (#…

    9ba281d
  8. ZeroIntensity
    closed this as completedon Jan 31, 2025
  9. srinivasreddy
    added a commit that references this issue on Feb 7, 2025

    pythongh-128509: Add `sys._is_immortal` for identifying immortal obje…

    6a8d880
  10. vstinner
    added 4 commits that reference this issue on Sep 13, 2026

    gh-128509: Use bytes singletons in marshal (#157398)

    658612a

    [3.14] gh-128509: Use bytes singletons in marshal (GH-157398) (#157432)

    c348765

    gh-128509: Add tests on sys._is_immortal() (#157376)

    fb46c67

    [3.14] gh-128509: Add tests on sys._is_immortal() (GH-157376) (#157446)

    fceb517
  11. johnslavik
    added 2 commits that reference this issue on Sep 14, 2026

    pythongh-128509: Use bytes singletons in marshal (python#157398)

    ea2a150

    pythongh-128509: Add tests on sys._is_immortal() (python#157376)

    169470f
  12. hugovk
    added a commit that references this issue on Sep 15, 2026

    [3.15] gh-128509: Use bytes singletons in marshal (GH-157398) (#157433)

    c24b1aa
  13. vstinner
    added a commit that references this issue on Oct 9, 2026

    [3.15] gh-128509: Add tests on sys._is_immortal() (GH-157376) (#157445)

    4911737
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.