diff options
author | Brandt Bucher <brandtbucher@gmail.com> | 2020-12-07 20:07:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-07 20:07:48 (GMT) |
commit | 60463e8e4f79e5b5e96dc43fb83ded373b489e33 (patch) | |
tree | a396c7ee5d9b3db595fac4980fc0700e4fe8a2e9 /Objects | |
parent | e9a6dcdefabb6c19074566f4ee0e02daaf57be18 (diff) | |
download | cpython-60463e8e4f79e5b5e96dc43fb83ded373b489e33.zip cpython-60463e8e4f79e5b5e96dc43fb83ded373b489e33.tar.gz cpython-60463e8e4f79e5b5e96dc43fb83ded373b489e33.tar.bz2 |
bpo-42536: GC track recycled tuples (GH-23623) (GH-23651)
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.
(cherry picked from commit 226a012d1cd61f42ecd3056c554922f359a1a35d)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/dictobject.c | 10 | ||||
-rw-r--r-- | Objects/enumobject.c | 11 | ||||
-rw-r--r-- | Objects/odictobject.c | 5 |
3 files changed, 26 insertions, 0 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index faee6bc..8a05653 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -3861,6 +3861,11 @@ dictiter_iternextitem(dictiterobject *di) Py_INCREF(result); Py_DECREF(oldkey); Py_DECREF(oldvalue); + // 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); + } } else { result = PyTuple_New(2); @@ -3976,6 +3981,11 @@ dictreviter_iternext(dictiterobject *di) Py_INCREF(result); Py_DECREF(oldkey); Py_DECREF(oldvalue); + // 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); + } } else { result = PyTuple_New(2); diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 4a83bb4..bdd0ea5 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -1,6 +1,7 @@ /* enumerate object */ #include "Python.h" +#include "pycore_object.h" // _PyObject_GC_TRACK() #include "clinic/enumobject.c.h" @@ -130,6 +131,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); @@ -175,6 +181,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); diff --git a/Objects/odictobject.c b/Objects/odictobject.c index d5bf499..b9f2169 100644 --- a/Objects/odictobject.c +++ b/Objects/odictobject.c @@ -1817,6 +1817,11 @@ odictiter_iternext(odictiterobject *di) Py_INCREF(result); Py_DECREF(PyTuple_GET_ITEM(result, 0)); /* borrowed */ Py_DECREF(PyTuple_GET_ITEM(result, 1)); /* borrowed */ + // 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); + } } else { result = PyTuple_New(2); |