From 9ac14288d7147dbbae08a8ffd8581e0f5e6fd706 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Wed, 26 Nov 2025 07:46:25 -0800 Subject: [PATCH] gh-141968: use `bytearray.take_bytes` in `encodings.idna` (#141975) --- Lib/encodings/idna.py | 5 +++-- .../Library/2025-11-25-23-35-07.gh-issue-141968.b3Gscp.rst | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-11-25-23-35-07.gh-issue-141968.b3Gscp.rst diff --git a/Lib/encodings/idna.py b/Lib/encodings/idna.py index 0c90b4c9fe18..d31ee07ab45b 100644 --- a/Lib/encodings/idna.py +++ b/Lib/encodings/idna.py @@ -226,7 +226,8 @@ class Codec(codecs.Codec): offset + exc.end, exc.reason, ) - return bytes(result+trailing_dot), len(input) + result += trailing_dot + return result.take_bytes(), len(input) def decode(self, input, errors='strict'): @@ -311,7 +312,7 @@ class IncrementalEncoder(codecs.BufferedIncrementalEncoder): result += trailing_dot size += len(trailing_dot) - return (bytes(result), size) + return (result.take_bytes(), size) class IncrementalDecoder(codecs.BufferedIncrementalDecoder): def _buffer_decode(self, input, errors, final): diff --git a/Misc/NEWS.d/next/Library/2025-11-25-23-35-07.gh-issue-141968.b3Gscp.rst b/Misc/NEWS.d/next/Library/2025-11-25-23-35-07.gh-issue-141968.b3Gscp.rst new file mode 100644 index 000000000000..32658be5b2f6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-11-25-23-35-07.gh-issue-141968.b3Gscp.rst @@ -0,0 +1,2 @@ +Remove data copy from :mod:`encodings.idna` :meth:`~codecs.Codec.encode` and +:meth:`~codecs.IncrementalEncoder.encode` by using :func:`bytearray.take_bytes`. -- 2.47.3