[Python-checkins] bpo-46085: Fix iterator cache mechanism of OrderedDict. (GH-30290)

miss-islington webhook-mailer at python.org
Thu Dec 30 00:29:23 EST 2021


https://github.com/python/cpython/commit/2d4049da1f61df5cb4314d7e10b54fa556880b0e
commit: 2d4049da1f61df5cb4314d7e10b54fa556880b0e
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-12-29T21:29:19-08:00
summary:

bpo-46085: Fix iterator cache mechanism of OrderedDict. (GH-30290)

(cherry picked from commit fb44d0589615590b1e7895ba78a038e96b15a219)

Co-authored-by: Dong-hee Na <donghee.na at python.org>

files:
A Misc/NEWS.d/next/Core and Builtins/2021-12-30-00-23-41.bpo-46085.bDuJqu.rst
M Objects/odictobject.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-12-30-00-23-41.bpo-46085.bDuJqu.rst b/Misc/NEWS.d/next/Core and Builtins/2021-12-30-00-23-41.bpo-46085.bDuJqu.rst
new file mode 100644
index 0000000000000..a2093f75c3b62
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-12-30-00-23-41.bpo-46085.bDuJqu.rst	
@@ -0,0 +1 @@
+Fix iterator cache mechanism of :class:`OrderedDict`.
diff --git a/Objects/odictobject.c b/Objects/odictobject.c
index b9f2169a72a8a..f3980aba93776 100644
--- a/Objects/odictobject.c
+++ b/Objects/odictobject.c
@@ -1290,6 +1290,7 @@ PyDoc_STRVAR(odict_reversed__doc__, "od.__reversed__() <==> reversed(od)");
 #define _odict_ITER_REVERSED 1
 #define _odict_ITER_KEYS 2
 #define _odict_ITER_VALUES 4
+#define _odict_ITER_ITEMS (_odict_ITER_KEYS|_odict_ITER_VALUES)
 
 /* forward */
 static PyObject * odictiter_new(PyODictObject *, int);
@@ -1708,7 +1709,7 @@ odictiter_dealloc(odictiterobject *di)
     _PyObject_GC_UNTRACK(di);
     Py_XDECREF(di->di_odict);
     Py_XDECREF(di->di_current);
-    if (di->kind & (_odict_ITER_KEYS | _odict_ITER_VALUES)) {
+    if ((di->kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS) {
         Py_DECREF(di->di_result);
     }
     PyObject_GC_Del(di);
@@ -1914,15 +1915,16 @@ odictiter_new(PyODictObject *od, int kind)
     if (di == NULL)
         return NULL;
 
-    if (kind & (_odict_ITER_KEYS | _odict_ITER_VALUES)){
+    if ((kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS) {
         di->di_result = PyTuple_Pack(2, Py_None, Py_None);
         if (di->di_result == NULL) {
             Py_DECREF(di);
             return NULL;
         }
     }
-    else
+    else {
         di->di_result = NULL;
+    }
 
     di->kind = kind;
     node = reversed ? _odict_LAST(od) : _odict_FIRST(od);



More information about the Python-checkins mailing list