Skip to content

Commit 9e474a9

Browse files
authored
gh-128982: Revert "#128982: Substitute regular expression in http.cookiejar.join_header_words for an efficient alternative (GH-128983)" and add tests (GH-130584)
* Revert "gh-128982: Substitute regular expression in `http.cookiejar.join_header_words` for an efficient alternative (GH-128983)" This reverts commit 56e1900. * Add tests
1 parent 56e1900 commit 9e474a9

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

Lib/http/cookiejar.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def join_header_words(lists):
448448
attr = []
449449
for k, v in pairs:
450450
if v is not None:
451-
if not v.isalnum() and '_' not in v:
451+
if not re.search(r"^\w+$", v):
452452
v = HEADER_JOIN_ESCAPE_RE.sub(r"\\\1", v) # escape " and \
453453
v = '"%s"' % v
454454
k = "%s=%s" % (k, v)

Lib/test/test_http_cookiejar.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,19 @@ def test_parse_ns_headers_special_names(self):
227227
self.assertEqual(parse_ns_headers([hdr]), expected)
228228

229229
def test_join_header_words(self):
230-
joined = join_header_words([[("foo", None), ("bar", "baz")]])
231-
self.assertEqual(joined, "foo; bar=baz")
232-
233-
self.assertEqual(join_header_words([[]]), "")
230+
for src, expected in [
231+
([[("foo", None), ("bar", "baz")]], "foo; bar=baz"),
232+
(([]), ""),
233+
(([[]]), ""),
234+
(([[("a", "_")]]), "a=_"),
235+
(([[("a", ";")]]), 'a=";"'),
236+
([[("n", None), ("foo", "foo;_")], [("bar", "foo_bar")]],
237+
'n; foo="foo;_", bar=foo_bar'),
238+
([[("n", "m"), ("foo", None)], [("bar", "foo_bar")]],
239+
'n=m; foo, bar=foo_bar'),
240+
]:
241+
with self.subTest(src=src):
242+
self.assertEqual(join_header_words(src), expected)
234243

235244
def test_split_header_words(self):
236245
tests = [
@@ -286,7 +295,10 @@ def test_roundtrip(self):
286295
'foo=bar; port="80,81"; discard, bar=baz'),
287296

288297
(r'Basic realm="\"foo\\\\bar\""',
289-
r'Basic; realm="\"foo\\\\bar\""')
298+
r'Basic; realm="\"foo\\\\bar\""'),
299+
300+
('n; foo="foo;_", bar=foo!_',
301+
'n; foo="foo;_", bar="foo!_"'),
290302
]
291303

292304
for arg, expect in tests:

Misc/NEWS.d/next/Library/2025-01-18-11-40-11.gh-issue-128982.557lS5.rst

-2
This file was deleted.

0 commit comments

Comments
 (0)