Skip to content
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

bpo-28254: Add a C-API for controlling the state of the garbage collector #25687

Merged
merged 17 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,13 @@ New Features
singleton or the ``False`` singleton.
(Contributed by Victor Stinner in :issue:`43753`.)

* Add new functions to quickly control the garbage collector from C code:
:c:func:`PyGC_Enable()`,
:c:func:`PyGC_Disable()`,
:c:func:`PyGC_IsEnabled()`.
scoder marked this conversation as resolved.
Show resolved Hide resolved
These functions allow to activate, deactivate and query the state of the garbage collector from C code without
having to import the :mod:`gc` module.

Porting to Python 3.10
----------------------

Expand Down
4 changes: 4 additions & 0 deletions Include/objimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);

/* C equivalent of gc.collect() which ignores the state of gc.enabled. */
PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
/* C API for controlling the state of the garbage collector */
PyAPI_FUNC(int) PyGC_Enable(void);
PyAPI_FUNC(int) PyGC_Disable(void);
PyAPI_FUNC(int) PyGC_IsEnabled(void);

/* Test if a type has a GC head */
#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add new C-API functions to control the state of the garbage collector:
scoder marked this conversation as resolved.
Show resolved Hide resolved
:c:func:`PyGC_Enable()`, :c:func:`PyGC_Disable()`, :c:func:`PyGC_IsEnabled()`,
corresponding to the functions in the :mod:`gc` module.
62 changes: 62 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,67 @@ test_sizeof_c_types(PyObject *self, PyObject *Py_UNUSED(ignored))
#endif
}

static PyObject*
test_gc_control(PyObject *self, PyObject *Py_UNUSED(ignored))
{
int orig_enabled = PyGC_IsEnabled();
const char* msg = "ok";
int old_state;

old_state = PyGC_Enable();
msg = "Enable(1)";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bugs are unlikely. IMO using assert() is good enough for _testcapi tests. You can leave the code as it is ;-)

See for example test_refcount_funcs() which uses assert().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that assertions can be compiled out, which is decidedly not intended here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general yes, but _testcapimodule.c starts with:

/* Always enable assertions */
#undef NDEBUG

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know! I'd keep the test as it is though. It doesn't really hurt, I think.

if (old_state != orig_enabled) {
goto failed;
}
msg = "IsEnabled(1)";
if (!PyGC_IsEnabled()) {
goto failed;
}

old_state = PyGC_Disable();
msg = "disable(2)";
if (!old_state) {
goto failed;
}
msg = "IsEnabled(2)";
if (PyGC_IsEnabled()) {
goto failed;
}

old_state = PyGC_Enable();
msg = "enable(3)";
if (old_state) {
goto failed;
}
msg = "IsEnabled(3)";
if (!PyGC_IsEnabled()) {
goto failed;
}

if (!orig_enabled) {
old_state = PyGC_Disable();
msg = "disable(4)";
if (old_state) {
goto failed;
}
msg = "IsEnabled(4)";
if (PyGC_IsEnabled()) {
goto failed;
}
}

Py_RETURN_NONE;

failed:
/* Try to clean up if we can. */
if (orig_enabled) {
PyGC_Enable();
} else {
PyGC_Disable();
}
PyErr_Format(TestError, "GC control failed in %s", msg);
return NULL;
}

static PyObject*
test_list_api(PyObject *self, PyObject *Py_UNUSED(ignored))
Expand Down Expand Up @@ -5544,6 +5605,7 @@ static PyMethodDef TestMethods[] = {
{"PyDateTime_DATE_GET", test_PyDateTime_DATE_GET, METH_O},
{"PyDateTime_TIME_GET", test_PyDateTime_TIME_GET, METH_O},
{"PyDateTime_DELTA_GET", test_PyDateTime_DELTA_GET, METH_O},
{"test_gc_control", test_gc_control, METH_NOARGS},
{"test_list_api", test_list_api, METH_NOARGS},
{"test_dict_iteration", test_dict_iteration, METH_NOARGS},
{"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS},
Expand Down
35 changes: 29 additions & 6 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1484,8 +1484,7 @@ static PyObject *
gc_enable_impl(PyObject *module)
/*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/
{
GCState *gcstate = get_gc_state();
gcstate->enabled = 1;
PyGC_Enable();
Py_RETURN_NONE;
}

Expand All @@ -1499,8 +1498,7 @@ static PyObject *
gc_disable_impl(PyObject *module)
/*[clinic end generated code: output=97d1030f7aa9d279 input=8c2e5a14e800d83b]*/
{
GCState *gcstate = get_gc_state();
gcstate->enabled = 0;
PyGC_Disable();
Py_RETURN_NONE;
}

Expand All @@ -1514,8 +1512,7 @@ static int
gc_isenabled_impl(PyObject *module)
/*[clinic end generated code: output=1874298331c49130 input=30005e0422373b31]*/
{
GCState *gcstate = get_gc_state();
return gcstate->enabled;
return PyGC_IsEnabled();
}

/*[clinic input]
Expand Down Expand Up @@ -2081,6 +2078,32 @@ PyGC_Collect(void)
return n;
}

/* C API for controlling the state of the garbage collector */
int
PyGC_Enable(void)
{
GCState *gcstate = get_gc_state();
int old_state = gcstate->enabled;
gcstate->enabled = 1;
return old_state;
}

int
PyGC_Disable(void)
{
GCState *gcstate = get_gc_state();
int old_state = gcstate->enabled;
gcstate->enabled = 0;
return old_state;
}

int
PyGC_IsEnabled(void)
{
GCState *gcstate = get_gc_state();
return gcstate->enabled;
}

Py_ssize_t
_PyGC_CollectNoFail(PyThreadState *tstate)
{
Expand Down