Skip to content

Commit 271e017

Browse files
committed
bpo-1635741: Port gc module to multiphase initialization
Signed-off-by: Christian Heimes <[email protected]>
1 parent fa2eee9 commit 271e017

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port :mod:`gc` extension module to multiphase initialization (:pep:`489`)

Modules/gcmodule.c

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,59 +1992,55 @@ static PyMethodDef GcMethods[] = {
19921992
{NULL, NULL} /* Sentinel */
19931993
};
19941994

1995-
static struct PyModuleDef gcmodule = {
1996-
PyModuleDef_HEAD_INIT,
1997-
"gc", /* m_name */
1998-
gc__doc__, /* m_doc */
1999-
-1, /* m_size */
2000-
GcMethods, /* m_methods */
2001-
NULL, /* m_reload */
2002-
NULL, /* m_traverse */
2003-
NULL, /* m_clear */
2004-
NULL /* m_free */
2005-
};
2006-
2007-
PyMODINIT_FUNC
2008-
PyInit_gc(void)
1995+
static int
1996+
gcmodule_exec(PyObject *module)
20091997
{
20101998
GCState *gcstate = get_gc_state();
20111999

2012-
PyObject *m = PyModule_Create(&gcmodule);
2013-
2014-
if (m == NULL) {
2015-
return NULL;
2016-
}
2017-
2000+
gcstate->garbage = PyList_New(0);
20182001
if (gcstate->garbage == NULL) {
2019-
gcstate->garbage = PyList_New(0);
2020-
if (gcstate->garbage == NULL) {
2021-
return NULL;
2022-
}
2002+
return -1;
20232003
}
2024-
Py_INCREF(gcstate->garbage);
2025-
if (PyModule_AddObject(m, "garbage", gcstate->garbage) < 0) {
2026-
return NULL;
2004+
if (PyModule_AddObjectRef(module, "garbage", gcstate->garbage) < 0) {
2005+
return -1;
20272006
}
20282007

2008+
gcstate->callbacks = PyList_New(0);
20292009
if (gcstate->callbacks == NULL) {
2030-
gcstate->callbacks = PyList_New(0);
2031-
if (gcstate->callbacks == NULL) {
2032-
return NULL;
2033-
}
2010+
return -1;
20342011
}
2035-
Py_INCREF(gcstate->callbacks);
2036-
if (PyModule_AddObject(m, "callbacks", gcstate->callbacks) < 0) {
2037-
return NULL;
2012+
if (PyModule_AddObjectRef(module, "callbacks", gcstate->callbacks) < 0) {
2013+
return -1;
20382014
}
20392015

2040-
#define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) { return NULL; }
2016+
#define ADD_INT(NAME) if (PyModule_AddIntConstant(module, #NAME, NAME) < 0) { return -1; }
20412017
ADD_INT(DEBUG_STATS);
20422018
ADD_INT(DEBUG_COLLECTABLE);
20432019
ADD_INT(DEBUG_UNCOLLECTABLE);
20442020
ADD_INT(DEBUG_SAVEALL);
20452021
ADD_INT(DEBUG_LEAK);
20462022
#undef ADD_INT
2047-
return m;
2023+
return 0;
2024+
}
2025+
2026+
static PyModuleDef_Slot gcmodule_slots[] = {
2027+
{Py_mod_exec, gcmodule_exec},
2028+
{0, NULL}
2029+
};
2030+
2031+
static struct PyModuleDef gcmodule = {
2032+
PyModuleDef_HEAD_INIT,
2033+
.m_name = "gc",
2034+
.m_doc = gc__doc__,
2035+
.m_size = 0, /* special case, state is part of interpreter state */
2036+
.m_methods = GcMethods,
2037+
.m_slots = gcmodule_slots
2038+
};
2039+
2040+
PyMODINIT_FUNC
2041+
PyInit_gc(void)
2042+
{
2043+
return PyModuleDef_Init(&gcmodule);
20482044
}
20492045

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

0 commit comments

Comments
 (0)