@@ -12,23 +12,12 @@ Post-History: `01-Jul-2025 <https://discuss.python.org/t/97306>`__
1212Abstract
1313========
1414
15- :pep: `683 ` introduced :term: `immortal ` objects into CPython, using an "immortal
16- reference count" where the reference count field is never modified on the
17- object. Currently, all APIs for enabling immortality on a given object are
18- private, and even those APIs aren't fully safe for immortalizing *any * object.
19-
20- In particular, the private API has ``_Py_SetImmortal ``, which simply untracks a
21- passed object from the garbage collector, and then sets its reference count to
22- the magic immortal number. But again, this isn't totally safe; for example, due
23- to the current string interning implementation, immortalizing a string can cause
24- crashes. To make matters worse, there's no mechanism in place to deallocate an
25- object once it has become immortal; objects on the heap are leaked, which is bad
26- for per-interpreter state.
27-
2815This PEP aims to introduce a public way to immortalize arbitrary objects, while
29- making sure the object is deallocated later (specifically, during interpreter
16+ making sure the object is deallocated later (during interpreter
3017finalization). This is done by introducing two new APIs: :func: `sys._immortalize `
31- on the Python side, and :c:func: `PyUnstable_Immortalize ` on the C side.
18+ on the Python side, and :c:func: `PyUnstable_Immortalize ` on the C side. These
19+ are generally considered to be low-level APIs that won't be needed by users; the
20+ main beneficiary will be CPython.
3221
3322Both of these functions make the given object immortal, and then deallocate it when
3423the interpreter is shutting down using a simulated garbage collection.
@@ -62,9 +51,33 @@ Or, in Python:
6251 sys._immortalize(my_singleton)
6352 assert sys._is_immortal(my_singleton)
6453
54+ Running either of these examples on a Python debug build with :option: `-X showrefcount <-X> `
55+ show that the (heap-allocated) immortal object is not leaked:
56+
57+ .. code-block :: bash
58+
59+ $ ./python -Xshowrefcount -c " import sys; sys._immortalize(object())"
60+ [0 refs, 0 blocks]
61+
6562 Motivation
6663==========
6764
65+ It's Currently Impossible to Immortalize General Objects
66+ ********************************************************
67+
68+ :pep: `683 ` introduced :term: `immortal ` objects into CPython, using an "immortal
69+ reference count" where the reference count field is never modified on the
70+ object. Currently, all APIs for enabling immortality on a given object are
71+ private, and even those APIs aren't fully safe for immortalizing *any * object.
72+
73+ In particular, the private API has ``_Py_SetImmortal ``, which simply untracks a
74+ passed object from the garbage collector, and then sets its reference count to
75+ the magic immortal number. But again, this isn't totally safe; for example, due
76+ to the current string interning implementation, immortalizing a string can cause
77+ crashes. To make matters worse, there's no mechanism in place to deallocate an
78+ object once it has become immortal; this is limiting for potential optimizations
79+ using immortality.
80+
6881What Problem Does Immortality Solve?
6982************************************
7083
0 commit comments