@@ -38,21 +38,18 @@ For example:
3838.. code-block :: C
3939
4040 static PyObject *
41- my_method(PyObject *self , PyObject *object) // METH_O
41+ my_singleton_new(PyTypeObject *type , PyObject *args, PyObject *kwargs)
4242 {
43- if (PyUnstable_Immortalize(object) < 0) {
43+ PyObject *self = type->tp_alloc(type, 0);
44+ if (self == NULL) {
4445 return NULL;
4546 }
4647
47- /* The object is now immortal, we can do whatever we want with it's
48- reference count without worrying about thread safety or leaks. */
49-
50- // This is totally safe for an immortal object
51- for (int i = 0; i < 10; ++i) {
52- Py_DECREF(object);
48+ if (PyUnstable_Immortalize(self) < 0) {
49+ return NULL;
5350 }
5451
55- Py_RETURN_NONE ;
52+ return self ;
5653 }
5754
5855 Or, in Python:
@@ -61,25 +58,12 @@ Or, in Python:
6158
6259 import sys
6360
64- my_own_singleton = object ()
65- sys._immortalize(my_own_singleton)
66- assert sys._is_immortal(my_own_singleton)
67-
68- What Do We Mean By "Immortal"?
69- ******************************
70-
71- By immortal, we mean an object with an immortal reference count, per :pep: `683 `.
72- However, the main difference in this PEP is that the underlying behavior differs
73- from the documentation, which states that the object is *never * deallocated:
61+ my_singleton = object ()
62+ sys._immortalize(my_singleton)
63+ assert sys._is_immortal(my_singleton)
7464
75- Some objects are immortal and have reference counts that are never
76- modified, and therefore the objects are never deallocated.
77-
78- In this case, immortal objects are eventually deallocated, it's just that the user
79- never really sees when that happens because they're freed during interpreter
80- finalization. So, the term "immortal" throughout this PEP really means
81- "never deallocated for the lifetime of the interpreter," rather than
82- "never deallocated."
65+ Motivation
66+ ==========
8367
8468What Problem Does Immortality Solve?
8569************************************
@@ -110,8 +94,6 @@ other areas of CPython, or in third-party libraries:
11094
11195.. _doing this in Cinder : https://engineering.fb.com/2023/08/15/developer-tools/immortal-objects-for-python-instagram-meta/
11296
113- Motivation
114- ==========
11597
11698Immortal Objects Must Remain in CPython
11799***************************************
@@ -179,9 +161,9 @@ Immortality is a Powerful Tool to Wield
179161***************************************
180162
181163In general, immortal objects seem to have a lot of applications that aren't
182- immediately clear. For example, `Nanobind used immortal objects `_
183- in its original :term: `free threading ` implementation. It seems like it would
184- have interesting use-cases for many places outside of CPython .
164+ immediately clear to CPython maintainers . For example, `Nanobind used immortal objects `_
165+ in its original :term: `free threading ` implementation. So, it seems like immortality
166+ has interesting use-cases in third-party libraries .
185167
186168.. _Nanobind used immortal objects : https://github.com/wjakob/nanobind/pull/695
187169
0 commit comments