From: Cody Maloney Date: Wed, 26 Nov 2025 15:44:25 +0000 (-0800) Subject: gh-141968: Use `bytearray.take_bytes` in `base64` `_b32encode` and `_b32decode` ... X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2c1fdf3592cee02f7d2e2f250f2fbd67ddea0a2c;p=thirdparty%2FPython%2Fcpython.git gh-141968: Use `bytearray.take_bytes` in `base64` `_b32encode` and `_b32decode` (#141971) --- diff --git a/Lib/base64.py b/Lib/base64.py index f95132a42740..341bf8eaf189 100644 --- a/Lib/base64.py +++ b/Lib/base64.py @@ -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 index 000000000000..8492cb35d5bc --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-11-25-22-54-07.gh-issue-141968.vg3AMJ.rst @@ -0,0 +1,2 @@ +Remove a data copy from :func:`base64.b32decode` and +:func:`base64.b32encode` by using :func:`bytearray.take_bytes`.