Skip to content

Commit d5a2377

Browse files
authored
bpo-991266: Fix quoting of Comment attribute of SimpleCookie (GH-6555)
1 parent b81ca28 commit d5a2377

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
@@ -220,6 +220,16 @@ def test_illegal_chars(self):
220220
with self.assertRaises(cookies.CookieError):
221221
C.load(rawdata)
222222

223+
def test_comment_quoting(self):
224+
c = cookies.SimpleCookie()
225+
c['foo'] = '\N{COPYRIGHT SIGN}'
226+
self.assertEqual(str(c['foo']), 'Set-Cookie: foo="\\251"')
227+
c['foo']['comment'] = 'comment \N{COPYRIGHT SIGN}'
228+
self.assertEqual(
229+
str(c['foo']),
230+
'Set-Cookie: foo="\\251"; Comment="comment \\251"'
231+
)
232+
223233

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