Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions Doc/c-api/object.rst
Original file line number Diff line number Diff line change
Expand Up @@ -801,3 +801,19 @@ Object Protocol
cannot fail.
.. versionadded:: 3.14
.. c:function:: int PyUnstable_SetImmortal(PyObject *obj)
Marks the object op immortal. The argument should be uniquely referenced by
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Marks the object op immortal. The argument should be uniquely referenced by
Marks the object as immortal. The argument should be uniquely referenced by

?

the calling thread.
This is a one-way process: objects can only be made immortal, they cannot be
made mortal once again. Immortal objects do not participate in reference counting
and will never be garbage collected.
This function is intended to be used soon after op is created, by the code that
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
This function is intended to be used soon after op is created, by the code that
This function is intended to be used soon after `obj` is created, by the code that

creates it, such as in the object's tp_new slot.
Returns 1 if the object was made immortal and returns 0 if it was not.
This function cannot fail.
.. versionadded:: next
2 changes: 2 additions & 0 deletions Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,5 @@ PyAPI_FUNC(int) PyUnstable_TryIncRef(PyObject *);
PyAPI_FUNC(void) PyUnstable_EnableTryIncRef(PyObject *);

PyAPI_FUNC(int) PyUnstable_Object_IsUniquelyReferenced(PyObject *);

PyAPI_FUNC(int) PyUnstable_SetImmortal(PyObject *op);
17 changes: 17 additions & 0 deletions Modules/_testcapi/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,22 @@ test_py_try_inc_ref(PyObject *self, PyObject *unused)
Py_RETURN_NONE;
}

static PyObject *
test_py_set_immortal(PyObject *self, PyObject *unused)
{
// the object is allocated on C stack as otherwise,
// it would trip the refleak checker when the object
// is made immortal and leak memory
PyObject object = {0};
Py_SET_REFCNT(&object, 1);
Py_SET_TYPE(&object, &PyBaseObject_Type);
assert(!PyUnstable_IsImmortal(&object));
PyUnstable_SetImmortal(&object);
assert(PyUnstable_IsImmortal(&object));
Py_DECREF(&object); // should not dealloc
assert(PyUnstable_IsImmortal(&object));
Py_RETURN_NONE;
}

static PyObject *
_test_incref(PyObject *ob)
Expand Down Expand Up @@ -528,6 +544,7 @@ static PyMethodDef test_methods[] = {
{"pyobject_is_unique_temporary", pyobject_is_unique_temporary, METH_O},
{"pyobject_is_unique_temporary_new_object", pyobject_is_unique_temporary_new_object, METH_NOARGS},
{"test_py_try_inc_ref", test_py_try_inc_ref, METH_NOARGS},
{"test_py_set_immortal", test_py_set_immortal, METH_NOARGS},
{"test_xincref_doesnt_leak",test_xincref_doesnt_leak, METH_NOARGS},
{"test_incref_doesnt_leak", test_incref_doesnt_leak, METH_NOARGS},
{"test_xdecref_doesnt_leak",test_xdecref_doesnt_leak, METH_NOARGS},
Expand Down
8 changes: 8 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2790,6 +2790,14 @@ PyUnstable_EnableTryIncRef(PyObject *op)
#endif
}

int
PyUnstable_SetImmortal(PyObject *op)
{
Copy link
Contributor

Choose a reason for hiding this comment

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

should there maybe be a call to IsUniquelyReferenced as a sanity check? It won't be foolproof for objects that are transiently shared. However, if the argument isn't uniquely referenced then that's definitely incorrect use of this function.

assert(op != NULL);
_Py_SetImmortal(op);
return 1;
}

void
_Py_ResurrectReference(PyObject *op)
{
Expand Down
Loading