Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
bpo-1635741: Port gc module to multiphase initialization
Signed-off-by: Christian Heimes <christian@python.org>
  • Loading branch information
tiran committed Nov 19, 2020
commit 271e017401075d1e4faad287daf863ac384ce849
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port :mod:`gc` extension module to multiphase initialization (:pep:`489`)
68 changes: 32 additions & 36 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1992,59 +1992,55 @@ static PyMethodDef GcMethods[] = {
{NULL, NULL} /* Sentinel */
};

static struct PyModuleDef gcmodule = {
PyModuleDef_HEAD_INIT,
"gc", /* m_name */
gc__doc__, /* m_doc */
-1, /* m_size */
GcMethods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL /* m_free */
};

PyMODINIT_FUNC
PyInit_gc(void)
static int
gcmodule_exec(PyObject *module)
{
GCState *gcstate = get_gc_state();

PyObject *m = PyModule_Create(&gcmodule);

if (m == NULL) {
return NULL;
}

gcstate->garbage = PyList_New(0);
if (gcstate->garbage == NULL) {
gcstate->garbage = PyList_New(0);
if (gcstate->garbage == NULL) {
return NULL;
}
return -1;
}
Py_INCREF(gcstate->garbage);
if (PyModule_AddObject(m, "garbage", gcstate->garbage) < 0) {
return NULL;
if (PyModule_AddObjectRef(module, "garbage", gcstate->garbage) < 0) {
return -1;
}

gcstate->callbacks = PyList_New(0);
if (gcstate->callbacks == NULL) {
gcstate->callbacks = PyList_New(0);
if (gcstate->callbacks == NULL) {
return NULL;
}
return -1;
}
Py_INCREF(gcstate->callbacks);
if (PyModule_AddObject(m, "callbacks", gcstate->callbacks) < 0) {
return NULL;
if (PyModule_AddObjectRef(module, "callbacks", gcstate->callbacks) < 0) {
return -1;
}

#define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) { return NULL; }
#define ADD_INT(NAME) if (PyModule_AddIntConstant(module, #NAME, NAME) < 0) { return -1; }
ADD_INT(DEBUG_STATS);
ADD_INT(DEBUG_COLLECTABLE);
ADD_INT(DEBUG_UNCOLLECTABLE);
ADD_INT(DEBUG_SAVEALL);
ADD_INT(DEBUG_LEAK);
#undef ADD_INT
return m;
return 0;
}

static PyModuleDef_Slot gcmodule_slots[] = {
{Py_mod_exec, gcmodule_exec},
{0, NULL}
};

static struct PyModuleDef gcmodule = {
PyModuleDef_HEAD_INIT,
.m_name = "gc",
.m_doc = gc__doc__,
.m_size = 0, /* special case, state is part of interpreter state */
Comment thread
tiran marked this conversation as resolved.
Outdated
.m_methods = GcMethods,
.m_slots = gcmodule_slots
};

PyMODINIT_FUNC
PyInit_gc(void)
{
return PyModuleDef_Init(&gcmodule);
}

/* Public API to invoke gc.collect() from C */
Expand Down