Ticket #12476: diff.diff

File diff.diff, 1.3 KB (added by Alex Gaynor, 15 years ago)

Initial patch (solves the issue). I'm not completely happy with it.

  • django/utils/datastructures.py

    diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
    index 06cf6c6..adcc354 100644
    a b  
     1from types import GeneratorType
     2
    13from django.utils.copycompat import deepcopy
    24
    35
    class SortedDict(dict):  
    6567    def __init__(self, data=None):
    6668        if data is None:
    6769            data = {}
     70        elif isinstance(data, GeneratorType):
     71            # Unfortunately we need to be able to read a generator twice.  Once
     72            # to get the data into self with our super().__init__ call and a
     73            # second time to setup keyOrder correctly
     74            data = list(data)
    6875        super(SortedDict, self).__init__(data)
    6976        if isinstance(data, dict):
    7077            self.keyOrder = data.keys()
  • tests/regressiontests/datastructures/tests.py

    diff --git a/tests/regressiontests/datastructures/tests.py b/tests/regressiontests/datastructures/tests.py
    index e658f33..148c7e4 100644
    a b True  
    9595>>> d.pop('one', 'missing')
    9696'missing'
    9797
     98>>> SortedDict((i, i) for i in xrange(3))
     99{0: 0, 1: 1, 2: 2}
     100
    98101We don't know which item will be popped in popitem(), so we'll just check that
    99102the number of keys has decreased.
    100103>>> l = len(d)
Back to Top