Fixed #12476 -- Forced the rollout of generators passed to SortedDict so that the data source can be read twice. Thanks to gsf for the report, and Alex for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12064 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-01-03 06:54:56 +00:00
parent f45ac2ff18
commit 0aa12da000
2 changed files with 12 additions and 2 deletions

View File

@ -1,3 +1,5 @@
from types import GeneratorType
from django.utils.copycompat import deepcopy from django.utils.copycompat import deepcopy
@ -65,6 +67,11 @@ class SortedDict(dict):
def __init__(self, data=None): def __init__(self, data=None):
if data is None: if data is None:
data = {} data = {}
elif isinstance(data, GeneratorType):
# Unfortunately we need to be able to read a generator twice. Once
# to get the data into self with our super().__init__ call and a
# second time to setup keyOrder correctly
data = list(data)
super(SortedDict, self).__init__(data) super(SortedDict, self).__init__(data)
if isinstance(data, dict): if isinstance(data, dict):
self.keyOrder = data.keys() self.keyOrder = data.keys()

View File

@ -95,6 +95,9 @@ True
>>> d.pop('one', 'missing') >>> d.pop('one', 'missing')
'missing' 'missing'
>>> SortedDict((i, i) for i in xrange(3))
{0: 0, 1: 1, 2: 2}
We don't know which item will be popped in popitem(), so we'll just check that We don't know which item will be popped in popitem(), so we'll just check that
the number of keys has decreased. the number of keys has decreased.
>>> l = len(d) >>> l = len(d)