Discussion:
Mutating while iterating
Aaron Brady
2014-07-26 20:51:16 UTC
Permalink
Hi, I asked about the inconsistency of the "RuntimeError" being raised when
mutating a container while iterating over it here [1], "set and dict
iteration" on Aug 16, 2012.

[1] http://www.gossamer-threads.com/lists/python/python/1004659

Continuing new from the bugs issue page [2]:

[2] http://bugs.python.org/issue22084

Other prior discussion [3] [4]:

[3] http://bugs.python.org/issue19332

[4] http://bugs.python.org/issue6017

Thanks Mr. Storchaka for your comments. The new documentation didn't help.
The current behavior is still a rare but inconsistent silent error. An
implementation sketch in pseudocode might simplify the endeavor [5]:

[5]
http://home.comcast.net/~castironpi-misc/irc-0168%20mutating%20while%20iterating%20markup.html

I gather we wouldn't want to pursue the "custom" data container, option
"2e": we would still need both "malloc/free" and a reference count.
Nick Coghlan
2014-07-27 03:39:59 UTC
Permalink
Post by Aaron Brady
Hi, I asked about the inconsistency of the "RuntimeError" being raised when
mutating a container while iterating over it here [1], "set and dict
iteration" on Aug 16, 2012.
Hi,

This is clearly an issue of grave concern to you, but as Raymond
pointed out previously, you appear to have misunderstood the purpose
of those exceptions. They're there to prevent catastrophic failure of
the interpreter itself (i.e. segmentation faults), not to help find
bugs in user code. If users want to mutate containers while they're
iterating over them, they're generally free to do so. The only time
we'll actively disallow it is when such mutation will outright *break*
the iterator, rather than merely producing potentially surprising
results.

I have closed the new issue and added a longer reply (with examples)
that will hopefully better explain why we have no intention of
changing this behaviour: http://bugs.python.org/issue22084#msg224100

Regards,
Nick.
--
Nick Coghlan | ncoghlan-***@public.gmane.org | Brisbane, Australia
Aaron Brady
2014-08-03 17:46:08 UTC
Permalink
Post by Nick Coghlan
Post by Aaron Brady
Hi, I asked about the inconsistency of the "RuntimeError" being raised when
mutating a container while iterating over it here [1], "set and dict
iteration" on Aug 16, 2012.
Hi,
This is clearly an issue of grave concern to you, but as Raymond
pointed out previously, you appear to have misunderstood the purpose
of those exceptions. They're there to prevent catastrophic failure of
the interpreter itself (i.e. segmentation faults), not to help find
bugs in user code. If users want to mutate containers while they're
iterating over them, they're generally free to do so. The only time
we'll actively disallow it is when such mutation will outright *break*
the iterator, rather than merely producing potentially surprising
results.
I have closed the new issue and added a longer reply (with examples)
that will hopefully better explain why we have no intention of
changing this behaviour: http://bugs.python.org/issue22084#msg224100
Python is replete with examples of prohibiting structures which are
likely bugs but aren't segfaults. There are also reciprocal-- though
not necessarily inverse-- limitations of the unordered collections
themselves ("set" and "dict"). The new behavior is transparent for
the programmer; no possible programs can /rely/ on the existing
behavior. The new behavior introduces no new objects, possibly except
"IterationError", no new syntax, and no new costs.

I propose we leave this discussion thread open for the time being. I
also take the issue of /re/-assigning to keys during iteration to be
settled as permitted.

Continue reading on narkive:
Loading...