]> granicus.if.org Git - python/commitdiff
Issue #22775: Fixed unpickling of http.cookies.SimpleCookie with protocol 2
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 2 Nov 2014 20:18:25 +0000 (22:18 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 2 Nov 2014 20:18:25 +0000 (22:18 +0200)
and above.  Patch by Tim Graham.

Lib/http/cookies.py
Lib/test/pickletester.py
Lib/test/test_http_cookies.py
Misc/ACKS
Misc/NEWS

index 556d101fb0e870e3312d453f6a94ce8dbac01ede..a6de6d588325caa7d6962df02045c87b62024b67 100644 (file)
@@ -486,8 +486,12 @@ class BaseCookie(dict):
 
     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."""
index a2bea600edb53cb62f49750d4f37d8722807f68c..5963175ddd1565602345b8e3c9228a948de3a6fb 100644 (file)
@@ -1284,7 +1284,7 @@ class AbstractPickleTests(unittest.TestCase):
         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)
index 76e5e9c4daddb828a468cf96f367f3e526cd84d1..2b0281edb4ca36586e3ee69ba9b8a40c45553976 100644 (file)
@@ -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 @@ class CookieTests(unittest.TestCase):
             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."""
index f0950ebb027185b68db353f50d04a96e0c81b8e1..30b6a0ec22d9ef1e9b903c7a3d798bf6d9631b07 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -492,6 +492,7 @@ Chris Gonnerman
 Shelley Gooch
 David Goodger
 Hans de Graaff
+Tim Graham
 Nathaniel Gray
 Eddy De Greef
 Grant Griffin
index 55f95b5e7519b53a6351c183aaff87dea0c3ff2b..ee1c53f4b92113519d11b01b72f80754ae5d4a36 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@ Core and Builtins
 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.