Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated
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
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated
and will never be garbage collected.
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated

This function is intended to be used soon after op is created, by the code that
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated
creates it, such as in the object's tp_new slot.
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated
Returns 1 if the object was made immortal and returns 0 if it was not.
This function cannot fail.

Comment thread
kumaraditya303 marked this conversation as resolved.
.. 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)
{
Comment thread
kumaraditya303 marked this conversation as resolved.
assert(op != NULL);
_Py_SetImmortal(op);
Comment thread
kumaraditya303 marked this conversation as resolved.
return 1;
}

void
_Py_ResurrectReference(PyObject *op)
{
Expand Down
Loading