Skip to content

Commit 0e6459b

Browse files
committed
Address code review
1 parent 035625d commit 0e6459b

File tree

1 file changed

+11
-35
lines changed

1 file changed

+11
-35
lines changed

Modules/_testcapi/immortal.c

+11-35
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,62 @@
11
#include "parts.h"
22

3-
static PyObject*
3+
static PyObject *
44
test_immortal_bool(PyObject *self, PyObject *Py_UNUSED(ignored))
55
{
66
PyObject* objects[] = {Py_True, Py_False};
77
Py_ssize_t n = Py_ARRAY_LENGTH(objects);
8-
for(Py_ssize_t i = 0; i < n; i++) {
8+
for (Py_ssize_t i = 0; i < n; i++) {
99
PyObject* obj = objects[i];
10-
if (!_Py_IsImmortal(obj)) {
11-
PyErr_Format(PyExc_RuntimeError, "%R should be the immportal object.", obj);
12-
return NULL;
13-
}
10+
assert(_Py_IsImmortal(obj));
1411
Py_ssize_t old_count = Py_REFCNT(obj);
1512
for (int j = 0; j < 10000; j++) {
1613
Py_DECREF(obj);
1714
}
1815
Py_ssize_t current_count = Py_REFCNT(obj);
19-
if (old_count != current_count) {
20-
PyErr_SetString(PyExc_RuntimeError, "Reference count should not be changed.");
21-
return NULL;
22-
}
16+
assert(old_count == current_count);
2317
}
2418
Py_RETURN_NONE;
2519
}
2620

2721
static PyObject *
2822
test_immortal_none(PyObject *self, PyObject *Py_UNUSED(ignored))
2923
{
30-
if (!_Py_IsImmortal(Py_None)) {
31-
PyErr_Format(PyExc_RuntimeError, "%R should be the immportal object.", Py_None);
32-
return NULL;
33-
}
24+
assert(_Py_IsImmortal(Py_None));
3425
Py_ssize_t old_count = Py_REFCNT(Py_None);
3526
for (int i = 0; i < 10000; i++) {
3627
Py_DECREF(Py_None);
3728
}
3829
Py_ssize_t current_count = Py_REFCNT(Py_None);
39-
if (old_count != current_count) {
40-
PyErr_SetString(PyExc_RuntimeError, "Reference count should not be changed.");
41-
return NULL;
42-
}
30+
assert(old_count == current_count);
4331
Py_RETURN_NONE;
4432
}
4533

4634
static PyObject *
4735
test_immortal_small_ints(PyObject *self, PyObject *Py_UNUSED(ignored))
4836
{
49-
for(int i = -5; i <= 256; i++) {
37+
for (int i = -5; i <= 256; i++) {
5038
PyObject *small_int = PyLong_FromLong(i);
51-
if (!_Py_IsImmortal(small_int)) {
52-
PyErr_Format(PyExc_RuntimeError, "Small int(%d) object should be the immportal object.", i);
53-
return NULL;
54-
}
39+
assert(_Py_IsImmortal(small_int));
5540
Py_ssize_t old_count = Py_REFCNT(small_int);
5641
for (int j = 0; j < 10000; j++) {
5742
Py_DECREF(small_int);
5843
}
5944
Py_ssize_t current_count = Py_REFCNT(small_int);
60-
if (old_count != current_count) {
61-
PyErr_Format(PyExc_RuntimeError, "Reference count of %d should not be changed.", i);
62-
return NULL;
63-
}
45+
assert(old_count == current_count);
6446
}
6547
Py_RETURN_NONE;
6648
}
6749

6850
static PyObject *
6951
test_immortal_ellipsis(PyObject *self, PyObject *Py_UNUSED(ignored))
7052
{
71-
if (!_Py_IsImmortal(Py_Ellipsis)) {
72-
PyErr_SetString(PyExc_RuntimeError, "Ellipsis object should be the immportal object.");
73-
return NULL;
74-
}
53+
assert(_Py_IsImmortal(Py_Ellipsis));
7554
Py_ssize_t old_count = Py_REFCNT(Py_Ellipsis);
7655
for (int i = 0; i < 10000; i++) {
7756
Py_DECREF(Py_Ellipsis);
7857
}
7958
Py_ssize_t current_count = Py_REFCNT(Py_Ellipsis);
80-
if (old_count != current_count) {
81-
PyErr_SetString(PyExc_RuntimeError, "Reference count should not be changed.");
82-
return NULL;
83-
}
59+
assert(old_count == current_count);
8460
Py_RETURN_NONE;
8561
}
8662

0 commit comments

Comments
 (0)