Skip to content

Commit

Permalink
Fixed #12476 -- Forced the rollout of generators passed to SortedDict…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
freakboy3742 committed Jan 3, 2010
1 parent f45ac2f commit 0aa12da
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 7 additions & 0 deletions django/utils/datastructures.py
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,5 @@
from types import GeneratorType

from django.utils.copycompat import deepcopy from django.utils.copycompat import deepcopy




Expand Down Expand Up @@ -65,6 +67,11 @@ def __new__(cls, *args, **kwargs):
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()
Expand Down
7 changes: 5 additions & 2 deletions tests/regressiontests/datastructures/tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@
>>> d.setlist('lastname', ['Holovaty', 'Willison']) >>> d.setlist('lastname', ['Holovaty', 'Willison'])
>>> d.getlist('lastname') >>> d.getlist('lastname')
['Holovaty', 'Willison'] ['Holovaty', 'Willison']
>>> d.values() >>> d.values()
['Developer', 'Simon', 'Willison'] ['Developer', 'Simon', 'Willison']
>>> list(d.itervalues()) >>> list(d.itervalues())
['Developer', 'Simon', 'Willison'] ['Developer', 'Simon', 'Willison']
### SortedDict ################################################################# ### SortedDict #################################################################
Expand Down Expand Up @@ -95,6 +95,9 @@
>>> 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)
Expand Down

0 comments on commit 0aa12da

Please sign in to comment.