@@ -165,12 +165,17 @@ PyStatus
165
165
_PyGC_Init (PyThreadState * tstate )
166
166
{
167
167
GCState * gcstate = & tstate -> interp -> gc ;
168
+
169
+ gcstate -> garbage = PyList_New (0 );
168
170
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 ();
173
177
}
178
+
174
179
return _PyStatus_OK ();
175
180
}
176
181
@@ -1997,21 +2002,13 @@ gcmodule_exec(PyObject *module)
1997
2002
{
1998
2003
GCState * gcstate = get_gc_state ();
1999
2004
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 );
2006
2008
if (PyModule_AddObjectRef (module , "garbage" , gcstate -> garbage ) < 0 ) {
2007
2009
return -1 ;
2008
2010
}
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 );
2015
2012
if (PyModule_AddObjectRef (module , "callbacks" , gcstate -> callbacks ) < 0 ) {
2016
2013
return -1 ;
2017
2014
}
@@ -2035,7 +2032,7 @@ static struct PyModuleDef gcmodule = {
2035
2032
PyModuleDef_HEAD_INIT ,
2036
2033
.m_name = "gc" ,
2037
2034
.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()
2039
2036
.m_methods = GcMethods ,
2040
2037
.m_slots = gcmodule_slots
2041
2038
};
0 commit comments