Skip to content

Commit 1b37268

Browse files
bpo-46085: Fix iterator cache mechanism of OrderedDict. (GH-30290)
(cherry picked from commit fb44d05) Co-authored-by: Dong-hee Na <[email protected]>
1 parent 9f0e40f commit 1b37268

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix iterator cache mechanism of :class:`OrderedDict`.

Objects/odictobject.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,7 @@ PyDoc_STRVAR(odict_reversed__doc__, "od.__reversed__() <==> reversed(od)");
12881288
#define _odict_ITER_REVERSED 1
12891289
#define _odict_ITER_KEYS 2
12901290
#define _odict_ITER_VALUES 4
1291+
#define _odict_ITER_ITEMS (_odict_ITER_KEYS|_odict_ITER_VALUES)
12911292

12921293
/* forward */
12931294
static PyObject * odictiter_new(PyODictObject *, int);
@@ -1705,7 +1706,7 @@ odictiter_dealloc(odictiterobject *di)
17051706
_PyObject_GC_UNTRACK(di);
17061707
Py_XDECREF(di->di_odict);
17071708
Py_XDECREF(di->di_current);
1708-
if (di->kind & (_odict_ITER_KEYS | _odict_ITER_VALUES)) {
1709+
if ((di->kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS) {
17091710
Py_DECREF(di->di_result);
17101711
}
17111712
PyObject_GC_Del(di);
@@ -1911,15 +1912,16 @@ odictiter_new(PyODictObject *od, int kind)
19111912
if (di == NULL)
19121913
return NULL;
19131914

1914-
if (kind & (_odict_ITER_KEYS | _odict_ITER_VALUES)){
1915+
if ((kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS) {
19151916
di->di_result = PyTuple_Pack(2, Py_None, Py_None);
19161917
if (di->di_result == NULL) {
19171918
Py_DECREF(di);
19181919
return NULL;
19191920
}
19201921
}
1921-
else
1922+
else {
19221923
di->di_result = NULL;
1924+
}
19231925

19241926
di->kind = kind;
19251927
node = reversed ? _odict_LAST(od) : _odict_FIRST(od);

0 commit comments

Comments
 (0)