summaryrefslogtreecommitdiff
path: root/django/http
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2014-10-31 14:26:27 -0400
committerTim Graham <timograham@gmail.com>2014-11-12 19:04:45 +0100
commit42b5e4feeacf7cfa57867bf9fd5a6046de8c1cd3 (patch)
tree41ab0e055e2c3a5870572b4b70ec7e89112a5784 /django/http
parent4e9a6c94e6bea805e089df2dee2d4ab2c902c827 (diff)
Fixed #23730 -- Moved support for SimpleCookie HIGHEST_PROTOCOL pickling to http.cookie.
This fix is necessary for Python 3.5 compatibility (refs #23763). Thanks Berker Peksag for review.
Diffstat (limited to 'django/http')
-rw-r--r--django/http/cookie.py20
-rw-r--r--django/http/response.py11
2 files changed, 19 insertions, 12 deletions
diff --git a/django/http/cookie.py b/django/http/cookie.py
index 7084c87766..3bd9065d3a 100644
--- a/django/http/cookie.py
+++ b/django/http/cookie.py
@@ -1,4 +1,5 @@
from __future__ import unicode_literals
+import sys
from django.utils.encoding import force_str
from django.utils import six
@@ -15,12 +16,29 @@ try:
except http_cookies.CookieError:
_cookie_allows_colon_in_names = False
-if _cookie_encodes_correctly and _cookie_allows_colon_in_names:
+# Cookie pickling bug is fixed in Python 2.7.9 and Python 3.4.3+
+# http://bugs.python.org/issue22775
+cookie_pickles_properly = (
+ (sys.version_info[:2] == (2, 7) and sys.version_info >= (2, 7, 9)) or
+ sys.version_info >= (3, 4, 3)
+)
+
+if _cookie_encodes_correctly and _cookie_allows_colon_in_names and cookie_pickles_properly:
SimpleCookie = http_cookies.SimpleCookie
else:
Morsel = http_cookies.Morsel
class SimpleCookie(http_cookies.SimpleCookie):
+ if not cookie_pickles_properly:
+ def __setitem__(self, key, value):
+ # Apply the fix from http://bugs.python.org/issue22775 where
+ # it's not fixed in Python itself
+ if isinstance(value, Morsel):
+ # allow assignment of constructed Morsels (e.g. for pickling)
+ dict.__setitem__(self, key, value)
+ else:
+ super(SimpleCookie, self).__setitem__(key, value)
+
if not _cookie_encodes_correctly:
def value_encode(self, val):
# Some browsers do not support quoted-string from RFC 2109,
diff --git a/django/http/response.py b/django/http/response.py
index 9e8280a307..3edf10d1e8 100644
--- a/django/http/response.py
+++ b/django/http/response.py
@@ -206,17 +206,6 @@ class HttpResponseBase(six.Iterator):
def __getitem__(self, header):
return self._headers[header.lower()][1]
- def __getstate__(self):
- # SimpleCookie is not pickleable with pickle.HIGHEST_PROTOCOL, so we
- # serialize to a string instead
- state = self.__dict__.copy()
- state['cookies'] = str(state['cookies'])
- return state
-
- def __setstate__(self, state):
- self.__dict__.update(state)
- self.cookies = SimpleCookie(self.cookies)
-
def has_header(self, header):
"""Case-insensitive check for a header."""
return header.lower() in self._headers