]> granicus.if.org Git - python/commitdiff
Restored backward compatibility of pickling http.cookies.Morsel. It was
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 18 Mar 2015 16:03:40 +0000 (18:03 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 18 Mar 2015 16:03:40 +0000 (18:03 +0200)
broken after converting instance attributes to properies in issue #2211.

Lib/http/cookies.py
Lib/test/test_http_cookies.py

index f4e903501c79e728f7b76d4aae17bdc38e1a7717..98489fb3c8af35e7a5a100e48a6872ba6bea82ed 100644 (file)
@@ -377,6 +377,18 @@ class Morsel(dict):
         self._value = val
         self._coded_value = coded_val
 
+    def __getstate__(self):
+        return {
+            'key': self._key,
+            'value': self._value,
+            'coded_value': self._coded_value,
+        }
+
+    def __setstate__(self, state):
+        self._key = state['key']
+        self._value = state['value']
+        self._coded_value = state['coded_value']
+
     def output(self, attrs=None, header="Set-Cookie:"):
         return "%s %s" % (header, self.OutputString(attrs))
 
index 7665b1572229ac77a301aa7f0e60148df736cb9b..5f1e74bbc6cb3df448b1664d67ca045bd3fa3bb9 100644 (file)
@@ -1,5 +1,6 @@
 # Simple test suite for http/cookies.py
 
+import copy
 from test.support import run_unittest, run_doctest, check_warnings
 import unittest
 from http import cookies
@@ -325,6 +326,11 @@ class MorselTests(unittest.TestCase):
         self.assertIsNot(morsel_a, morsel_b)
         self.assertEqual(morsel_a, morsel_b)
 
+        morsel_b = copy.copy(morsel_a)
+        self.assertIsInstance(morsel_b, cookies.Morsel)
+        self.assertIsNot(morsel_a, morsel_b)
+        self.assertEqual(morsel_a, morsel_b)
+
     def test_setitem(self):
         morsel = cookies.Morsel()
         morsel['expires'] = 0
@@ -383,6 +389,20 @@ class MorselTests(unittest.TestCase):
         self.assertRaises(TypeError, morsel.update)
         self.assertRaises(TypeError, morsel.update, 0)
 
+    def test_pickle(self):
+        morsel_a = cookies.Morsel()
+        morsel_a.set('foo', 'bar', 'baz')
+        morsel_a.update({
+            'version': 2,
+            'comment': 'foo',
+        })
+        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+            with self.subTest(proto=proto):
+                morsel_b = pickle.loads(pickle.dumps(morsel_a, proto))
+                self.assertIsInstance(morsel_b, cookies.Morsel)
+                self.assertEqual(morsel_b, morsel_a)
+                self.assertEqual(str(morsel_b), str(morsel_a))
+
     def test_repr(self):
         morsel = cookies.Morsel()
         self.assertEqual(repr(morsel), '<Morsel: None=None>')