Skip to content

Commit 7ad98d9

Browse files
committed
Create gc callbacks in _PyGC_init()
1 parent 331d676 commit 7ad98d9

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

Modules/gcmodule.c

+14-17
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,17 @@ PyStatus
165165
_PyGC_Init(PyThreadState *tstate)
166166
{
167167
GCState *gcstate = &tstate->interp->gc;
168+
169+
gcstate->garbage = PyList_New(0);
168170
if (gcstate->garbage == NULL) {
169-
gcstate->garbage = PyList_New(0);
170-
if (gcstate->garbage == NULL) {
171-
return _PyStatus_NO_MEMORY();
172-
}
171+
return _PyStatus_NO_MEMORY();
172+
}
173+
174+
gcstate->callbacks = PyList_New(0);
175+
if (gcstate->callbacks == NULL) {
176+
return _PyStatus_NO_MEMORY();
173177
}
178+
174179
return _PyStatus_OK();
175180
}
176181

@@ -1997,21 +2002,13 @@ gcmodule_exec(PyObject *module)
19972002
{
19982003
GCState *gcstate = get_gc_state();
19992004

2000-
/* Initialized by _PyGC_Init() early in interpreter lifecycle */
2001-
if (gcstate->garbage == NULL) {
2002-
PyErr_SetString(PyExc_SystemError,
2003-
"GC garbage bin is not initialized");
2004-
return -1;
2005-
}
2005+
/* garbage and callbacks are initialized by _PyGC_Init() early in
2006+
* interpreter lifecycle. */
2007+
assert(gcstate->garbage != NULL);
20062008
if (PyModule_AddObjectRef(module, "garbage", gcstate->garbage) < 0) {
20072009
return -1;
20082010
}
2009-
2010-
assert(gcstate->callbacks == NULL);
2011-
gcstate->callbacks = PyList_New(0);
2012-
if (gcstate->callbacks == NULL) {
2013-
return -1;
2014-
}
2011+
assert(gcstate->callbacks != NULL);
20152012
if (PyModule_AddObjectRef(module, "callbacks", gcstate->callbacks) < 0) {
20162013
return -1;
20172014
}
@@ -2035,7 +2032,7 @@ static struct PyModuleDef gcmodule = {
20352032
PyModuleDef_HEAD_INIT,
20362033
.m_name = "gc",
20372034
.m_doc = gc__doc__,
2038-
.m_size = 0, /* special case, state is part of interpreter state */
2035+
.m_size = 0, // per interpreter state, see: get_gc_state()
20392036
.m_methods = GcMethods,
20402037
.m_slots = gcmodule_slots
20412038
};

0 commit comments

Comments
 (0)