Fixed #7233 -- Ensured that QueryDict classes are always unpicklable. This

problem only arose on some systems, since it depends upon the order in which
the attributes are pickled. Makes reliable testing kind of tricky.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8460 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-08-21 13:55:21 +00:00
parent c8c159cbba
commit f2477b6450
2 changed files with 41 additions and 15 deletions

View File

@ -128,6 +128,11 @@ class QueryDict(MultiValueDict):
Values retrieved from this class are converted from the given encoding Values retrieved from this class are converted from the given encoding
(DEFAULT_CHARSET by default) to unicode. (DEFAULT_CHARSET by default) to unicode.
""" """
# These are both reset in __init__, but is specified here at the class
# level so that unpickling will have valid values
_mutable = True
_encoding = None
def __init__(self, query_string, mutable=False, encoding=None): def __init__(self, query_string, mutable=False, encoding=None):
MultiValueDict.__init__(self) MultiValueDict.__init__(self)
if not encoding: if not encoding:
@ -136,12 +141,24 @@ class QueryDict(MultiValueDict):
from django.conf import settings from django.conf import settings
encoding = settings.DEFAULT_CHARSET encoding = settings.DEFAULT_CHARSET
self.encoding = encoding self.encoding = encoding
self._mutable = True
for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True
self.appendlist(force_unicode(key, encoding, errors='replace'), self.appendlist(force_unicode(key, encoding, errors='replace'),
force_unicode(value, encoding, errors='replace')) force_unicode(value, encoding, errors='replace'))
self._mutable = mutable self._mutable = mutable
def _get_encoding(self):
if self._encoding is None:
# *Important*: do not import settings at the module level because
# of the note in core.handlers.modpython.
from django.conf import settings
self._encoding = settings.DEFAULT_CHARSET
return self._encoding
def _set_encoding(self, value):
self._encoding = value
encoding = property(_get_encoding, _set_encoding)
def _assert_mutable(self): def _assert_mutable(self):
if not self._mutable: if not self._mutable:
raise AttributeError("This QueryDict instance is immutable") raise AttributeError("This QueryDict instance is immutable")

View File

@ -392,6 +392,15 @@ u'\ufffd'
[u'bar', u'\ufffd'] [u'bar', u'\ufffd']
########################
# Pickling a QueryDict #
########################
>>> import pickle
>>> q = QueryDict('a=b&c=d')
>>> q1 = pickle.loads(pickle.dumps(q))
>>> q == q1
True
###################################### ######################################
# HttpResponse with Unicode headers # # HttpResponse with Unicode headers #
###################################### ######################################