mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
bpo-42536: GC track recycled tuples (GH-23623)
Several built-in and standard library types now ensure that their internal result tuples are always tracked by the garbage collector: - collections.OrderedDict.items - dict.items - enumerate - functools.reduce - itertools.combinations - itertools.combinations_with_replacement - itertools.permutations - itertools.product - itertools.zip_longest - zip Previously, they could have become untracked by a prior garbage collection.
This commit is contained in:
parent
2de5097ba4
commit
226a012d1c
12 changed files with 192 additions and 0 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "Python.h"
|
||||
#include "pycore_long.h" // _PyLong_GetOne()
|
||||
#include "pycore_object.h" // _PyObject_GC_TRACK()
|
||||
|
||||
#include "clinic/enumobject.c.h"
|
||||
|
||||
|
@ -131,6 +132,11 @@ enum_next_long(enumobject *en, PyObject* next_item)
|
|||
PyTuple_SET_ITEM(result, 1, next_item);
|
||||
Py_DECREF(old_index);
|
||||
Py_DECREF(old_item);
|
||||
// bpo-42536: The GC may have untracked this result tuple. Since we're
|
||||
// recycling it, make sure it's tracked again:
|
||||
if (!_PyObject_GC_IS_TRACKED(result)) {
|
||||
_PyObject_GC_TRACK(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
result = PyTuple_New(2);
|
||||
|
@ -176,6 +182,11 @@ enum_next(enumobject *en)
|
|||
PyTuple_SET_ITEM(result, 1, next_item);
|
||||
Py_DECREF(old_index);
|
||||
Py_DECREF(old_item);
|
||||
// bpo-42536: The GC may have untracked this result tuple. Since we're
|
||||
// recycling it, make sure it's tracked again:
|
||||
if (!_PyObject_GC_IS_TRACKED(result)) {
|
||||
_PyObject_GC_TRACK(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
result = PyTuple_New(2);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue