Skip to content

gh-84436 Add integration C API tests for immortal objects #103962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 2, 2023
Prev Previous commit
Next Next commit
address code review
  • Loading branch information
corona10 committed May 2, 2023
commit 55a394b36869e73662f33e04cfae47805e8ef0cc
26 changes: 12 additions & 14 deletions Modules/_testcapi/immortal.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ static PyObject*
test_immortal_bool(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject* objects[] = {Py_True, Py_False};
Py_ssize_t n = sizeof(objects) / sizeof(objects[0]);
Py_ssize_t n = Py_ARRAY_LENGTH(objects);
for(Py_ssize_t i = 0; i < n; i++) {
PyObject* obj = objects[i];
if (!_Py_IsImmortal(obj)) {
PyErr_Format(PyExc_RuntimeError, "%R should be the immportal object.", obj);
return NULL;
}
Py_ssize_t old_count = Py_REFCNT(obj);
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 10000; j++) {
Py_DECREF(obj);
}
Py_ssize_t current_count = Py_REFCNT(obj);
Expand All @@ -27,16 +27,15 @@ test_immortal_bool(PyObject *self, PyObject *Py_UNUSED(ignored))
static PyObject *
test_immortal_none(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject* none = Py_None;
if (!_Py_IsImmortal(none)) {
PyErr_Format(PyExc_RuntimeError, "%R should be the immportal object.", none);
if (!_Py_IsImmortal(Py_None)) {
PyErr_Format(PyExc_RuntimeError, "%R should be the immportal object.", Py_None);
return NULL;
}
Py_ssize_t old_count = Py_REFCNT(none);
Py_ssize_t old_count = Py_REFCNT(Py_None);
for (int i = 0; i < 10000; i++) {
Py_DECREF(none);
Py_DECREF(Py_None);
}
Py_ssize_t current_count = Py_REFCNT(none);
Py_ssize_t current_count = Py_REFCNT(Py_None);
if (old_count != current_count) {
PyErr_SetString(PyExc_RuntimeError, "Reference count should not be changed.");
return NULL;
Expand All @@ -54,7 +53,7 @@ test_immortal_small_ints(PyObject *self, PyObject *Py_UNUSED(ignored))
return NULL;
}
Py_ssize_t old_count = Py_REFCNT(small_int);
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 10000; j++) {
Py_DECREF(small_int);
}
Py_ssize_t current_count = Py_REFCNT(small_int);
Expand All @@ -69,16 +68,15 @@ test_immortal_small_ints(PyObject *self, PyObject *Py_UNUSED(ignored))
static PyObject *
test_immortal_ellipsis(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject* ellipsis = Py_Ellipsis;
if (!_Py_IsImmortal(ellipsis)) {
if (!_Py_IsImmortal(Py_Ellipsis)) {
PyErr_SetString(PyExc_RuntimeError, "Ellipsis object should be the immportal object.");
return NULL;
}
Py_ssize_t old_count = Py_REFCNT(ellipsis);
Py_ssize_t old_count = Py_REFCNT(Py_Ellipsis);
for (int i = 0; i < 10000; i++) {
Py_DECREF(ellipsis);
Py_DECREF(Py_Ellipsis);
}
Py_ssize_t current_count = Py_REFCNT(ellipsis);
Py_ssize_t current_count = Py_REFCNT(Py_Ellipsis);
if (old_count != current_count) {
PyErr_SetString(PyExc_RuntimeError, "Reference count should not be changed.");
return NULL;
Expand Down