99#include "pycore_initconfig.h" // _PyStatus_NO_MEMORY()
1010#include "pycore_interp.h" // PyInterpreterState.gc
1111#include "pycore_interpframe.h" // _PyFrame_GetLocalsArray()
12+ #include "pycore_list.h" // _PyList_GetItemRef()
1213#include "pycore_object_alloc.h" // _PyObject_MallocWithType()
1314#include "pycore_pystate.h" // _PyThreadState_GET()
1415#include "pycore_tstate.h" // _PyThreadStateImpl
@@ -1940,33 +1941,36 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
19401941
19411942 /* The local variable cannot be rebound, check it for sanity */
19421943 assert (PyList_CheckExact (gcstate -> callbacks ));
1943- PyObject * info = NULL ;
1944- if (PyList_GET_SIZE (gcstate -> callbacks ) != 0 ) {
1945- info = Py_BuildValue ("{sisnsnsnsd}" ,
1946- "generation" , generation ,
1947- "collected" , collected ,
1948- "uncollectable" , uncollectable ,
1949- "candidates" , candidates ,
1950- "duration" , duration );
1951- if (info == NULL ) {
1952- PyErr_FormatUnraisable ("Exception ignored while "
1953- "invoking gc callbacks" );
1954- return ;
1955- }
1944+ if (PyList_GET_SIZE (gcstate -> callbacks ) == 0 ) {
1945+ return ;
1946+ }
1947+
1948+ PyObject * info = Py_BuildValue ("{sisnsnsnsd}" ,
1949+ "generation" , generation ,
1950+ "collected" , collected ,
1951+ "uncollectable" , uncollectable ,
1952+ "candidates" , candidates ,
1953+ "duration" , duration );
1954+ if (info == NULL ) {
1955+ PyErr_FormatUnraisable ("Exception ignored while "
1956+ "invoking gc callbacks" );
1957+ return ;
19561958 }
19571959
19581960 PyObject * phase_obj = PyUnicode_FromString (phase );
19591961 if (phase_obj == NULL ) {
1960- Py_XDECREF (info );
1962+ Py_DECREF (info );
19611963 PyErr_FormatUnraisable ("Exception ignored while "
19621964 "invoking gc callbacks" );
19631965 return ;
19641966 }
19651967
19661968 PyObject * stack [] = {phase_obj , info };
19671969 for (Py_ssize_t i = 0 ; i < PyList_GET_SIZE (gcstate -> callbacks ); i ++ ) {
1968- PyObject * r , * cb = PyList_GET_ITEM (gcstate -> callbacks , i );
1969- Py_INCREF (cb ); /* make sure cb doesn't go away */
1970+ PyObject * r , * cb = _PyList_GetItemRef ((PyListObject * )gcstate -> callbacks , i );
1971+ if (cb == NULL ) {
1972+ break ;
1973+ }
19701974 r = PyObject_Vectorcall (cb , stack , 2 , NULL );
19711975 if (r == NULL ) {
19721976 PyErr_FormatUnraisable ("Exception ignored while "
@@ -1978,7 +1982,7 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
19781982 Py_DECREF (cb );
19791983 }
19801984 Py_DECREF (phase_obj );
1981- Py_XDECREF (info );
1985+ Py_DECREF (info );
19821986 assert (!_PyErr_Occurred (tstate ));
19831987}
19841988
0 commit comments