https://github.com/python/cpython/commit/9e474a98af4184615540467dea16da05f4d284d8 commit: 9e474a98af4184615540467dea16da05f4d284d8 branch: main author: Petr Viktorin <encu...@gmail.com> committer: encukou <encu...@gmail.com> date: 2025-02-26T15:42:39+01:00 summary:
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 56e190068177855266f32a7efa329d145b279f94. * Add tests files: D Misc/NEWS.d/next/Library/2025-01-18-11-40-11.gh-issue-128982.557lS5.rst M Lib/http/cookiejar.py M Lib/test/test_http_cookiejar.py diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 4ebb1601b4dd82..fb0fd2e97999af 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -448,7 +448,7 @@ def join_header_words(lists): attr = [] for k, v in pairs: if v is not None: - if not v.isalnum() and '_' not in v: + if not re.search(r"^\w+$", v): v = HEADER_JOIN_ESCAPE_RE.sub(r"\\\1", v) # escape " and \ v = '"%s"' % v k = "%s=%s" % (k, v) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index dbf9ce10f76f91..25a671809d4499 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -227,10 +227,19 @@ def test_parse_ns_headers_special_names(self): self.assertEqual(parse_ns_headers([hdr]), expected) def test_join_header_words(self): - joined = join_header_words([[("foo", None), ("bar", "baz")]]) - self.assertEqual(joined, "foo; bar=baz") - - self.assertEqual(join_header_words([[]]), "") + for src, expected in [ + ([[("foo", None), ("bar", "baz")]], "foo; bar=baz"), + (([]), ""), + (([[]]), ""), + (([[("a", "_")]]), "a=_"), + (([[("a", ";")]]), 'a=";"'), + ([[("n", None), ("foo", "foo;_")], [("bar", "foo_bar")]], + 'n; foo="foo;_", bar=foo_bar'), + ([[("n", "m"), ("foo", None)], [("bar", "foo_bar")]], + 'n=m; foo, bar=foo_bar'), + ]: + with self.subTest(src=src): + self.assertEqual(join_header_words(src), expected) def test_split_header_words(self): tests = [ @@ -286,7 +295,10 @@ def test_roundtrip(self): 'foo=bar; port="80,81"; discard, bar=baz'), (r'Basic realm="\"foo\\\\bar\""', - r'Basic; realm="\"foo\\\\bar\""') + r'Basic; realm="\"foo\\\\bar\""'), + + ('n; foo="foo;_", bar=foo!_', + 'n; foo="foo;_", bar="foo!_"'), ] for arg, expect in tests: diff --git a/Misc/NEWS.d/next/Library/2025-01-18-11-40-11.gh-issue-128982.557lS5.rst b/Misc/NEWS.d/next/Library/2025-01-18-11-40-11.gh-issue-128982.557lS5.rst deleted file mode 100644 index 44b7a7b537364c..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-01-18-11-40-11.gh-issue-128982.557lS5.rst +++ /dev/null @@ -1,2 +0,0 @@ -Improve the performance of :func:`!http.cookiejar.join_header_words` by up -to 35%. Patch by Bénédikt Tran. _______________________________________________ Python-checkins mailing list -- python-checkins@python.org To unsubscribe send an email to python-checkins-le...@python.org https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: arch...@mail-archive.com