Commit 4aa86795 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #23783: Fixed memory leak in PyObject_ClearWeakRefs() in case of

MemoryError.
parent 7d0325d6
...@@ -900,11 +900,9 @@ PyObject_ClearWeakRefs(PyObject *object) ...@@ -900,11 +900,9 @@ PyObject_ClearWeakRefs(PyObject *object)
if (*list != NULL) { if (*list != NULL) {
PyWeakReference *current = *list; PyWeakReference *current = *list;
Py_ssize_t count = _PyWeakref_GetWeakrefCount(current); Py_ssize_t count = _PyWeakref_GetWeakrefCount(current);
int restore_error = PyErr_Occurred() ? 1 : 0;
PyObject *err_type, *err_value, *err_tb; PyObject *err_type, *err_value, *err_tb;
if (restore_error) PyErr_Fetch(&err_type, &err_value, &err_tb);
PyErr_Fetch(&err_type, &err_value, &err_tb);
if (count == 1) { if (count == 1) {
PyObject *callback = current->wr_callback; PyObject *callback = current->wr_callback;
...@@ -922,8 +920,7 @@ PyObject_ClearWeakRefs(PyObject *object) ...@@ -922,8 +920,7 @@ PyObject_ClearWeakRefs(PyObject *object)
tuple = PyTuple_New(count * 2); tuple = PyTuple_New(count * 2);
if (tuple == NULL) { if (tuple == NULL) {
if (restore_error) _PyErr_ChainExceptions(err_type, err_value, err_tb);
PyErr_Fetch(&err_type, &err_value, &err_tb);
return; return;
} }
...@@ -954,7 +951,7 @@ PyObject_ClearWeakRefs(PyObject *object) ...@@ -954,7 +951,7 @@ PyObject_ClearWeakRefs(PyObject *object)
} }
Py_DECREF(tuple); Py_DECREF(tuple);
} }
if (restore_error) assert(!PyErr_Occurred());
PyErr_Restore(err_type, err_value, err_tb); PyErr_Restore(err_type, err_value, err_tb);
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment