bpo-33622: Fix issues with handling errors in the GC. (GH-7078) · python/cpython@2fe940c (original) (raw)

`@@ -648,10 +648,8 @@ debug_cycle(const char *msg, PyObject *op)

`

648

648

` * garbage list (a Python list), else only the objects in finalizers with

`

649

649

` * del methods are appended to garbage. All objects in finalizers are

`

650

650

` * merged into the old list regardless.

`

651

``

`-

`

652

``

`-

`

653

651

` */

`

654

``

`-

static int

`

``

652

`+

static void

`

655

653

`handle_legacy_finalizers(PyGC_Head *finalizers, PyGC_Head *old)

`

656

654

`{

`

657

655

`PyGC_Head *gc = finalizers->gc.gc_next;

`

`@@ -666,12 +664,11 @@ handle_legacy_finalizers(PyGC_Head *finalizers, PyGC_Head *old)

`

666

664

``

667

665

`if ((_PyRuntime.gc.debug & DEBUG_SAVEALL) || has_legacy_finalizer(op)) {

`

668

666

`if (PyList_Append(_PyRuntime.gc.garbage, op) < 0)

`

669

``

`-

return -1;

`

``

667

`+

break;

`

670

668

` }

`

671

669

` }

`

672

670

``

673

671

`gc_list_merge(finalizers, old);

`

674

``

`-

return 0;

`

675

672

`}

`

676

673

``

677

674

`/* Run first-time finalizers (if any) on all the objects in collectable.

`

`@@ -945,7 +942,7 @@ collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable,

`

945

942

` * reachable list of garbage. The programmer has to deal with

`

946

943

` * this if they insist on creating this type of structure.

`

947

944

` */

`

948

``

`-

(void)handle_legacy_finalizers(&finalizers, old);

`

``

945

`+

handle_legacy_finalizers(&finalizers, old);

`

949

946

``

950

947

`/* Clear free list only during the collection of the highest

`

951

948

` * generation */

`

`@@ -1009,9 +1006,12 @@ invoke_gc_callback(const char *phase, int generation,

`

1009

1006

`PyObject *r, *cb = PyList_GET_ITEM(_PyRuntime.gc.callbacks, i);

`

1010

1007

`Py_INCREF(cb); /* make sure cb doesn't go away */

`

1011

1008

`r = PyObject_CallFunction(cb, "sO", phase, info);

`

1012

``

`-

Py_XDECREF(r);

`

1013

``

`-

if (r == NULL)

`

``

1009

`+

if (r == NULL) {

`

1014

1010

`PyErr_WriteUnraisable(cb);

`

``

1011

`+

}

`

``

1012

`+

else {

`

``

1013

`+

Py_DECREF(r);

`

``

1014

`+

}

`

1015

1015

`Py_DECREF(cb);

`

1016

1016

` }

`

1017

1017

`Py_XDECREF(info);

`

`@@ -1567,8 +1567,11 @@ PyGC_Collect(void)

`

1567

1567

`if (_PyRuntime.gc.collecting)

`

1568

1568

`n = 0; /* already collecting, don't do anything */

`

1569

1569

`else {

`

``

1570

`+

PyObject *exc, *value, *tb;

`

1570

1571

`_PyRuntime.gc.collecting = 1;

`

``

1572

`+

PyErr_Fetch(&exc, &value, &tb);

`

1571

1573

`n = collect_with_callback(NUM_GENERATIONS - 1);

`

``

1574

`+

PyErr_Restore(exc, value, tb);

`

1572

1575

`_PyRuntime.gc.collecting = 0;

`

1573

1576

` }

`

1574

1577

``