diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index a87898dadf4af6..8b896011734df5 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -3240,7 +3240,7 @@ objects. See the :ref:`What's New ` entry for common code patterns which can be optimized with - :func:`bytearray.take_bytes`. + :meth:`bytearray.take_bytes`. Since bytearray objects are sequences of integers (akin to a list), for a bytearray object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be 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/Lib/re/_compiler.py b/Lib/re/_compiler.py index 20dd561d1c1520..c2ca8e25abe34d 100644 --- a/Lib/re/_compiler.py +++ b/Lib/re/_compiler.py @@ -375,7 +375,7 @@ def _optimize_charset(charset, iscased=None, fixup=None, fixes=None): # less significant byte is a bit index in the chunk (just like the # CHARSET matching). - charmap = bytes(charmap) # should be hashable + charmap = charmap.take_bytes() # should be hashable comps = {} mapping = bytearray(256) block = 0 diff --git a/Misc/NEWS.d/3.15.0a2.rst b/Misc/NEWS.d/3.15.0a2.rst index ba439d49517add..4e3a62b0f4a7d2 100644 --- a/Misc/NEWS.d/3.15.0a2.rst +++ b/Misc/NEWS.d/3.15.0a2.rst @@ -1451,7 +1451,7 @@ under no memory. .. section: Core and Builtins Update :class:`bytearray` to use a :class:`bytes` under the hood as its -buffer and add :func:`bytearray.take_bytes` to take it out. +buffer and add :meth:`bytearray.take_bytes` to take it out. .. diff --git a/Misc/NEWS.d/next/Library/2025-11-22-16-33-48.gh-issue-141863.4PLhnv.rst b/Misc/NEWS.d/next/Library/2025-11-22-16-33-48.gh-issue-141863.4PLhnv.rst index 910ab98c950f9e..585774ba2cb66b 100644 --- a/Misc/NEWS.d/next/Library/2025-11-22-16-33-48.gh-issue-141863.4PLhnv.rst +++ b/Misc/NEWS.d/next/Library/2025-11-22-16-33-48.gh-issue-141863.4PLhnv.rst @@ -1,2 +1,2 @@ -Update :ref:`asyncio-streams` to use :func:`bytearray.take_bytes` for a over +Update :ref:`asyncio-streams` to use :meth:`bytearray.take_bytes` for a over 10% performance improvement on pyperformance asyncio_tcp benchmark. 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 index 8492cb35d5bc76..4c89902813f6a6 100644 --- 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 @@ -1,2 +1,2 @@ Remove a data copy from :func:`base64.b32decode` and -:func:`base64.b32encode` by using :func:`bytearray.take_bytes`. +:func:`base64.b32encode` by using :meth:`bytearray.take_bytes`. diff --git a/Misc/NEWS.d/next/Library/2025-11-25-23-22-46.gh-issue-141968.R1sHnJ.rst b/Misc/NEWS.d/next/Library/2025-11-25-23-22-46.gh-issue-141968.R1sHnJ.rst index 16b9ad78c3cd3f..eca0ac4c8e6014 100644 --- a/Misc/NEWS.d/next/Library/2025-11-25-23-22-46.gh-issue-141968.R1sHnJ.rst +++ b/Misc/NEWS.d/next/Library/2025-11-25-23-22-46.gh-issue-141968.R1sHnJ.rst @@ -1,2 +1,2 @@ Remove data copy from :func:`wave.Wave_read.readframes` and -:func:`wave.Wave_write.writeframes` by using :func:`bytearray.take_bytes`. +:func:`wave.Wave_write.writeframes` by using :meth:`bytearray.take_bytes`. 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`. 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 index 32658be5b2f632..50124e8b19609f 100644 --- 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 @@ -1,2 +1,2 @@ Remove data copy from :mod:`encodings.idna` :meth:`~codecs.Codec.encode` and -:meth:`~codecs.IncrementalEncoder.encode` by using :func:`bytearray.take_bytes`. +:meth:`~codecs.IncrementalEncoder.encode` by using :meth:`bytearray.take_bytes`. diff --git a/Misc/NEWS.d/next/Library/2025-11-26-14-20-10.gh-issue-141968.W139Pv.rst b/Misc/NEWS.d/next/Library/2025-11-26-14-20-10.gh-issue-141968.W139Pv.rst new file mode 100644 index 00000000000000..c5375707814ff5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-11-26-14-20-10.gh-issue-141968.W139Pv.rst @@ -0,0 +1,2 @@ +Remove data copy from :mod:`re` compilation of regexes with large charsets +by using :meth:`bytearray.take_bytes`. diff --git a/Tools/c-analyzer/cpython/_parser.py b/Tools/c-analyzer/cpython/_parser.py index d348a99fff7a11..6332dec3aefe4f 100644 --- a/Tools/c-analyzer/cpython/_parser.py +++ b/Tools/c-analyzer/cpython/_parser.py @@ -114,7 +114,10 @@ def format_tsv_lines(lines): ('*', './Include/internal/mimalloc'), ('Modules/_decimal/**/*.c', 'Modules/_decimal/libmpdec'), + ('Modules/_elementtree.c', 'Modules/expat'), + ('Modules/pyexpat.c', 'Modules/expat'), + ('Modules/_hacl/*.c', 'Modules/_hacl/include'), ('Modules/_hacl/*.c', 'Modules/_hacl/'), ('Modules/_hacl/*.h', 'Modules/_hacl/include'), @@ -125,6 +128,7 @@ def format_tsv_lines(lines): ('Modules/sha3module.c', 'Modules/_hacl/include'), ('Modules/blake2module.c', 'Modules/_hacl/include'), ('Modules/hmacmodule.c', 'Modules/_hacl/include'), + ('Objects/stringlib/*.h', 'Objects'), # possible system-installed headers, just in case