[issue24369] Using OrderedDict.move_to_end during iteration is problematic.
Eric Snow added the comment: Here's a patch that tracks changes to the C OrderedDict linked list, similar to how it's done in deque. I've left the pure Python OrderedDict alone. @Raymond, that state counter works great. :) -- keywords: +patch stage: test needed - patch review Added file: http://bugs.python.org/file39610/issue24369-iteration-mutation.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue24369 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24369] Using OrderedDict.move_to_end during iteration is problematic.
New submission from Eric Snow: While the dict/OrderedDict iterators already check for additions and deletions, using the OrderedDict.move_to_end during iteration can lead to surprising results. The following results in an infinite loop: od = OrderedDict.fromkeys('abc') last = None for k in od: if last is not None: od.move_to_end(last) last = k Ideally we could disallow changing order during iteration, just like we disallow deletion. Since we've gone 3 minor versions already, would it be too late to break backward compatibility on this point? -- components: Library (Lib) messages: 244718 nosy: eric.snow, rhettinger priority: high severity: normal stage: test needed status: open title: Using OrderedDict.move_to_end during iteration is problematic. type: behavior versions: Python 3.4, Python 3.5, Python 3.6 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue24369 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24369] Using OrderedDict.move_to_end during iteration is problematic.
Raymond Hettinger added the comment: The C version should defend itself against any key-changes during iteration (see the state counter used in deque objects for an example of how to do this). The pure python version of OrderedDict has only minimal defenses against mutating during iteration, and it should be left as-is. FWIW, surprising is in the eye of the beholder. When it comes to mutating containers during iteration, all kinds of things can happen (that is why databases implement reader and writer locks). The following results in an infinite loop: s = list('abc') for k in s: s.append(k) -- assignee: - rhettinger priority: high - normal versions: -Python 3.4 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue24369 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24369] Using OrderedDict.move_to_end during iteration is problematic.
Eric Snow added the comment: Sounds good. Thanks, Raymond. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue24369 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com