mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
true reason for allocating the tuple before checking the dict size.
This commit is contained in:
parent
eb28ef209e
commit
f4b33f61fb
1 changed files with 8 additions and 5 deletions
|
@ -1362,11 +1362,14 @@ dict_popitem(dictobject *mp, PyObject *args)
|
||||||
|
|
||||||
if (!PyArg_NoArgs(args))
|
if (!PyArg_NoArgs(args))
|
||||||
return NULL;
|
return NULL;
|
||||||
/* Allocate the result tuple first. Believe it or not,
|
/* Allocate the result tuple before checking the size. Believe it
|
||||||
* this allocation could trigger a garbage collection which
|
* or not, this allocation could trigger a garbage collection which
|
||||||
* could resize the dict, which would invalidate the pointer
|
* could empty the dict, so if we checked the size first and that
|
||||||
* (ep) into the dict calculated below.
|
* happened, the result would be an infinite loop (searching for an
|
||||||
* So we have to do this first.
|
* 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);
|
res = PyTuple_New(2);
|
||||||
if (res == NULL)
|
if (res == NULL)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue