diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -431,16 +431,18 @@ class Morsel(dict): if value == "": continue if key not in attrs: continue if key == "expires" and isinstance(value, int): append("%s=%s" % (self._reserved[key], _getdate(value))) elif key == "max-age" and isinstance(value, int): append("%s=%d" % (self._reserved[key], value)) + elif key == "comment" and isinstance(value, str): + append("%s=%s" % (self._reserved[key], _quote(value))) elif key in self._flags: if value: append(str(self._reserved[key])) else: append("%s=%s" % (self._reserved[key], value)) # Return the result return _semispacejoin(result) diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py --- a/Lib/test/test_http_cookies.py +++ b/Lib/test/test_http_cookies.py @@ -211,16 +211,26 @@ class CookieTests(unittest.TestCase): self.assertEqual(C1.output(), expected_output) def test_illegal_chars(self): rawdata = "a=b; c,d=e" C = cookies.SimpleCookie() with self.assertRaises(cookies.CookieError): C.load(rawdata) + def test_comment_quoting(self): + c = cookies.SimpleCookie() + c['foo'] = '\N{COPYRIGHT SIGN}' + self.assertEqual(str(c['foo']), 'Set-Cookie: foo="\\251"') + c['foo']['comment'] = 'comment \N{COPYRIGHT SIGN}' + self.assertEqual( + str(c['foo']), + 'Set-Cookie: foo="\\251"; Comment="comment \\251"' + ) + class MorselTests(unittest.TestCase): """Tests for the Morsel object.""" def test_defaults(self): morsel = cookies.Morsel() self.assertIsNone(morsel.key) self.assertIsNone(morsel.value)