https://github.com/python/cpython/commit/5a7c9c6861009d3b2e4b566c01f2fb2e14b48514
commit: 5a7c9c6861009d3b2e4b566c01f2fb2e14b48514
branch: main
author: Cody Maloney <[email protected]>
committer: vstinner <[email protected]>
date: 2025-11-28T17:47:14Z
summary:

gh-141968: Use take_bytes in encodings.punycode (#141974)

Removes a copy going from bytearray to bytes.

Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-11-25-23-29-08.gh-issue-141968.0JnjXf.rst
M Lib/encodings/punycode.py

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..0cefeed30ed8a9
--- /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
+:meth:`bytearray.take_bytes`.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to