Skip to content

Commit bb54e28

Browse files
Convert to fstr, move check
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent dce754d commit bb54e28

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Lib/http/cookies.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def update(self, values):
338338
if key not in self._reserved:
339339
raise CookieError("Invalid attribute %r" % (key,))
340340
if _has_control_character(key, val):
341-
raise CookieError("Control characters are not allowed in cookies %r %r" % (key, val))
341+
raise CookieError(f"Control characters are not allowed in cookies {key!r} {val!r}")
342342
data[key] = val
343343
dict.update(self, data)
344344

@@ -385,13 +385,16 @@ def __repr__(self):
385385

386386
def js_output(self, attrs=None):
387387
# Print javascript
388+
output_string = self.OutputString(attrs)
389+
if _has_control_character(output_string):
390+
raise CookieError("Control characters are not allowed in cookies")
388391
return """
389392
<script type="text/javascript">
390393
<!-- begin hiding
391394
document.cookie = \"%s\";
392395
// end hiding -->
393396
</script>
394-
""" % (self.OutputString(attrs).replace('"', r'\"'))
397+
""" % (output_string.replace('"', r'\"'))
395398

396399
def OutputString(self, attrs=None):
397400
# Build up our result
@@ -530,8 +533,6 @@ def js_output(self, attrs=None):
530533
result = []
531534
items = sorted(self.items())
532535
for key, value in items:
533-
if _has_control_character(value.OutputString(attrs)):
534-
raise CookieError("Control characters are not allowed in cookies")
535536
result.append(value.js_output(attrs))
536537
return _nulljoin(result)
537538

0 commit comments

Comments
 (0)