| 161 | |
| 162 | def __new__(cls, query_string, mutable=False, encoding=None): |
| 163 | """Premise: when an instance of a QueryDict is being unpickled, |
| 164 | __init__() is never called by the unpickler (as documented at |
| 165 | http://docs.python.org/lib/pickle-inst.html). As a result, the |
| 166 | unpickled instance does not have the attributes _mutable and encoding. |
| 167 | That makes __setitem__ calls fail during the unpickle. |
| 168 | |
| 169 | Solution: The following override ensures that the instance being |
| 170 | unpickled will always have the attributes _mutable and encoding. |
| 171 | |
| 172 | This method together with __getnewargs__() ensure that |
| 173 | **mutable copies** of the QueryDict can be pickled and unpickled. |
| 174 | """ |
| 175 | obj = super(QueryDict, cls).__new__(cls, query_string, mutable=mutable, encoding=encoding) |
| 176 | obj._mutable = mutable |
| 177 | obj.encoding = encoding |
| 178 | return obj |
| 179 | |
| 180 | def __getnewargs__(self): |
| 181 | """This method is called when an instance of this class is being pickled. |
| 182 | The returned tuple is saved by the pickler and it is then passed back to |
| 183 | QueryDict.__new__() when the object is being unpickled. |
| 184 | """ |
| 185 | return ("", self._mutable, self.encoding) |
| 186 | |