Ticket #15812: sorteddict.patch

File sorteddict.patch, 1.5 KB (added by slinkp, 4 years ago)
  • django/utils/datastructures.py

     
    175175            yield self[key]
    176176
    177177    def update(self, dict_):
    178         for k, v in dict_.iteritems():
     178        if getattr(dict_, 'iteritems', None) is not None:
     179            dict_ = dict_.iteritems()
     180        for k, v in dict_:
     181            # This works for tuples, lists, and other iterators too.
    179182            self[k] = v
    180183
    181184    def setdefault(self, key, default):
  • tests/regressiontests/utils/datastructures.py

     
    114114        d = SortedDict(((1, "one"), (0, "zero"), (2, "two")))
    115115        self.assertEqual(repr(d), "{1: 'one', 0: 'zero', 2: 'two'}")
    116116
     117
     118    def test_tuple_update(self):
     119        d = SortedDict()
     120        d.update(((1, "one"), (0, "zero"), (2, "two")))
     121        self.assertEqual(repr(d), "{1: 'one', 0: 'zero', 2: 'two'}")
     122
     123    def test_generator_update(self):
     124        def some_generator():
     125            for i in range(3, 0, -1):
     126                yield (i, 'o' * i)
     127        d = SortedDict()
     128        d.update(some_generator())
     129        self.assertEqual(repr(d), "{3: 'ooo', 2: 'oo', 1: 'o'}")
     130
     131
    117132    def test_pickle(self):
    118133        self.assertEqual(
    119134            pickle.loads(pickle.dumps(self.d1, 2)),
Back to Top