summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-11-02 20:19:56 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-11-02 20:19:56 (GMT)
commit2cb0e73a89589ce56ba17da39a06f8017cfc92e4 (patch)
tree8180efd154004c2ba0fa67a923e5f4f9a381494c
parent030dbb91a92096c0a7611f2e0c711035622b5669 (diff)
parent8cf7c1cff0f1176387118826fffdf1c517405f3a (diff)
downloadcpython-2cb0e73a89589ce56ba17da39a06f8017cfc92e4.zip
cpython-2cb0e73a89589ce56ba17da39a06f8017cfc92e4.tar.gz
cpython-2cb0e73a89589ce56ba17da39a06f8017cfc92e4.tar.bz2
Issue #22775: Fixed unpickling of http.cookies.SimpleCookie with protocol 2
and above. Patch by Tim Graham.
-rw-r--r--Lib/http/cookies.py8
-rw-r--r--Lib/test/pickletester.py2
-rw-r--r--Lib/test/test_http_cookies.py15
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS3
5 files changed, 25 insertions, 4 deletions
diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py
index 556d101..a6de6d5 100644
--- a/Lib/http/cookies.py
+++ b/Lib/http/cookies.py
@@ -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."""
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index a2bea60..5963175 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -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)
diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py
index 76e5e9c..2b0281e 100644
--- 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 @@ 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."""
diff --git a/Misc/ACKS b/Misc/ACKS
index 23e97d4..a63bac7 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -496,6 +496,7 @@ Chris Gonnerman
Shelley Gooch
David Goodger
Hans de Graaff
+Tim Graham
Kim Gräsman
Nathaniel Gray
Eddy De Greef
diff --git a/Misc/NEWS b/Misc/NEWS
index 0c85390..8d9564d 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -183,6 +183,9 @@ Core and Builtins
Library
-------
+- Issue #22775: Fixed unpickling of http.cookies.SimpleCookie with protocol 2
+ and above. Patch by Tim Graham.
+
- Issue #22776: Brought excluded code into the scope of a try block in
SysLogHandler.emit().