[Python-checkins] cpython (3.4): Issue #22775: Fixed unpickling of http.cookies.SimpleCookie with protocol 2

serhiy.storchaka python-checkins at python.org
Sun Nov 2 21:22:44 CET 2014


https://hg.python.org/cpython/rev/7be6ef737aaf
changeset:   93366:7be6ef737aaf
branch:      3.4
parent:      93364:c51d85cf57f2
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Nov 02 22:18:25 2014 +0200
summary:
  Issue #22775: Fixed unpickling of http.cookies.SimpleCookie with protocol 2
and above.  Patch by Tim Graham.

files:
  Lib/http/cookies.py           |   8 ++++++--
  Lib/test/pickletester.py      |   2 +-
  Lib/test/test_http_cookies.py |  15 ++++++++++++++-
  Misc/ACKS                     |   1 +
  Misc/NEWS                     |   3 +++
  5 files changed, 25 insertions(+), 4 deletions(-)


diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py
--- a/Lib/http/cookies.py
+++ b/Lib/http/cookies.py
@@ -486,8 +486,12 @@
 
     def __setitem__(self, key, value):
         """Dictionary style assignment."""
-        rval, cval = self.value_encode(value)
-        self.__set(key, rval, cval)
+        if isinstance(value, Morsel):
+            # allow assignment of constructed Morsels (e.g. for pickling)
+            dict.__setitem__(self, key, value)
+        else:
+            rval, cval = self.value_encode(value)
+            self.__set(key, rval, cval)
 
     def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"):
         """Return a string suitable for HTTP."""
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -1284,7 +1284,7 @@
         loaded = self.loads(DATA5)
         self.assertEqual(type(loaded), SimpleCookie)
         self.assertEqual(list(loaded.keys()), ["key"])
-        self.assertEqual(loaded["key"].value, "Set-Cookie: key=value")
+        self.assertEqual(loaded["key"].value, "value")
 
         for (exc, data) in DATA7.items():
             loaded = self.loads(data)
diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py
--- a/Lib/test/test_http_cookies.py
+++ b/Lib/test/test_http_cookies.py
@@ -3,7 +3,7 @@
 from test.support import run_unittest, run_doctest, check_warnings
 import unittest
 from http import cookies
-
+import pickle
 import warnings
 
 class CookieTests(unittest.TestCase):
@@ -187,6 +187,19 @@
             self.assertEqual(dict(C), {})
             self.assertEqual(C.output(), '')
 
+    def test_pickle(self):
+        rawdata = 'Customer="WILE_E_COYOTE"; Path=/acme; Version=1'
+        expected_output = 'Set-Cookie: %s' % rawdata
+
+        C = cookies.SimpleCookie()
+        C.load(rawdata)
+        self.assertEqual(C.output(), expected_output)
+
+        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+            with self.subTest(proto=proto):
+                C1 = pickle.loads(pickle.dumps(C, protocol=proto))
+                self.assertEqual(C1.output(), expected_output)
+
 
 class MorselTests(unittest.TestCase):
     """Tests for the Morsel object."""
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -492,6 +492,7 @@
 Shelley Gooch
 David Goodger
 Hans de Graaff
+Tim Graham
 Nathaniel Gray
 Eddy De Greef
 Grant Griffin
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@
 Library
 -------
 
+- Issue #22775: Fixed unpickling of http.cookies.SimpleCookie with protocol 2
+  and above.  Patch by Tim Graham.
+
 - Issue #22366: urllib.request.urlopen will accept a context object
   (SSLContext) as an argument which will then used be for HTTPS connection.
   Patch by Alex Gaynor.

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list