Skip to content

Commit f70e54c

Browse files
authored
[3.14] gh-159739: Handle OrderedDict item iterator allocation failures (GH-159774) (#159930)
Keep the tuple construction API used by Python 3.14. (cherry picked from commit c874649)
1 parent 9d6f05e commit f70e54c

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid a crash when memory allocation fails while creating a
2+
:class:`collections.OrderedDict` items iterator.

‎Objects/odictobject.c‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,22 +1945,21 @@ odictiter_new(PyODictObject *od, int kind)
19451945
odictiterobject *di;
19461946
_ODictNode *node;
19471947
int reversed = kind & _odict_ITER_REVERSED;
1948-
1949-
di = PyObject_GC_New(odictiterobject, &PyODictIter_Type);
1950-
if (di == NULL)
1951-
return NULL;
1948+
PyObject *result = NULL;
19521949

19531950
if ((kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS) {
1954-
di->di_result = PyTuple_Pack(2, Py_None, Py_None);
1955-
if (di->di_result == NULL) {
1956-
Py_DECREF(di);
1951+
result = PyTuple_Pack(2, Py_None, Py_None);
1952+
if (result == NULL) {
19571953
return NULL;
19581954
}
19591955
}
1960-
else {
1961-
di->di_result = NULL;
1956+
di = PyObject_GC_New(odictiterobject, &PyODictIter_Type);
1957+
if (di == NULL) {
1958+
Py_XDECREF(result);
1959+
return NULL;
19621960
}
19631961

1962+
di->di_result = result;
19641963
di->kind = kind;
19651964
node = reversed ? _odict_LAST(od) : _odict_FIRST(od);
19661965
di->di_current = node ? Py_NewRef(_odictnode_KEY(node)) : NULL;

0 commit comments

Comments
 (0)