]> granicus.if.org Git - python/commitdiff
bpo-991266: Fix quoting of Comment attribute of SimpleCookie (GH-6555)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 23 Apr 2018 00:58:23 +0000 (17:58 -0700)
committerBerker Peksag <berker.peksag@gmail.com>
Mon, 23 Apr 2018 00:58:23 +0000 (03:58 +0300)
(cherry picked from commit d5a2377c3d70e4143bcbee4a765b3434e21f683a)

Co-authored-by: Berker Peksag <berker.peksag@gmail.com>
Lib/http/cookies.py
Lib/test/test_http_cookies.py
Misc/NEWS.d/next/Library/2018-04-21-00-24-08.bpo-991266.h93TP_.rst [new file with mode: 0644]

index 7e0259ee32e46303455d1b73121a1f1032838871..6e1034f131a30f11dd07b342f5f155356bd2d0aa 100644 (file)
@@ -408,6 +408,8 @@ class Morsel(dict):
                 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]))
index 2ff690243fc30ed429ab2c1dde7ef9fcfa71ed59..c1caad332d617aa971403c8b6f30867cef12206e 100644 (file)
@@ -207,6 +207,16 @@ class CookieTests(unittest.TestCase):
         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."""
diff --git a/Misc/NEWS.d/next/Library/2018-04-21-00-24-08.bpo-991266.h93TP_.rst b/Misc/NEWS.d/next/Library/2018-04-21-00-24-08.bpo-991266.h93TP_.rst
new file mode 100644 (file)
index 0000000..3af6c27
--- /dev/null
@@ -0,0 +1 @@
+Fix quoting of the ``Comment`` attribute of :class:`http.cookies.SimpleCookie`.