From 3600bb629c5f1c9729cd16c70d299d0e3d753423 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Tue, 25 Nov 2025 23:29:17 -0800 Subject: [PATCH 1/2] gh-141968: Use take_byes in encodings.punycode Removes a copy going from bytearray to bytes. --- Lib/encodings/punycode.py | 6 +++--- .../Library/2025-11-25-23-29-08.gh-issue-141968.0JnjXf.rst | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-11-25-23-29-08.gh-issue-141968.0JnjXf.rst diff --git a/Lib/encodings/punycode.py b/Lib/encodings/punycode.py index 4622fc8c9206f3..268fccbd53974e 100644 --- a/Lib/encodings/punycode.py +++ b/Lib/encodings/punycode.py @@ -17,7 +17,7 @@ def segregate(str): else: extended.add(c) extended = sorted(extended) - return bytes(base), extended + return base.take_bytes(), extended def selective_len(str, max): """Return the length of str, considering only characters below max.""" @@ -83,7 +83,7 @@ def generate_generalized_integer(N, bias): t = T(j, bias) if N < t: result.append(digits[N]) - return bytes(result) + return result.take_bytes() result.append(digits[t + ((N - t) % (36 - t))]) N = (N - t) // (36 - t) j += 1 @@ -112,7 +112,7 @@ def generate_integers(baselen, deltas): s = generate_generalized_integer(delta, bias) result.extend(s) bias = adapt(delta, points==0, baselen+points+1) - return bytes(result) + return result.take_bytes() def punycode_encode(text): base, extended = segregate(text) diff --git a/Misc/NEWS.d/next/Library/2025-11-25-23-29-08.gh-issue-141968.0JnjXf.rst b/Misc/NEWS.d/next/Library/2025-11-25-23-29-08.gh-issue-141968.0JnjXf.rst new file mode 100644 index 00000000000000..0ec0627e4e3765 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-11-25-23-29-08.gh-issue-141968.0JnjXf.rst @@ -0,0 +1,2 @@ +Remove data copy from :mod:`codecs` ``punycode`` encoding by using +:func:`bytearray.take_bytes`. From 69cdccc55b9dfa3d3d675f02232a86ffea1631a6 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 28 Nov 2025 18:18:23 +0100 Subject: [PATCH 2/2] Update Misc/NEWS.d/next/Library/2025-11-25-23-29-08.gh-issue-141968.0JnjXf.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../next/Library/2025-11-25-23-29-08.gh-issue-141968.0JnjXf.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-11-25-23-29-08.gh-issue-141968.0JnjXf.rst b/Misc/NEWS.d/next/Library/2025-11-25-23-29-08.gh-issue-141968.0JnjXf.rst index 0ec0627e4e3765..0cefeed30ed8a9 100644 --- a/Misc/NEWS.d/next/Library/2025-11-25-23-29-08.gh-issue-141968.0JnjXf.rst +++ b/Misc/NEWS.d/next/Library/2025-11-25-23-29-08.gh-issue-141968.0JnjXf.rst @@ -1,2 +1,2 @@ Remove data copy from :mod:`codecs` ``punycode`` encoding by using -:func:`bytearray.take_bytes`. +:meth:`bytearray.take_bytes`.