[Python-Dev] Issue 19332: Guard against changing dict during iteration (original) (raw)
Ethan Furman ethan at stoneleaf.us
Wed Nov 6 05:38:09 CET 2013
- Previous message: [Python-Dev] [Python-checkins] cpython (3.3): Issue #18345: Added cookbook example illustrating handler customisation.
- Next message: [Python-Dev] Issue 19332: Guard against changing dict during iteration
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
http://bugs.python.org/issue19332
Summary:
--> d = {1: 'one'} --> for k in d: ... d[k+1] = d[k] * k ... Traceback (most recent call last): File "", line 1, in RuntimeError: dictionary changed size during iteration
--> for k in d: ... del d[k] ... Traceback (most recent call last): File "", line 1, in RuntimeError: dictionary changed size during iteration
While the above two cases are covered, the following is not:
--> d = dict.fromkeys('abcd') --> for i in d: ... print(i) ... d[i + 'x'] = None ... del d[i] ... d a dx dxx ax c b
Which can yield pretty bizarre results and does not raise an exception.
There is a patch to fix this.
Raymond's first comment was:
"The decision to not monitor adding or removing keys was intentional. It is just not worth the cost in either time or space."
Considering that the first two scenarios do raise exceptions that statement does not seem correct, at least not with current code.
Later, Raymend rejected the patch with the comment:
"I disagree with adding such unimportant code to the critical path."
I understand we need to keep the critical path as fast as possible, and that it is a balancing act between completely correct code and warnings in the docs.
What I'm hoping for is either someone to shed some light on what is the "unimportant" part, or perhaps some performance measurements that would show there is no performance based reason to not have the patch added. (I can try to do the performance part myself if necessary, I'm just not sure what all the steps are yet.)
--
Ethan
- Previous message: [Python-Dev] [Python-checkins] cpython (3.3): Issue #18345: Added cookbook example illustrating handler customisation.
- Next message: [Python-Dev] Issue 19332: Guard against changing dict during iteration
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]