New submission from Serhiy Storchaka:

Currently dict iterating is guarded against changing dict's size. However when 
dict changed during iteration so that it's size left unchanged, this 
modification left unnoticed.

>>> d = dict.fromkeys('abcd')
>>> for i in d:
...     print(i)
...     d[i + 'x'] = None
...     del d[i]
... 
d
a
dx
dxx
ax
c
b

In general iterating over mutating dict considered logical error. It is good 
detect it as early as possible.

The proposed patch introduces a counter which changed every time when added or 
removed key. If an iterator detects that this counter is changed, it raises 
runtime error.

----------
components: Interpreter Core
files: dict_mutating_iteration.patch
keywords: patch
messages: 200784
nosy: pitrou, rhettinger, serhiy.storchaka
priority: normal
severity: normal
stage: patch review
status: open
title: Guard against changing dict during iteration
type: enhancement
versions: Python 3.4
Added file: http://bugs.python.org/file32279/dict_mutating_iteration.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue19332>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to