@@ -751,10 +751,8 @@ debug_cycle(const char *msg, PyObject *op)
751751 * garbage list (a Python list), else only the objects in finalizers with
752752 * __del__ methods are appended to garbage. All objects in finalizers are
753753 * merged into the old list regardless.
754- * Returns 0 if all OK, <0 on error (out of memory to grow the garbage list).
755- * The finalizers list is made empty on a successful return.
756754 */
757- static int
755+ static void
758756handle_legacy_finalizers (PyGC_Head * finalizers , PyGC_Head * old )
759757{
760758 PyGC_Head * gc = finalizers -> gc .gc_next ;
@@ -769,12 +767,11 @@ handle_legacy_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
769767
770768 if ((debug & DEBUG_SAVEALL ) || has_legacy_finalizer (op )) {
771769 if (PyList_Append (garbage , op ) < 0 )
772- return -1 ;
770+ break ;
773771 }
774772 }
775773
776774 gc_list_merge (finalizers , old );
777- return 0 ;
778775}
779776
780777/* Run first-time finalizers (if any) on all the objects in collectable.
@@ -1045,7 +1042,7 @@ collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable,
10451042 * reachable list of garbage. The programmer has to deal with
10461043 * this if they insist on creating this type of structure.
10471044 */
1048- ( void ) handle_legacy_finalizers (& finalizers , old );
1045+ handle_legacy_finalizers (& finalizers , old );
10491046
10501047 /* Clear free list only during the collection of the highest
10511048 * generation */
@@ -1109,9 +1106,12 @@ invoke_gc_callback(const char *phase, int generation,
11091106 PyObject * r , * cb = PyList_GET_ITEM (callbacks , i );
11101107 Py_INCREF (cb ); /* make sure cb doesn't go away */
11111108 r = PyObject_CallFunction (cb , "sO" , phase , info );
1112- Py_XDECREF (r );
1113- if (r == NULL )
1109+ if (r == NULL ) {
11141110 PyErr_WriteUnraisable (cb );
1111+ }
1112+ else {
1113+ Py_DECREF (r );
1114+ }
11151115 Py_DECREF (cb );
11161116 }
11171117 Py_XDECREF (info );
@@ -1588,8 +1588,11 @@ PyGC_Collect(void)
15881588 if (collecting )
15891589 n = 0 ; /* already collecting, don't do anything */
15901590 else {
1591+ PyObject * exc , * value , * tb ;
15911592 collecting = 1 ;
1593+ PyErr_Fetch (& exc , & value , & tb );
15921594 n = collect_with_callback (NUM_GENERATIONS - 1 );
1595+ PyErr_Restore (exc , value , tb );
15931596 collecting = 0 ;
15941597 }
15951598
0 commit comments