gh-128509: Add sys._is_immortal for identifying immortal objects (#128510)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
Peter Bierma 2025-01-31 10:27:08 -05:00 committed by GitHub
parent 60a85415ae
commit 9ba281d871
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 81 additions and 1 deletions

View file

@ -373,6 +373,36 @@ exit:
return return_value;
}
PyDoc_STRVAR(sys__is_immortal__doc__,
"_is_immortal($module, op, /)\n"
"--\n"
"\n"
"Return True if the given object is \"immortal\" per PEP 683.\n"
"\n"
"This function should be used for specialized purposes only.");
#define SYS__IS_IMMORTAL_METHODDEF \
{"_is_immortal", (PyCFunction)sys__is_immortal, METH_O, sys__is_immortal__doc__},
static int
sys__is_immortal_impl(PyObject *module, PyObject *op);
static PyObject *
sys__is_immortal(PyObject *module, PyObject *op)
{
PyObject *return_value = NULL;
int _return_value;
_return_value = sys__is_immortal_impl(module, op);
if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
}
return_value = PyBool_FromLong((long)_return_value);
exit:
return return_value;
}
PyDoc_STRVAR(sys_settrace__doc__,
"settrace($module, function, /)\n"
"--\n"
@ -1724,4 +1754,4 @@ exit:
#ifndef SYS_GETANDROIDAPILEVEL_METHODDEF
#define SYS_GETANDROIDAPILEVEL_METHODDEF
#endif /* !defined(SYS_GETANDROIDAPILEVEL_METHODDEF) */
/*[clinic end generated code: output=568b0a0069dc43e8 input=a9049054013a1b77]*/
/*[clinic end generated code: output=1e5f608092c12636 input=a9049054013a1b77]*/

View file

@ -972,6 +972,23 @@ sys__is_interned_impl(PyObject *module, PyObject *string)
return PyUnicode_CHECK_INTERNED(string);
}
/*[clinic input]
sys._is_immortal -> bool
op: object
/
Return True if the given object is "immortal" per PEP 683.
This function should be used for specialized purposes only.
[clinic start generated code]*/
static int
sys__is_immortal_impl(PyObject *module, PyObject *op)
/*[clinic end generated code: output=c2f5d6a80efb8d1a input=4609c9bf5481db76]*/
{
return PyUnstable_IsImmortal(op);
}
/*
* Cached interned string objects used for calling the profile and
@ -2588,6 +2605,7 @@ static PyMethodDef sys_methods[] = {
SYS__GETFRAMEMODULENAME_METHODDEF
SYS_GETWINDOWSVERSION_METHODDEF
SYS__ENABLELEGACYWINDOWSFSENCODING_METHODDEF
SYS__IS_IMMORTAL_METHODDEF
SYS_INTERN_METHODDEF
SYS__IS_INTERNED_METHODDEF
SYS_IS_FINALIZING_METHODDEF