Skip to content

Commit 9fc998d

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 f900503 commit 9fc998d

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
@@ -408,6 +408,8 @@ def OutputString(self, attrs=None):
408408
append("%s=%s" % (self._reserved[key], _getdate(value)))
409409
elif key == "max-age" and isinstance(value, int):
410410
append("%s=%d" % (self._reserved[key], value))
411+
elif key == "comment" and isinstance(value, str):
412+
append("%s=%s" % (self._reserved[key], _quote(value)))
411413
elif key in self._flags:
412414
if value:
413415
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
@@ -207,6 +207,16 @@ def test_illegal_chars(self):
207207
with self.assertRaises(cookies.CookieError):
208208
C.load(rawdata)
209209

210+
def test_comment_quoting(self):
211+
c = cookies.SimpleCookie()
212+
c['foo'] = '\N{COPYRIGHT SIGN}'
213+
self.assertEqual(str(c['foo']), 'Set-Cookie: foo="\\251"')
214+
c['foo']['comment'] = 'comment \N{COPYRIGHT SIGN}'
215+
self.assertEqual(
216+
str(c['foo']),
217+
'Set-Cookie: foo="\\251"; Comment="comment \\251"'
218+
)
219+
210220

211221
class MorselTests(unittest.TestCase):
212222
"""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)