Author: mtredinnick
Date: 2008-08-21 08:55:21 -0500 (Thu, 21 Aug 2008)
New Revision: 8460
Modified:
django/trunk/django/http/__init__.py
django/trunk/tests/regressiontests/httpwrappers/tests.py
Log:
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.
Modified: django/trunk/django/http/__init__.py
===================================================================
--- django/trunk/django/http/__init__.py 2008-08-21 13:54:53 UTC (rev
8459)
+++ django/trunk/django/http/__init__.py 2008-08-21 13:55:21 UTC (rev
8460)
@@ -128,6 +128,11 @@
Values retrieved from this class are converted from the given encoding
(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):
MultiValueDict.__init__(self)
if not encoding:
@@ -136,12 +141,24 @@
from django.conf import settings
encoding = settings.DEFAULT_CHARSET
self.encoding = encoding
- self._mutable = True
for key, value in parse_qsl((query_string or ''), True): #
keep_blank_values=True
self.appendlist(force_unicode(key, encoding, errors='replace'),
force_unicode(value, encoding, errors='replace'))
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):
if not self._mutable:
raise AttributeError("This QueryDict instance is immutable")
Modified: django/trunk/tests/regressiontests/httpwrappers/tests.py
===================================================================
--- django/trunk/tests/regressiontests/httpwrappers/tests.py 2008-08-21
13:54:53 UTC (rev 8459)
+++ django/trunk/tests/regressiontests/httpwrappers/tests.py 2008-08-21
13:55:21 UTC (rev 8460)
@@ -392,17 +392,26 @@
[u'bar', u'\ufffd']
-######################################
-# HttpResponse with Unicode headers #
-######################################
-
->>> r = HttpResponse()
-
+########################
+# Pickling a QueryDict #
+########################
+>>> import pickle
+>>> q = QueryDict('a=b&c=d')
+>>> q1 = pickle.loads(pickle.dumps(q))
+>>> q == q1
+True
+
+######################################
+# HttpResponse with Unicode headers #
+######################################
+
+>>> r = HttpResponse()
+
If we insert a unicode value it will be converted to an ascii
string. This makes sure we comply with the HTTP specifications.
-
->>> r['value'] = u'test value'
->>> isinstance(r['value'], str)
+
+>>> r['value'] = u'test value'
+>>> isinstance(r['value'], str)
True
An error is raised When a unicode object with non-ascii is assigned.
@@ -411,10 +420,10 @@
Traceback (most recent call last):
...
UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
-
-The response also converts unicode keys to strings.
-
->>> r[u'test'] = 'testing key'
+
+The response also converts unicode keys to strings.
+
+>>> r[u'test'] = 'testing key'
>>> l = list(r.items())
>>> l.sort()
>>> l[1]
@@ -426,7 +435,7 @@
Traceback (most recent call last):
...
UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
-
+
"""
from django.http import QueryDict, HttpResponse
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---