[Python-Dev] Making sure dictionary adds/deletes during iteration always raise exception

Joe Jevnik jjevnik at quantopian.com
Tue Dec 13 13:13:13 EST 2016


> Is it possible to add a key, triggering a resize of the dict, then
remove one, and continue iterating through the old (deallocated)
memory?

You can add and remove keys between calling next which would resize the
dictionary; however, it will not iterate through uninitialized memory. The
dictiter holds the current index and each time next is called it goes
directly to ma_keys->dk_entries[saved_index] or ma_values[saved_index]

On Tue, Dec 13, 2016 at 12:55 PM, Chris Angelico <rosuav at gmail.com> wrote:

> On Wed, Dec 14, 2016 at 4:48 AM, Guido van Rossum <guido at python.org>
> wrote:
> > IIUC the private version gets updated every time the dict gets modified
> --
> > but what we need here should only trigger when a key is added or removed,
> > not when a value is updated.
>
> Is it possible to add a key, triggering a resize of the dict, then
> remove one, and continue iterating through the old (deallocated)
> memory? If so, that could potentially cause a crash.
>
> ChrisA
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> joe%40quantopian.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20161213/b6561bfa/attachment.html>


More information about the Python-Dev mailing list