Ticket #7233: querydict_pickle_fix.diff

File querydict_pickle_fix.diff, 1.9 KB (added by rajesh.dhawan@…, 17 years ago)

Fix to make copies of QueryDict instances pickle/unpickle friendly

  • django/http/__init__.py

     
    158158                            force_unicode(value, encoding, errors='replace'))
    159159        self._mutable = mutable
    160160
     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
    161187    def _assert_mutable(self):
    162188        if not self._mutable:
    163189            raise AttributeError("This QueryDict instance is immutable")
     
    239265            output.extend([urlencode({k: smart_str(v, self.encoding)}) for v in list_])
    240266        return '&'.join(output)
    241267
     268
    242269def parse_cookie(cookie):
    243270    if cookie == '':
    244271        return {}
Back to Top