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
22 changes: 22 additions & 0 deletions Include/refcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,28 @@ static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
# define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), (refcnt))
#endif

#if !defined(Py_LIMITED_API)
/* Refresh the refcount of an immortal object to make
* accidental de-immortalization less likely */
static inline void
_Py_RefreshImmortal(PyObject *ob)
{
#ifndef Py_GIL_DISABLED
if (_Py_IsImmortal(ob)) {
#if SIZEOF_VOID_P > 4
ob->ob_refcnt = _Py_IMMORTAL_INITIAL_REFCNT;
#else
if (_Py_IsStaticImmortal(ob)) {
ob->ob_refcnt = _Py_STATIC_IMMORTAL_INITIAL_REFCNT;
}
else {
ob->ob_refcnt = _Py_IMMORTAL_INITIAL_REFCNT;
}
#endif
}
#endif
}
#endif // Py_LIMITED_API

/*
The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
Expand Down
3 changes: 3 additions & 0 deletions Objects/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ _Py_CheckFunctionResult(PyThreadState *tstate, PyObject *callable,
return NULL;
}
}
if (result) {
_Py_RefreshImmortal(result);
}
return result;
}

Expand Down
Loading