Skip to content

Commit e262963

Browse files
committed
refactor code
1 parent 8d864ef commit e262963

1 file changed

Lines changed: 31 additions & 30 deletions

File tree

  • Modules/_testlimitedcapi

Modules/_testlimitedcapi/set.c

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,6 @@ set_clear(PyObject *self, PyObject *obj)
120120
RETURN_INT(PySet_Clear(obj));
121121
}
122122

123-
/* Raise AssertionError with test_name + ": " + msg, and return NULL. */
124-
125-
static PyObject *
126-
raiseTestError(const char* test_name, const char* msg)
127-
{
128-
PyObject *exc = PyErr_GetRaisedException();
129-
PyErr_Format(exc, "%s: %s", test_name, msg);
130-
return NULL;
131-
}
132-
133-
134123
static PyObject *
135124
test_frozenset_add_in_capi(PyObject *self, PyObject *Py_UNUSED(obj))
136125
{
@@ -167,55 +156,67 @@ test_frozenset_add_in_capi(PyObject *self, PyObject *Py_UNUSED(obj))
167156
}
168157

169158
static PyObject *
170-
test_frozenset_add_in_capi_tracking(PyObject *self, PyObject *Py_UNUSED(ignored))
159+
raiseTestError(const char* test_name, const char* msg)
171160
{
172-
PyObject *one = PyLong_FromLong(1);
173-
assert(one);
161+
PyObject *exc = PyErr_GetRaisedException();
162+
PyErr_Format(PyErr_GetRaisedException(), "%s: %s", test_name, msg);
163+
return NULL;
164+
}
174165

166+
static PyObject *
167+
test_frozenset_add_in_capi_tracking_immutable(PyObject *self, PyObject *Py_UNUSED(ignored))
168+
{
175169
// Test: GC tracking - frozenset with only immutable items should not be tracked
176-
frozenset = PyFrozenSet_New(NULL);
170+
PyObject *frozenset = PyFrozenSet_New(NULL);
177171
if (frozenset == NULL) {
178172
return NULL;
179173
}
180-
if (PySet_Add(frozenset, one) < 0) {
174+
if (PySet_Add(frozenset, Py_True) < 0) {
181175
Py_DECREF(frozenset);
182176
return NULL;
183177
}
184178
if (PyObject_GC_IsTracked(frozenset)) {
185179
Py_DECREF(frozenset);
186-
return raiseTestError("test_pyset_add_frozenset",
187-
"frozenset with only int should not be GC tracked");
180+
return raiseTestError("test_frozenset_add_in_capi_tracking_immutable",
181+
"frozenset with only int should not be GC tracked");
188182
}
189183
Py_DECREF(frozenset);
184+
Py_RETURN_NONE;
185+
}
190186

187+
static PyObject *
188+
test_frozenset_add_in_capi_tracking(PyObject *self, PyObject *Py_UNUSED(ignored))
189+
{
191190
// Test: GC tracking - frozenset with tracked object should be tracked
192-
frozenset = PyFrozenSet_New(NULL);
191+
PyObject *frozenset = PyFrozenSet_New(NULL);
193192
if (frozenset == NULL) {
194193
return NULL;
195194
}
196195

197196
PyObject *tracked_obj = PyErr_NewException("_testlimitedcapi.py_set_add", NULL, NULL);
198197
if (tracked_obj == NULL) {
199-
Py_DECREF(frozenset);
200-
return NULL;
198+
goto error;
201199
}
202200
if (!PyObject_GC_IsTracked(tracked_obj)) {
203-
return raiseTestError("test_pyset_add_frozenset",
201+
return raiseTestError("test_frozenset_add_in_capi_tracking",
204202
"test object should be tracked");
205203
}
206204
if (PySet_Add(frozenset, tracked_obj) < 0) {
207-
Py_DECREF(frozenset);
208-
Py_DECREF(tracked_obj);
209-
return NULL;
205+
goto error;
210206
}
207+
Py_DECREF(tracked_obj);
211208
if (!PyObject_GC_IsTracked(frozenset)) {
212209
Py_DECREF(frozenset);
213-
Py_DECREF(tracked_obj);
214-
return raiseTestError("test_pyset_add_frozenset",
210+
return raiseTestError("test_frozenset_add_in_capi_tracking",
215211
"frozenset with with GC tracked object should be tracked");
216212
}
217-
213+
Py_DECREF(frozenset);
218214
Py_RETURN_NONE;
215+
216+
error:
217+
Py_DECREF(frozenset);
218+
Py_XDECREF(tracked_obj);
219+
return NULL;
219220
}
220221

221222

@@ -264,7 +265,6 @@ test_set_contains_does_not_convert_unhashable_key(PyObject *self, PyObject *Py_U
264265
return NULL;
265266
}
266267

267-
268268
static PyMethodDef test_methods[] = {
269269
{"set_check", set_check, METH_O},
270270
{"set_checkexact", set_checkexact, METH_O},
@@ -284,9 +284,10 @@ static PyMethodDef test_methods[] = {
284284
{"set_clear", set_clear, METH_O},
285285

286286
{"test_frozenset_add_in_capi", test_frozenset_add_in_capi, METH_NOARGS},
287+
{"test_frozenset_add_in_capi_tracking", test_frozenset_add_in_capi_tracking, METH_NOARGS},
288+
{"test_frozenset_add_in_capi_tracking_immutable", test_frozenset_add_in_capi_tracking_immutable, METH_NOARGS},
287289
{"test_set_contains_does_not_convert_unhashable_key",
288290
test_set_contains_does_not_convert_unhashable_key, METH_NOARGS},
289-
{"test_pyset_add_frozenset", test_pyset_add_frozenset, METH_NOARGS},
290291

291292
{NULL},
292293
};

0 commit comments

Comments
 (0)