diff --git a/Modules/_testcapi/immortal.c b/Modules/_testcapi/immortal.c index 8a6068481cf979..6cc4bc6abdb113 100644 --- a/Modules/_testcapi/immortal.c +++ b/Modules/_testcapi/immortal.c @@ -1,19 +1,23 @@ #include "parts.h" +int verify_immortality(PyObject *object) +{ + assert(_Py_IsImmortal(object)); + Py_ssize_t old_count = Py_REFCNT(object); + for (int j = 0; j < 10000; j++) { + Py_DECREF(object); + } + Py_ssize_t current_count = Py_REFCNT(object); + return old_count == current_count; +} + static PyObject * test_immortal_bool(PyObject *self, PyObject *Py_UNUSED(ignored)) { PyObject *objects[] = {Py_True, Py_False}; Py_ssize_t n = Py_ARRAY_LENGTH(objects); for (Py_ssize_t i = 0; i < n; i++) { - PyObject* obj = objects[i]; - assert(_Py_IsImmortal(obj)); - Py_ssize_t old_count = Py_REFCNT(obj); - for (int j = 0; j < 10000; j++) { - Py_DECREF(obj); - } - Py_ssize_t current_count = Py_REFCNT(obj); - assert(old_count == current_count); + assert(verify_immortality(objects[i])); } Py_RETURN_NONE; } @@ -21,13 +25,7 @@ test_immortal_bool(PyObject *self, PyObject *Py_UNUSED(ignored)) static PyObject * test_immortal_none(PyObject *self, PyObject *Py_UNUSED(ignored)) { - assert(_Py_IsImmortal(Py_None)); - Py_ssize_t old_count = Py_REFCNT(Py_None); - for (int i = 0; i < 10000; i++) { - Py_DECREF(Py_None); - } - Py_ssize_t current_count = Py_REFCNT(Py_None); - assert(old_count == current_count); + assert(verify_immortality(Py_None)); Py_RETURN_NONE; } @@ -35,14 +33,7 @@ static PyObject * test_immortal_small_ints(PyObject *self, PyObject *Py_UNUSED(ignored)) { for (int i = -5; i <= 256; i++) { - PyObject *small_int = PyLong_FromLong(i); - assert(_Py_IsImmortal(small_int)); - Py_ssize_t old_count = Py_REFCNT(small_int); - for (int j = 0; j < 10000; j++) { - Py_DECREF(small_int); - } - Py_ssize_t current_count = Py_REFCNT(small_int); - assert(old_count == current_count); + assert(verify_immortality(PyLong_FromLong(i))); } Py_RETURN_NONE; } @@ -50,13 +41,7 @@ test_immortal_small_ints(PyObject *self, PyObject *Py_UNUSED(ignored)) static PyObject * test_immortal_ellipsis(PyObject *self, PyObject *Py_UNUSED(ignored)) { - assert(_Py_IsImmortal(Py_Ellipsis)); - Py_ssize_t old_count = Py_REFCNT(Py_Ellipsis); - for (int i = 0; i < 10000; i++) { - Py_DECREF(Py_Ellipsis); - } - Py_ssize_t current_count = Py_REFCNT(Py_Ellipsis); - assert(old_count == current_count); + assert(verify_immortality(Py_Ellipsis)); Py_RETURN_NONE; }