changeset: 98756:33d53a41daeb parent: 98754:8e97d54e6d7d parent: 98755:88e6641c3dd3 user: Serhiy Storchaka date: Wed Oct 14 19:22:44 2015 +0300 files: Lib/test/test_collections.py Misc/NEWS description: Issue #25406: Fixed a bug in C implementation of OrderedDict.move_to_end() that caused segmentation fault or hang in iterating after moving several items to the start of ordered dict. diff -r 8e97d54e6d7d -r 33d53a41daeb Lib/test/test_collections.py --- a/Lib/test/test_collections.py Wed Oct 14 15:28:59 2015 +0200 +++ b/Lib/test/test_collections.py Wed Oct 14 19:22:44 2015 +0300 @@ -1995,6 +1995,20 @@ with self.assertRaises(KeyError): od.move_to_end('x', 0) + def test_move_to_end_issue25406(self): + OrderedDict = self.module.OrderedDict + od = OrderedDict.fromkeys('abc') + od.move_to_end('c', last=False) + self.assertEqual(list(od), list('cab')) + od.move_to_end('a', last=False) + self.assertEqual(list(od), list('acb')) + + od = OrderedDict.fromkeys('abc') + od.move_to_end('a') + self.assertEqual(list(od), list('bca')) + od.move_to_end('c') + self.assertEqual(list(od), list('bac')) + def test_sizeof(self): OrderedDict = self.module.OrderedDict # Wimpy test: Just verify the reported size is larger than a regular dict diff -r 8e97d54e6d7d -r 33d53a41daeb Misc/NEWS --- a/Misc/NEWS Wed Oct 14 15:28:59 2015 +0200 +++ b/Misc/NEWS Wed Oct 14 19:22:44 2015 +0300 @@ -63,6 +63,10 @@ Library ------- +- Issue #25406: Fixed a bug in C implementation of OrderedDict.move_to_end() + that caused segmentation fault or hang in iterating after moving several + items to the start of ordered dict. + - Issue #25382: pickletools.dis() now outputs implicit memo index for the MEMOIZE opcode. diff -r 8e97d54e6d7d -r 33d53a41daeb Objects/odictobject.c --- a/Objects/odictobject.c Wed Oct 14 15:28:59 2015 +0200 +++ b/Objects/odictobject.c Wed Oct 14 19:22:44 2015 +0300 @@ -618,37 +618,26 @@ static void _odict_add_head(PyODictObject *od, _ODictNode *node) { - if (_odict_FIRST(od) == NULL) { - _odictnode_PREV(node) = NULL; - _odictnode_NEXT(node) = NULL; - _odict_FIRST(od) = node; + _odictnode_PREV(node) = NULL; + _odictnode_NEXT(node) = _odict_FIRST(od); + if (_odict_FIRST(od) == NULL) _odict_LAST(od) = node; - } - else { - _odictnode_PREV(node) = NULL; - _odictnode_NEXT(node) = _odict_FIRST(od); - _odict_FIRST(od) = node; + else _odictnode_PREV(_odict_FIRST(od)) = node; - } + _odict_FIRST(od) = node; od->od_state++; } static void _odict_add_tail(PyODictObject *od, _ODictNode *node) { - if (_odict_LAST(od) == NULL) { - _odictnode_PREV(node) = NULL; - _odictnode_NEXT(node) = NULL; + _odictnode_PREV(node) = _odict_LAST(od); + _odictnode_NEXT(node) = NULL; + if (_odict_LAST(od) == NULL) _odict_FIRST(od) = node; - _odict_LAST(od) = node; - } - else { - _odictnode_PREV(node) = _odict_LAST(od); - _odictnode_NEXT(node) = NULL; + else _odictnode_NEXT(_odict_LAST(od)) = node; - _odict_LAST(od) = node; - } - + _odict_LAST(od) = node; od->od_state++; }