summaryrefslogtreecommitdiffstats
path: root/Objects/dictobject.c
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-06-02 05:42:29 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-06-02 05:42:29 (GMT)
commitf4b33f61fb83f54d3086ef28c34e71a18ade2b9d (patch)
treeffe4c791168d7a113d42b2b864ce42196ddb20a2 /Objects/dictobject.c
parenteb28ef209eb0ce0b0975ee91331d02652ee3c41c (diff)
downloadcpython-f4b33f61fb83f54d3086ef28c34e71a18ade2b9d.zip
cpython-f4b33f61fb83f54d3086ef28c34e71a18ade2b9d.tar.gz
cpython-f4b33f61fb83f54d3086ef28c34e71a18ade2b9d.tar.bz2
dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
true reason for allocating the tuple before checking the dict size.
Diffstat (limited to 'Objects/dictobject.c')
-rw-r--r--Objects/dictobject.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 754c750..857b3c6 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -1362,11 +1362,14 @@ dict_popitem(dictobject *mp, PyObject *args)
if (!PyArg_NoArgs(args))
return NULL;
- /* Allocate the result tuple first. Believe it or not,
- * this allocation could trigger a garbage collection which
- * could resize the dict, which would invalidate the pointer
- * (ep) into the dict calculated below.
- * So we have to do this first.
+ /* Allocate the result tuple before checking the size. Believe it
+ * or not, this allocation could trigger a garbage collection which
+ * could empty the dict, so if we checked the size first and that
+ * happened, the result would be an infinite loop (searching for an
+ * entry that no longer exists). Note that the usual popitem()
+ * idiom is "while d: k, v = d.popitem()". so needing to throw the
+ * tuple away if the dict *is* empty isn't a significant
+ * inefficiency -- possible, but unlikely in practice.
*/
res = PyTuple_New(2);
if (res == NULL)