diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-22-07-58-00.gh-issue-127119.p9Yv4U.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-22-07-58-00.gh-issue-127119.p9Yv4U.rst new file mode 100644 index 00000000000000..68b8b1d37cffc1 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-22-07-58-00.gh-issue-127119.p9Yv4U.rst @@ -0,0 +1 @@ +Slightly optimize the :class:`int` deallocator by removing a redundant check. diff --git a/Objects/boolobject.c b/Objects/boolobject.c index a88a8ad0cfd560..fa210227b77fbd 100644 --- a/Objects/boolobject.c +++ b/Objects/boolobject.c @@ -159,11 +159,15 @@ static PyNumberMethods bool_as_number = { static void bool_dealloc(PyObject *boolean) { +#ifndef Py_GIL_DISABLED /* This should never get called, but we also don't want to SEGV if * we accidentally decref Booleans out of existence. Instead, * since bools are immortal, re-set the reference count. + * + * See PEP 683, section Accidental De-Immortalizing for details */ _Py_SetImmortal(boolean); +#endif } /* The type object for bool. Note that this cannot be subclassed! */ diff --git a/Objects/longobject.c b/Objects/longobject.c index 4aa35685b509f2..66300ddca0ef78 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -3614,9 +3614,12 @@ long_richcompare(PyObject *self, PyObject *other, int op) static void long_dealloc(PyObject *self) { +#ifndef Py_GIL_DISABLED /* This should never get called, but we also don't want to SEGV if * we accidentally decref small Ints out of existence. Instead, * since small Ints are immortal, re-set the reference count. + * + * See PEP 683, section Accidental De-Immortalizing for details */ PyLongObject *pylong = (PyLongObject*)self; if (pylong && _PyLong_IsCompact(pylong)) { @@ -3629,6 +3632,7 @@ long_dealloc(PyObject *self) } } } +#endif Py_TYPE(self)->tp_free(self); } diff --git a/Objects/object.c b/Objects/object.c index 8868fa29066404..36cd64a36595dd 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -2015,11 +2015,15 @@ none_repr(PyObject *op) static void none_dealloc(PyObject* none) { +#ifndef Py_GIL_DISABLED /* This should never get called, but we also don't want to SEGV if - * we accidentally decref None out of existence. Instead, - * since None is an immortal object, re-set the reference count. + * we accidentally decref NotImplemented out of existence. Instead, + * since Notimplemented is an immortal object, re-set the reference count. + * + * See PEP 683, section Accidental De-Immortalizing for details */ _Py_SetImmortal(none); +#endif } static PyObject * @@ -2161,11 +2165,15 @@ notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) static void notimplemented_dealloc(PyObject *notimplemented) { +#ifndef Py_GIL_DISABLED /* This should never get called, but we also don't want to SEGV if * we accidentally decref NotImplemented out of existence. Instead, * since Notimplemented is an immortal object, re-set the reference count. + * + * See PEP 683, section Accidental De-Immortalizing for details */ _Py_SetImmortal(notimplemented); +#endif } static int diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 4fef0af93fe095..2bdb53a52767e6 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -34,11 +34,15 @@ ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) static void ellipsis_dealloc(PyObject *ellipsis) { +#ifndef Py_GIL_DISABLED /* This should never get called, but we also don't want to SEGV if - * we accidentally decref Ellipsis out of existence. Instead, - * since Ellipsis is an immortal object, re-set the reference count. + * we accidentally decref NotImplemented out of existence. Instead, + * since Notimplemented is an immortal object, re-set the reference count. + * + * See PEP 683, section Accidental De-Immortalizing for details */ _Py_SetImmortal(ellipsis); +#endif } static PyObject * diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index bacb858978c5d7..84ecc4f6c77b8d 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -91,11 +91,15 @@ nodefault_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) static void nodefault_dealloc(PyObject *nodefault) { +#ifndef Py_GIL_DISABLED /* This should never get called, but we also don't want to SEGV if - * we accidentally decref NoDefault out of existence. Instead, - * since NoDefault is an immortal object, re-set the reference count. + * we accidentally decref NotImplemented out of existence. Instead, + * since Notimplemented is an immortal object, re-set the reference count. + * + * See PEP 683, section Accidental De-Immortalizing for details */ _Py_SetImmortal(nodefault); +#endif } PyDoc_STRVAR(nodefault_doc,