No mutation check in dict.lookdict_unicode()?

I noticed that lookdict() checks if the dict is mutated during lookup:

if (dk == mp->ma_keys && ep->me_key == startkey) {
    // code
}
else {
    /* The dict was mutated, restart */
    goto top;
}

This check is not present in lookdict_unicode() and lookdict_unicode_nodummy().

Is this check really needed for lookup? If so, why it’s not present in dicts with only string keys?

Because they do not use PyObject_RichCompareBool() which can release the GIL and execute arbitrary user code.

5 Likes