Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2cb0e73

Browse files
Issue #22775: Fixed unpickling of http.cookies.SimpleCookie with protocol 2
and above. Patch by Tim Graham.
2 parents 030dbb9 + 8cf7c1c commit 2cb0e73

5 files changed

Lines changed: 25 additions & 4 deletions

File tree

Lib/http/cookies.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,12 @@ def __set(self, key, real_value, coded_value):
486486

487487
def __setitem__(self, key, value):
488488
"""Dictionary style assignment."""
489-
rval, cval = self.value_encode(value)
490-
self.__set(key, rval, cval)
489+
if isinstance(value, Morsel):
490+
# allow assignment of constructed Morsels (e.g. for pickling)
491+
dict.__setitem__(self, key, value)
492+
else:
493+
rval, cval = self.value_encode(value)
494+
self.__set(key, rval, cval)
491495

492496
def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"):
493497
"""Return a string suitable for HTTP."""

Lib/test/pickletester.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ def test_unpickle_from_2x(self):
12841284
loaded = self.loads(DATA5)
12851285
self.assertEqual(type(loaded), SimpleCookie)
12861286
self.assertEqual(list(loaded.keys()), ["key"])
1287-
self.assertEqual(loaded["key"].value, "Set-Cookie: key=value")
1287+
self.assertEqual(loaded["key"].value, "value")
12881288

12891289
for (exc, data) in DATA7.items():
12901290
loaded = self.loads(data)

Lib/test/test_http_cookies.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from test.support import run_unittest, run_doctest, check_warnings
44
import unittest
55
from http import cookies
6-
6+
import pickle
77
import warnings
88

99
class CookieTests(unittest.TestCase):
@@ -187,6 +187,19 @@ def test_invalid_cookies(self):
187187
self.assertEqual(dict(C), {})
188188
self.assertEqual(C.output(), '')
189189

190+
def test_pickle(self):
191+
rawdata = 'Customer="WILE_E_COYOTE"; Path=/acme; Version=1'
192+
expected_output = 'Set-Cookie: %s' % rawdata
193+
194+
C = cookies.SimpleCookie()
195+
C.load(rawdata)
196+
self.assertEqual(C.output(), expected_output)
197+
198+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
199+
with self.subTest(proto=proto):
200+
C1 = pickle.loads(pickle.dumps(C, protocol=proto))
201+
self.assertEqual(C1.output(), expected_output)
202+
190203

191204
class MorselTests(unittest.TestCase):
192205
"""Tests for the Morsel object."""

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ Chris Gonnerman
496496
Shelley Gooch
497497
David Goodger
498498
Hans de Graaff
499+
Tim Graham
499500
Kim Gräsman
500501
Nathaniel Gray
501502
Eddy De Greef

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ Core and Builtins
183183
Library
184184
-------
185185

186+
- Issue #22775: Fixed unpickling of http.cookies.SimpleCookie with protocol 2
187+
and above. Patch by Tim Graham.
188+
186189
- Issue #22776: Brought excluded code into the scope of a try block in
187190
SysLogHandler.emit().
188191

0 commit comments

Comments
 (0)