Skip to content

Commit

Permalink
Address code review
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed May 2, 2023
1 parent b03def1 commit e0295b5
Showing 1 changed file with 15 additions and 30 deletions.
45 changes: 15 additions & 30 deletions Modules/_testcapi/immortal.c
Original file line number Diff line number Diff line change
@@ -1,62 +1,47 @@
#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;
}

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;
}

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;
}

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;
}

Expand Down

0 comments on commit e0295b5

Please sign in to comment.