Skip to content

Commit f67449d

Browse files
gh-148849: Deprecate http.cookies.BaseCookie.js_output()
1 parent 95559d2 commit f67449d

5 files changed

Lines changed: 31 additions & 8 deletions

File tree

Doc/library/http.cookies.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ Cookie Objects
109109

110110
The meaning for *attrs* is the same as in :meth:`output`.
111111

112+
.. deprecated:: 3.15
113+
:meth:`!js_output` is deprecated and will be removed in Python 3.17.
114+
Use :meth:`output` instead.
115+
112116

113117
.. method:: BaseCookie.load(rawdata)
114118

Doc/whatsnew/3.15.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,12 @@ New deprecations
17411741
(Contributed by Bénédikt Tran in :gh:`134978`.)
17421742

17431743

1744+
* :meth:`http.cookies.BaseCookie.js_output` is deprecated and will be
1745+
removed in Python 3.17. Use
1746+
:meth:`~http.cookies.BaseCookie.output` instead.
1747+
(Contributed by kishorhange111 in :gh:`148849`.)
1748+
1749+
17441750
* :mod:`re`:
17451751

17461752
* :func:`re.match` and :meth:`re.Pattern.match` are now

Lib/http/cookies.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
import re
133133
import string
134134
import types
135+
import warnings
135136

136137
__all__ = ["CookieError", "BaseCookie", "SimpleCookie"]
137138

@@ -539,6 +540,10 @@ def __repr__(self):
539540
l.append('%s=%s' % (key, repr(value.value)))
540541
return '<%s: %s>' % (self.__class__.__name__, _spacejoin(l))
541542

543+
@warnings.deprecated(
544+
"http.cookies.BaseCookie.js_output() is deprecated and will "
545+
"be removed in Python 3.17; use output() instead"
546+
)
542547
def js_output(self, attrs=None):
543548
"""Return a string suitable for JavaScript."""
544549
result = []

Lib/test/test_http_cookies.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,17 @@ def test_load(self):
176176
self.assertEqual(C.output(['path']),
177177
'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
178178
cookie_encoded = base64.b64encode(b'Customer="WILE_E_COYOTE"; Path=/acme; Version=1').decode('ascii')
179-
self.assertEqual(C.js_output(), fr"""
179+
with self.assertWarnsRegex(DeprecationWarning, "BaseCookie.js_output"):
180+
self.assertEqual(C.js_output(), fr"""
180181
<script type="text/javascript">
181182
<!-- begin hiding
182183
document.cookie = atob("{cookie_encoded}");
183184
// end hiding -->
184185
</script>
185186
""")
186187
cookie_encoded = base64.b64encode(b'Customer="WILE_E_COYOTE"; Path=/acme').decode('ascii')
187-
self.assertEqual(C.js_output(['path']), fr"""
188+
with self.assertWarnsRegex(DeprecationWarning, "BaseCookie.js_output"):
189+
self.assertEqual(C.js_output(['path']), fr"""
188190
<script type="text/javascript">
189191
<!-- begin hiding
190192
document.cookie = atob("{cookie_encoded}");
@@ -293,15 +295,17 @@ def test_quoted_meta(self):
293295
self.assertEqual(C.output(['path']),
294296
'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
295297
expected_encoded_cookie = base64.b64encode(b'Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1').decode('ascii')
296-
self.assertEqual(C.js_output(), fr"""
298+
with self.assertWarnsRegex(DeprecationWarning, "BaseCookie.js_output"):
299+
self.assertEqual(C.js_output(), fr"""
297300
<script type="text/javascript">
298301
<!-- begin hiding
299302
document.cookie = atob("{expected_encoded_cookie}");
300303
// end hiding -->
301304
</script>
302305
""")
303306
expected_encoded_cookie = base64.b64encode(b'Customer=\"WILE_E_COYOTE\"; Path=/acme').decode('ascii')
304-
self.assertEqual(C.js_output(['path']), fr"""
307+
with self.assertWarnsRegex(DeprecationWarning, "BaseCookie.js_output"):
308+
self.assertEqual(C.js_output(['path']), fr"""
305309
<script type="text/javascript">
306310
<!-- begin hiding
307311
document.cookie = atob("{expected_encoded_cookie}");
@@ -672,16 +676,18 @@ def test_control_characters_output(self):
672676
morsel._key = c0 # Override private variable.
673677
cookie = cookies.SimpleCookie()
674678
cookie["cookie"] = morsel
675-
with self.assertRaises(cookies.CookieError):
676-
cookie.js_output()
679+
with self.assertWarnsRegex(DeprecationWarning, "BaseCookie.js_output"):
680+
with self.assertRaises(cookies.CookieError):
681+
cookie.js_output()
677682

678683
morsel = cookies.Morsel()
679684
morsel.set("key", "value", "coded-value")
680685
morsel._coded_value = c0 # Override private variable.
681686
cookie = cookies.SimpleCookie()
682687
cookie["cookie"] = morsel
683-
with self.assertRaises(cookies.CookieError):
684-
cookie.js_output()
688+
with self.assertWarnsRegex(DeprecationWarning, "BaseCookie.js_output"):
689+
with self.assertRaises(cookies.CookieError):
690+
cookie.js_output()
685691

686692

687693
def load_tests(loader, tests, pattern):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecate :meth:`http.cookies.BaseCookie.js_output`, which will be
2+
removed in Python 3.17. Use :meth:`http.cookies.BaseCookie.output` instead.

0 commit comments

Comments
 (0)