diff -r c95ce9b0a085 Lib/http/cookies.py --- a/Lib/http/cookies.py Fri Oct 31 13:13:09 2014 +0100 +++ b/Lib/http/cookies.py Fri Oct 31 12:51:54 2014 -0400 @@ -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 -r c95ce9b0a085 Lib/test/pickletester.py --- a/Lib/test/pickletester.py Fri Oct 31 13:13:09 2014 +0100 +++ b/Lib/test/pickletester.py Fri Oct 31 12:51:54 2014 -0400 @@ -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 -r c95ce9b0a085 Lib/test/test_http_cookies.py --- a/Lib/test/test_http_cookies.py Fri Oct 31 13:13:09 2014 +0100 +++ b/Lib/test/test_http_cookies.py Fri Oct 31 12:51:54 2014 -0400 @@ -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,17 @@ self.assertEqual(dict(C), {}) self.assertEqual(C.output(), '') + def test_pickle_cookie_highest_protocol(self): + rawdata = 'Customer="WILE_E_COYOTE"' + expected_output = 'Set-Cookie: %s' % rawdata + + C = cookies.SimpleCookie() + C.load(rawdata) + self.assertEqual(C.output(), expected_output) + + C1 = pickle.loads(pickle.dumps(C, protocol=pickle.HIGHEST_PROTOCOL)) + self.assertEqual(C1.output(), expected_output) + class MorselTests(unittest.TestCase): """Tests for the Morsel object."""