Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6050 -- Handled edge-case of duplicate keys being passed when

initialising SortedDict. Patch from Collin Grady and SmileyChris.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6751 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit caa0523cb8afc19fbbe193b8488eed810dc35829 1 parent 8f97d96
Malcolm Tredinnick authored November 29, 2007
5  django/utils/datastructures.py
@@ -60,7 +60,10 @@ def __init__(self, data=None):
60 60
         if isinstance(data, dict):
61 61
             self.keyOrder = data.keys()
62 62
         else:
63  
-            self.keyOrder = [key for key, value in data]
  63
+            self.keyOrder = []
  64
+            for key, value in data:
  65
+                if key not in self.keyOrder:
  66
+                    self.keyOrder.append(key)
64 67
 
65 68
     def __deepcopy__(self, memo):
66 69
         from copy import deepcopy
51  tests/regressiontests/utils/datastructures.py
... ...
@@ -0,0 +1,51 @@
  1
+"""
  2
+>>> from django.utils.datastructures import SortedDict
  3
+
  4
+>>> d = SortedDict()
  5
+>>> d[7] = 'seven'
  6
+>>> d[1] = 'one'
  7
+>>> d[9] = 'nine'
  8
+>>> d.keys()
  9
+[7, 1, 9]
  10
+>>> d.values()
  11
+['seven', 'one', 'nine']
  12
+>>> d.items()
  13
+[(7, 'seven'), (1, 'one'), (9, 'nine')]
  14
+
  15
+# Overwriting an item keeps it's place.
  16
+>>> d[1] = 'ONE'
  17
+>>> d.values()
  18
+['seven', 'ONE', 'nine']
  19
+
  20
+# New items go to the end.
  21
+>>> d[0] = 'nil'
  22
+>>> d.keys()
  23
+[7, 1, 9, 0]
  24
+
  25
+# Deleting an item, then inserting the same key again will place it at the end.
  26
+>>> del d[7]
  27
+>>> d.keys()
  28
+[1, 9, 0]
  29
+>>> d[7] = 'lucky number 7'
  30
+>>> d.keys()
  31
+[1, 9, 0, 7]
  32
+
  33
+# Changing the keys won't do anything, it's only a copy of the keys dict.
  34
+>>> k = d.keys()
  35
+>>> k.remove(9)
  36
+>>> d.keys()
  37
+[1, 9, 0, 7]
  38
+
  39
+# Initialising a SortedDict with two keys will just take the first one. A real
  40
+# dict will actually take the second value so we will too, but we'll keep the
  41
+# ordering from the first key found.
  42
+>>> tuples = ((2, 'two'), (1, 'one'), (2, 'second-two'))
  43
+>>> d = SortedDict(tuples)
  44
+>>> d.keys()
  45
+[2, 1]
  46
+>>> real_dict = dict(tuples)
  47
+>>> real_dict.values()
  48
+['one', 'second-two']
  49
+>>> d.values()
  50
+['second-two', 'one']
  51
+"""
13  tests/regressiontests/utils/tests.py
@@ -6,7 +6,14 @@
6 6
 
7 7
 from django.utils import html, checksums
8 8
 
9  
-from timesince import timesince_tests
  9
+import timesince
  10
+import datastructures
  11
+
  12
+# Extra tests
  13
+__test__ = {
  14
+    'timesince': timesince,
  15
+    'datastructures': datastructures,
  16
+}
10 17
 
11 18
 class TestUtilsHtml(TestCase):
12 19
 
@@ -142,10 +149,6 @@ def test_luhn(self):
142 149
         for value, output in items:
143 150
             self.check_output(f, value, output)
144 151
 
145  
-__test__ = {
146  
-    'timesince_tests': timesince_tests,
147  
-}
148  
-
149 152
 if __name__ == "__main__":
150 153
     import doctest
151 154
     doctest.testmod()
2  tests/regressiontests/utils/timesince.py
... ...
@@ -1,4 +1,4 @@
1  
-timesince_tests = """
  1
+"""
2 2
 >>> from datetime import datetime, timedelta
3 3
 >>> from django.utils.timesince import timesince
4 4
 

0 notes on commit caa0523

Please sign in to comment.
Something went wrong with that request. Please try again.