Skip to content

PEP 797: Arbitrary Object Immortalization #4497

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Further improve the abstract.
  • Loading branch information
ZeroIntensity committed Jul 18, 2025
commit e8cdb33fbb5265c66a4cd56fbf43925b64c3074d
43 changes: 28 additions & 15 deletions peps/pep-0797.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,12 @@ Post-History: `01-Jul-2025 <https://discuss.python.org/t/97306>`__
Abstract
Copy link
Member

Choose a reason for hiding this comment

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

This abstract is much longer than it should be.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, will shorten it. Which parts do you think should be excluded/moved?

Copy link
Member

Choose a reason for hiding this comment

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

Most of it, really. The abstract should generally only be a few paragraphs. It explains primarily what the PEP does, not the motivation or implementation details.

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've now significantly reduced the size of the abstract. Does that look better?

========

:pep:`683` introduced :term:`immortal` objects into CPython, using an "immortal
reference count" where the reference count field is never modified on the
object. Currently, all APIs for enabling immortality on a given object are
private, and even those APIs aren't fully safe for immortalizing *any* object.

In particular, the private API has ``_Py_SetImmortal``, which simply untracks a
passed object from the garbage collector, and then sets its reference count to
the magic immortal number. But again, this isn't totally safe; for example, due
to the current string interning implementation, immortalizing a string can cause
crashes. To make matters worse, there's no mechanism in place to deallocate an
object once it has become immortal; objects on the heap are leaked, which is bad
for per-interpreter state.

This PEP aims to introduce a public way to immortalize arbitrary objects, while
making sure the object is deallocated later (specifically, during interpreter
making sure the object is deallocated later (during interpreter
finalization). This is done by introducing two new APIs: :func:`sys._immortalize`
on the Python side, and :c:func:`PyUnstable_Immortalize` on the C side.
on the Python side, and :c:func:`PyUnstable_Immortalize` on the C side. These
are generally considered to be low-level APIs that won't be needed by users; the
main beneficiary will be CPython.

Both of these functions make the given object immortal, and then deallocate it when
the interpreter is shutting down using a simulated garbage collection.
Expand Down Expand Up @@ -62,9 +51,33 @@ Or, in Python:
sys._immortalize(my_singleton)
assert sys._is_immortal(my_singleton)

Running either of these examples on a Python debug build with :option:`-X showrefcount <-X>`
show that the (heap-allocated) immortal object is not leaked:

.. code-block:: bash

$ ./python -Xshowrefcount -c "import sys; sys._immortalize(object())"
[0 refs, 0 blocks]

Motivation
==========

It's Currently Impossible to Immortalize General Objects
********************************************************

:pep:`683` introduced :term:`immortal` objects into CPython, using an "immortal
reference count" where the reference count field is never modified on the
object. Currently, all APIs for enabling immortality on a given object are
private, and even those APIs aren't fully safe for immortalizing *any* object.

In particular, the private API has ``_Py_SetImmortal``, which simply untracks a
passed object from the garbage collector, and then sets its reference count to
the magic immortal number. But again, this isn't totally safe; for example, due
to the current string interning implementation, immortalizing a string can cause
crashes. To make matters worse, there's no mechanism in place to deallocate an
object once it has become immortal; this is limiting for potential optimizations
using immortality.

What Problem Does Immortality Solve?
************************************

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy