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

Lib/Cookie.py
Lib/test/test_cookie.py
Misc/ACKS
Misc/NEWS

index d67443745721a7d78222983e615dae2c4f13fac1..0b15531196b833944e5f761d2fe653ab26b76cdb 100644 (file)
@@ -591,8 +591,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)
     # end __setitem__
 
     def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"):
index 41ba60f9d2a4545a6b4e5ada825b9ba0b3bd678e..36cd52e58fec2c2a4b822378d947f27de1e442cf 100644 (file)
@@ -3,6 +3,7 @@
 from test.test_support import run_unittest, run_doctest, check_warnings
 import unittest
 import Cookie
+import pickle
 
 
 class CookieTests(unittest.TestCase):
@@ -141,6 +142,18 @@ 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 = Cookie.SimpleCookie()
+        C.load(rawdata)
+        self.assertEqual(C.output(), expected_output)
+
+        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+            C1 = pickle.loads(pickle.dumps(C, protocol=proto))
+            self.assertEqual(C1.output(), expected_output)
+
 
 def test_main():
     run_unittest(CookieTests)
index b1d6d3f07287befb9d19b6790f18bc1fc6c8aa7b..d44240f0f9d90d23ee139943da15e818a30e7449 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -484,6 +484,7 @@ Chris Gonnerman
 Shelley Gooch
 David Goodger
 Hans de Graaff
+Tim Graham
 Nathaniel Gray
 Eddy De Greef
 Grant Griffin
index 1da89701308ca507a82d3d03cd8f5031622fb341..57c752ca6660f7c181bd387c5eb83390ad5a528a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -37,6 +37,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #22775: Fixed unpickling of Cookie.SimpleCookie with protocol 2.
+  Patch by Tim Graham.
+
 - Issue #22776: Brought excluded code into the scope of a try block in
   SysLogHandler.emit().