Skip to content

Commit a91d066

Browse files
gh-148849: Add Morsel._js_output() to avoid multiple warnings
1 parent c957098 commit a91d066

2 files changed

Lines changed: 26 additions & 14 deletions

File tree

Lib/http/cookies.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,9 @@ def output(self, attrs=None, header="Set-Cookie:"):
391391
def __repr__(self):
392392
return '<%s: %s>' % (self.__class__.__name__, self.OutputString())
393393

394-
def js_output(self, attrs=None):
395-
warnings._deprecated(
396-
"http.cookies.Morsel.js_output",
397-
message=warnings._DEPRECATED_MSG + "; use output() instead",
398-
remove=(3, 19),
399-
)
394+
395+
def _js_output(self, attrs=None):
396+
"""Internal implementation without deprecation warning."""
400397
import base64
401398
# Print javascript
402399
output_string = self.OutputString(attrs)
@@ -413,6 +410,14 @@ def js_output(self, attrs=None):
413410
</script>
414411
""" % (output_encoded,)
415412

413+
def js_output(self, attrs=None):
414+
warnings._deprecated(
415+
"http.cookies.Morsel.js_output",
416+
message=warnings._DEPRECATED_MSG + "; use output() instead",
417+
remove=(3, 19),
418+
)
419+
return self._js_output(attrs)
420+
416421
def OutputString(self, attrs=None):
417422
# Build up our result
418423
#
@@ -547,10 +552,15 @@ def __repr__(self):
547552

548553
def js_output(self, attrs=None):
549554
"""Return a string suitable for JavaScript."""
555+
warnings._deprecated(
556+
"http.cookies.BaseCookie.js_output",
557+
message=warnings._DEPRECATED_MSG + "; use output() instead",
558+
remove=(3, 19),
559+
)
550560
result = []
551561
items = sorted(self.items())
552562
for key, value in items:
553-
result.append(value.js_output(attrs))
563+
result.append(value._js_output(attrs))
554564
return _nulljoin(result)
555565

556566
def load(self, rawdata):

Lib/test/test_http_cookies.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ 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-
with self.assertWarnsRegex(DeprecationWarning, r"Morsel\.js_output"):
179+
with self.assertWarnsRegex(DeprecationWarning, r"BaseCookie\.js_output"):
180180
self.assertEqual(C.js_output(), fr"""
181181
<script type="text/javascript">
182182
<!-- begin hiding
@@ -185,7 +185,7 @@ def test_load(self):
185185
</script>
186186
""")
187187
cookie_encoded = base64.b64encode(b'Customer="WILE_E_COYOTE"; Path=/acme').decode('ascii')
188-
with self.assertWarnsRegex(DeprecationWarning, r"Morsel\.js_output"):
188+
with self.assertWarnsRegex(DeprecationWarning, r"BaseCookie\.js_output"):
189189
self.assertEqual(C.js_output(['path']), fr"""
190190
<script type="text/javascript">
191191
<!-- begin hiding
@@ -295,7 +295,7 @@ def test_quoted_meta(self):
295295
self.assertEqual(C.output(['path']),
296296
'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
297297
expected_encoded_cookie = base64.b64encode(b'Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1').decode('ascii')
298-
with self.assertWarnsRegex(DeprecationWarning, r"Morsel\.js_output"):
298+
with self.assertWarnsRegex(DeprecationWarning, r"BaseCookie\.js_output"):
299299
self.assertEqual(C.js_output(), fr"""
300300
<script type="text/javascript">
301301
<!-- begin hiding
@@ -304,7 +304,7 @@ def test_quoted_meta(self):
304304
</script>
305305
""")
306306
expected_encoded_cookie = base64.b64encode(b'Customer=\"WILE_E_COYOTE\"; Path=/acme').decode('ascii')
307-
with self.assertWarnsRegex(DeprecationWarning, r"Morsel\.js_output"):
307+
with self.assertWarnsRegex(DeprecationWarning, r"BaseCookie\.js_output"):
308308
self.assertEqual(C.js_output(['path']), fr"""
309309
<script type="text/javascript">
310310
<!-- begin hiding
@@ -693,21 +693,23 @@ def test_control_characters_output(self):
693693
def test_morsel_js_output_deprecated(self):
694694
morsel = cookies.Morsel()
695695
morsel.set("key", "value", "value")
696-
with self.assertWarnsRegex(DeprecationWarning, r"Morsel\.js_output"):
696+
with self.assertWarnsRegex(DeprecationWarning, r"Morsel\.js_output") as cm:
697697
result = morsel.js_output()
698+
self.assertEqual(cm.filename, __file__)
698699
self.assertIn("document.cookie", result)
699700

701+
700702
def test_basecookie_js_output_warns_once(self):
701703
C = cookies.SimpleCookie()
702704
C["key"] = "value"
703705
with self.assertWarns(DeprecationWarning) as cm:
704706
C.js_output()
705-
# Should only be one warning even though BaseCookie iterates Morsels
706707
deprecation_warnings = [
707708
w for w in cm.warnings if issubclass(w.category, DeprecationWarning)
708709
]
709710
self.assertEqual(len(deprecation_warnings), 1)
710-
self.assertRegex(str(deprecation_warnings[0].message), r"Morsel\.js_output")
711+
self.assertRegex(str(deprecation_warnings[0].message), r"BaseCookie\.js_output")
712+
self.assertEqual(cm.filename, __file__)
711713

712714
def load_tests(loader, tests, pattern):
713715
tests.addTest(doctest.DocTestSuite(cookies))

0 commit comments

Comments
 (0)