bpo-28254: Add a C-API for controlling the GC state (GH-25687) · python/cpython@3cc481b (original) (raw)
`@@ -144,6 +144,67 @@ test_sizeof_c_types(PyObject *self, PyObject *Py_UNUSED(ignored))
`
144
144
`#endif
`
145
145
`}
`
146
146
``
``
147
`+
static PyObject*
`
``
148
`+
test_gc_control(PyObject *self, PyObject *Py_UNUSED(ignored))
`
``
149
`+
{
`
``
150
`+
int orig_enabled = PyGC_IsEnabled();
`
``
151
`+
const char* msg = "ok";
`
``
152
`+
int old_state;
`
``
153
+
``
154
`+
old_state = PyGC_Enable();
`
``
155
`+
msg = "Enable(1)";
`
``
156
`+
if (old_state != orig_enabled) {
`
``
157
`+
goto failed;
`
``
158
`+
}
`
``
159
`+
msg = "IsEnabled(1)";
`
``
160
`+
if (!PyGC_IsEnabled()) {
`
``
161
`+
goto failed;
`
``
162
`+
}
`
``
163
+
``
164
`+
old_state = PyGC_Disable();
`
``
165
`+
msg = "disable(2)";
`
``
166
`+
if (!old_state) {
`
``
167
`+
goto failed;
`
``
168
`+
}
`
``
169
`+
msg = "IsEnabled(2)";
`
``
170
`+
if (PyGC_IsEnabled()) {
`
``
171
`+
goto failed;
`
``
172
`+
}
`
``
173
+
``
174
`+
old_state = PyGC_Enable();
`
``
175
`+
msg = "enable(3)";
`
``
176
`+
if (old_state) {
`
``
177
`+
goto failed;
`
``
178
`+
}
`
``
179
`+
msg = "IsEnabled(3)";
`
``
180
`+
if (!PyGC_IsEnabled()) {
`
``
181
`+
goto failed;
`
``
182
`+
}
`
``
183
+
``
184
`+
if (!orig_enabled) {
`
``
185
`+
old_state = PyGC_Disable();
`
``
186
`+
msg = "disable(4)";
`
``
187
`+
if (old_state) {
`
``
188
`+
goto failed;
`
``
189
`+
}
`
``
190
`+
msg = "IsEnabled(4)";
`
``
191
`+
if (PyGC_IsEnabled()) {
`
``
192
`+
goto failed;
`
``
193
`+
}
`
``
194
`+
}
`
``
195
+
``
196
`+
Py_RETURN_NONE;
`
``
197
+
``
198
`+
failed:
`
``
199
`+
/* Try to clean up if we can. */
`
``
200
`+
if (orig_enabled) {
`
``
201
`+
PyGC_Enable();
`
``
202
`+
} else {
`
``
203
`+
PyGC_Disable();
`
``
204
`+
}
`
``
205
`+
PyErr_Format(TestError, "GC control failed in %s", msg);
`
``
206
`+
return NULL;
`
``
207
`+
}
`
147
208
``
148
209
`static PyObject*
`
149
210
`test_list_api(PyObject *self, PyObject *Py_UNUSED(ignored))
`
`@@ -5544,6 +5605,7 @@ static PyMethodDef TestMethods[] = {
`
5544
5605
` {"PyDateTime_DATE_GET", test_PyDateTime_DATE_GET, METH_O},
`
5545
5606
` {"PyDateTime_TIME_GET", test_PyDateTime_TIME_GET, METH_O},
`
5546
5607
` {"PyDateTime_DELTA_GET", test_PyDateTime_DELTA_GET, METH_O},
`
``
5608
`+
{"test_gc_control", test_gc_control, METH_NOARGS},
`
5547
5609
` {"test_list_api", test_list_api, METH_NOARGS},
`
5548
5610
` {"test_dict_iteration", test_dict_iteration, METH_NOARGS},
`
5549
5611
` {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS},
`