From 9ae141ea3a7142e4affb7c7945663839fd0b9977 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 9 Sep 2026 22:00:37 +0200 Subject: [PATCH 1/7] gh-156939: Document that PyBytesObject ends with a NUL byte --- Doc/c-api/bytes.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst index ff68ecafcda4d08..c6f038409e1d499 100644 --- a/Doc/c-api/bytes.rst +++ b/Doc/c-api/bytes.rst @@ -8,6 +8,10 @@ Bytes Objects These functions raise :exc:`TypeError` when expecting a bytes parameter and called with a non-bytes parameter. +.. impl-detail:: + A :c:type:`PyBytesObject` allocates an extra trailing NUL byte for + compatibility with NUL terminated C strings. + .. index:: pair: object; bytes From 7a2b992f81c9cc3e1b2c7ccf8f4828a0b50898d2 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 9 Sep 2026 22:09:31 +0200 Subject: [PATCH 2/7] Don't add the note as an implementation detail --- Doc/c-api/bytes.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst index c6f038409e1d499..ba719f9648d19de 100644 --- a/Doc/c-api/bytes.rst +++ b/Doc/c-api/bytes.rst @@ -8,9 +8,9 @@ Bytes Objects These functions raise :exc:`TypeError` when expecting a bytes parameter and called with a non-bytes parameter. -.. impl-detail:: - A :c:type:`PyBytesObject` allocates an extra trailing NUL byte for - compatibility with NUL terminated C strings. +A :c:type:`PyBytesObject` allocates an extra trailing NUL byte for +compatibility with NUL terminated C strings. It is not counted in +:c:func:`PyBytes_Size` length. .. index:: pair: object; bytes From 65550b742c7d3d0a0e23cff3972bf2bc7893b032 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 9 Sep 2026 22:12:20 +0200 Subject: [PATCH 3/7] Replace NUL with null For consistency with the page. --- Doc/c-api/bytes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst index ba719f9648d19de..9fd4284f0035042 100644 --- a/Doc/c-api/bytes.rst +++ b/Doc/c-api/bytes.rst @@ -8,8 +8,8 @@ Bytes Objects These functions raise :exc:`TypeError` when expecting a bytes parameter and called with a non-bytes parameter. -A :c:type:`PyBytesObject` allocates an extra trailing NUL byte for -compatibility with NUL terminated C strings. It is not counted in +A :c:type:`PyBytesObject` allocates an extra trailing null byte for +compatibility with null terminated C strings. It is not counted in :c:func:`PyBytes_Size` length. .. index:: pair: object; bytes From 1d3f14c44e627ea167a3c66751c73f89a6dab986 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 14 Sep 2026 18:15:07 +0200 Subject: [PATCH 4/7] Add the note as a CPython implementation detail --- Doc/c-api/bytes.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst index 9fd4284f0035042..365d1ab77d01085 100644 --- a/Doc/c-api/bytes.rst +++ b/Doc/c-api/bytes.rst @@ -8,9 +8,11 @@ Bytes Objects These functions raise :exc:`TypeError` when expecting a bytes parameter and called with a non-bytes parameter. -A :c:type:`PyBytesObject` allocates an extra trailing null byte for -compatibility with null terminated C strings. It is not counted in -:c:func:`PyBytes_Size` length. +.. impl-detail:: + + A :c:type:`PyBytesObject` allocates an extra trailing null byte for + compatibility with null terminated C strings. It is not counted in + :c:func:`PyBytes_Size` length. .. index:: pair: object; bytes From 6b25e03659894dcad13e499eda44422e1701abe7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 15 Sep 2026 13:58:34 +0200 Subject: [PATCH 5/7] Update Doc/c-api/bytes.rst Co-authored-by: Petr Viktorin --- Doc/c-api/bytes.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst index 365d1ab77d01085..688d7b6c32d9426 100644 --- a/Doc/c-api/bytes.rst +++ b/Doc/c-api/bytes.rst @@ -10,9 +10,10 @@ called with a non-bytes parameter. .. impl-detail:: - A :c:type:`PyBytesObject` allocates an extra trailing null byte for - compatibility with null terminated C strings. It is not counted in - :c:func:`PyBytes_Size` length. + The internal buffer of :c:type:`PyBytesObject` always includes an extra + trailing null byte for compatibility with null terminated C strings. + This extra byte is not counted in :c:func:`PyBytes_Size` nor in the + various *length* and *size* arguments of the functions below. .. index:: pair: object; bytes From 67563753a776f72b52609cee3bfd2aa065955eff Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 15 Sep 2026 14:01:02 +0200 Subject: [PATCH 6/7] Add similar note to bytearray --- Doc/c-api/bytearray.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/c-api/bytearray.rst b/Doc/c-api/bytearray.rst index 2b36da997d42956..6ef1f55e94da42b 100644 --- a/Doc/c-api/bytearray.rst +++ b/Doc/c-api/bytearray.rst @@ -12,6 +12,12 @@ Byte Array Objects This subtype of :c:type:`PyObject` represents a Python bytearray object. + .. impl-detail:: + + The internal buffer of :c:type:`PyByteArrayObject` always includes an + extra trailing null byte for compatibility with null terminated C + strings. This extra byte is not counted in :c:func:`PyByteArray_Size` + nor in the various *length* and *size* arguments of the functions below. .. c:var:: PyTypeObject PyByteArray_Type From 34ecdfddaa82c99ea21ba115db09ed80a822f54e Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 15 Sep 2026 14:13:31 +0200 Subject: [PATCH 7/7] Update Doc/c-api/bytearray.rst Co-authored-by: Petr Viktorin --- Doc/c-api/bytearray.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/c-api/bytearray.rst b/Doc/c-api/bytearray.rst index 6ef1f55e94da42b..8f3cb2d63cb3c82 100644 --- a/Doc/c-api/bytearray.rst +++ b/Doc/c-api/bytearray.rst @@ -17,7 +17,7 @@ Byte Array Objects The internal buffer of :c:type:`PyByteArrayObject` always includes an extra trailing null byte for compatibility with null terminated C strings. This extra byte is not counted in :c:func:`PyByteArray_Size` - nor in the various *length* and *size* arguments of the functions below. + nor in the *len* arguments of the functions below. .. c:var:: PyTypeObject PyByteArray_Type