]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-141968: Use `bytearray.take_bytes` in `base64` `_b32encode` and `_b32decode` ...
authorCody Maloney <cmaloney@users.noreply.github.com>
Wed, 26 Nov 2025 15:44:25 +0000 (07:44 -0800)
committerGitHub <noreply@github.com>
Wed, 26 Nov 2025 15:44:25 +0000 (21:14 +0530)
Lib/base64.py
Misc/NEWS.d/next/Library/2025-11-25-22-54-07.gh-issue-141968.vg3AMJ.rst [new file with mode: 0644]

index f95132a42740517665dd4a75678aa3a29191165a..341bf8eaf1891e8ef352e6bb407a1188287d9c22 100644 (file)
@@ -193,7 +193,7 @@ def _b32encode(alphabet, s):
         encoded[-3:] = b'==='
     elif leftover == 4:
         encoded[-1:] = b'='
-    return bytes(encoded)
+    return encoded.take_bytes()
 
 def _b32decode(alphabet, s, casefold=False, map01=None):
     # Delay the initialization of the table to not waste memory
@@ -238,7 +238,7 @@ def _b32decode(alphabet, s, casefold=False, map01=None):
         last = acc.to_bytes(5)  # big endian
         leftover = (43 - 5 * padchars) // 8  # 1: 4, 3: 3, 4: 2, 6: 1
         decoded[-5:] = last[:leftover]
-    return bytes(decoded)
+    return decoded.take_bytes()
 
 
 def b32encode(s):
diff --git a/Misc/NEWS.d/next/Library/2025-11-25-22-54-07.gh-issue-141968.vg3AMJ.rst b/Misc/NEWS.d/next/Library/2025-11-25-22-54-07.gh-issue-141968.vg3AMJ.rst
new file mode 100644 (file)
index 0000000..8492cb3
--- /dev/null
@@ -0,0 +1,2 @@
+Remove a data copy from :func:`base64.b32decode` and
+:func:`base64.b32encode` by using :func:`bytearray.take_bytes`.