Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit 0aa12da0003fda573d30b7b0d05c08192d80c264 1 parent f45ac2f
@freakboy3742 freakboy3742 authored
View
7 django/utils/datastructures.py
@@ -1,3 +1,5 @@
+from types import GeneratorType
+
from django.utils.copycompat import deepcopy
@@ -65,6 +67,11 @@ def __new__(cls, *args, **kwargs):
def __init__(self, data=None):
if data is None:
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)
if isinstance(data, dict):
self.keyOrder = data.keys()
View
7 tests/regressiontests/datastructures/tests.py
@@ -60,9 +60,9 @@
>>> d.setlist('lastname', ['Holovaty', 'Willison'])
>>> d.getlist('lastname')
['Holovaty', 'Willison']
->>> d.values()
+>>> d.values()
['Developer', 'Simon', 'Willison']
->>> list(d.itervalues())
+>>> list(d.itervalues())
['Developer', 'Simon', 'Willison']
### SortedDict #################################################################
@@ -95,6 +95,9 @@
>>> d.pop('one', '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
the number of keys has decreased.
>>> l = len(d)
Please sign in to comment.
Something went wrong with that request. Please try again.