Skip to content

Commit 8a6f4b4

Browse files
bpo-991266: Fix quoting of Comment attribute of SimpleCookie (GH-6555)
(cherry picked from commit d5a2377) Co-authored-by: Berker Peksag <berker.peksag@gmail.com>
1 parent 26c289d commit 8a6f4b4

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

Lib/http/cookies.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ def OutputString(self, attrs=None):
436436
append("%s=%s" % (self._reserved[key], _getdate(value)))
437437
elif key == "max-age" and isinstance(value, int):
438438
append("%s=%d" % (self._reserved[key], value))
439+
elif key == "comment" and isinstance(value, str):
440+
append("%s=%s" % (self._reserved[key], _quote(value)))
439441
elif key in self._flags:
440442
if value:
441443
append(str(self._reserved[key]))

Lib/test/test_http_cookies.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ def test_illegal_chars(self):
216216
with self.assertRaises(cookies.CookieError):
217217
C.load(rawdata)
218218

219+
def test_comment_quoting(self):
220+
c = cookies.SimpleCookie()
221+
c['foo'] = '\N{COPYRIGHT SIGN}'
222+
self.assertEqual(str(c['foo']), 'Set-Cookie: foo="\\251"')
223+
c['foo']['comment'] = 'comment \N{COPYRIGHT SIGN}'
224+
self.assertEqual(
225+
str(c['foo']),
226+
'Set-Cookie: foo="\\251"; Comment="comment \\251"'
227+
)
228+
219229

220230
class MorselTests(unittest.TestCase):
221231
"""Tests for the Morsel object."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix quoting of the ``Comment`` attribute of :class:`http.cookies.SimpleCookie`.

0 commit comments

Comments
 (0)