[issue24726] OrderedDict has strange behaviour when dict.__setitem__ is used.

Serhiy Storchaka report at bugs.python.org
Wed Oct 21 15:18:33 EDT 2015


Serhiy Storchaka added the comment:

__repr__() allocates a list with the size len(od) and fills it iterating linked list. If the size of linked list is less then the size of the dict, the rest of the list is not initialized.

Even worse things happened when the size of linked list is greater then the size of the dict. Following example causes a crash:

from collections import OrderedDict
od = OrderedDict()
class K(str):
    def __hash__(self):
        return 1

od[K('a')] = 1
od[K('b')] = 2
print(len(od), len(list(od)))
K.__eq__ = lambda self, other: True
dict.__delitem__(od, K('a'))
print(len(od), len(list(od)))
print(repr(od))

Proposed patch fixes both issues.

----------
components: +Extension Modules -Library (Lib)
keywords: +patch
stage: test needed -> patch review
type: behavior -> crash
versions:  -Python 2.7, Python 3.4
Added file: http://bugs.python.org/file40834/odict_repr_after_dict_setitem_delitem.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24726>
_______________________________________


More information about the Python-bugs-list mailing list