Ticket #7598: keyOrder_fix.diff
File keyOrder_fix.diff, 4.6 KB (added by , 9 years ago) 


django/utils/datastructures.py
59 59 data = {} 60 60 super(SortedDict, self).__init__(data) 61 61 if isinstance(data, dict): 62 self.key Order = data.keys()62 self.key_order = data.keys() 63 63 else: 64 self.key Order = []64 self.key_order = [] 65 65 for key, value in data: 66 if key not in self.key Order:67 self.key Order.append(key)66 if key not in self.key_order: 67 self.key_order.append(key) 68 68 69 69 def __deepcopy__(self, memo): 70 70 from copy import deepcopy … … 73 73 74 74 def __setitem__(self, key, value): 75 75 super(SortedDict, self).__setitem__(key, value) 76 if key not in self.key Order:77 self.key Order.append(key)76 if key not in self.key_order: 77 self.key_order.append(key) 78 78 79 79 def __delitem__(self, key): 80 80 super(SortedDict, self).__delitem__(key) 81 self.key Order.remove(key)81 self.key_order.remove(key) 82 82 83 83 def __iter__(self): 84 for k in self.key Order:84 for k in self.key_order: 85 85 yield k 86 86 87 87 def pop(self, k, *args): 88 88 result = super(SortedDict, self).pop(k, *args) 89 89 try: 90 self.key Order.remove(k)90 self.key_order.remove(k) 91 91 except ValueError: 92 92 # Key wasn't in the dictionary in the first place. No problem. 93 93 pass … … 95 95 96 96 def popitem(self): 97 97 result = super(SortedDict, self).popitem() 98 self.key Order.remove(result[0])98 self.key_order.remove(result[0]) 99 99 return result 100 100 101 101 def items(self): 102 return zip(self.key Order, self.values())102 return zip(self.key_order, self.values()) 103 103 104 104 def iteritems(self): 105 for key in self.key Order:105 for key in self.key_order: 106 106 yield key, super(SortedDict, self).__getitem__(key) 107 107 108 108 def keys(self): 109 return self.key Order[:]109 return self.key_order[:] 110 110 111 111 def iterkeys(self): 112 return iter(self.key Order)112 return iter(self.key_order) 113 113 114 114 def values(self): 115 return [super(SortedDict, self).__getitem__(k) for k in self.key Order]115 return [super(SortedDict, self).__getitem__(k) for k in self.key_order] 116 116 117 117 def itervalues(self): 118 for key in self.key Order:118 for key in self.key_order: 119 119 yield super(SortedDict, self).__getitem__(key) 120 120 121 121 def update(self, dict_): … … 123 123 self.__setitem__(k, v) 124 124 125 125 def setdefault(self, key, default): 126 if key not in self.key Order:127 self.key Order.append(key)126 if key not in self.key_order: 127 self.key_order.append(key) 128 128 return super(SortedDict, self).setdefault(key, default) 129 129 130 130 def value_for_index(self, index): 131 131 """Returns the value of the item at the given zerobased index.""" 132 return self[self.key Order[index]]132 return self[self.key_order[index]] 133 133 134 134 def insert(self, index, key, value): 135 135 """Inserts the key, value pair before the item with the given index.""" 136 if key in self.key Order:137 n = self.key Order.index(key)138 del self.key Order[n]136 if key in self.key_order: 137 n = self.key_order.index(key) 138 del self.key_order[n] 139 139 if n < index: 140 140 index = 1 141 self.key Order.insert(index, key)141 self.key_order.insert(index, key) 142 142 super(SortedDict, self).__setitem__(key, value) 143 143 144 144 def copy(self): 145 145 """Returns a copy of this object.""" 146 146 # This way of initializing the copy means it works for subclasses, too. 147 147 obj = self.__class__(self) 148 obj.key Order = self.keyOrder[:]148 obj.key_order = self.key_order[:] 149 149 return obj 150 150 151 151 def __repr__(self): … … 157 157 158 158 def clear(self): 159 159 super(SortedDict, self).clear() 160 self.key Order = []160 self.key_order = [] 161 161 162 162 class MultiValueDictKeyError(KeyError): 163 163 pass 
tests/regressiontests/datastructures/tests.py
104 104 >>> d.clear() 105 105 >>> d 106 106 {} 107 >>> d.key Order107 >>> d.key_order 108 108 [] 109 109 110 110 ### DotExpandedDict ############################################################