Skip to content

gh-156939: Document that PyBytesObject ends with a NUL byte - #157236

Merged
vstinner merged 8 commits into
python:mainfrom
vstinner:bytes_nul
Sep 15, 2026
Merged

vstinner merged 8 commits into
python:mainfrom
vstinner:bytes_nul

Conversation

@vstinner

@vstinner vstinner commented Sep 9, 2026

Copy link
Copy Markdown
Member

@vstinner vstinner added topic-C-API needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Sep 9, 2026
@bedevere-app bedevere-app Bot added docs Documentation in the Doc dir skip news labels Sep 9, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Docs PRs Sep 9, 2026
@read-the-docs-community

read-the-docs-community Bot commented Sep 9, 2026

Copy link
Copy Markdown

@vstinner

vstinner commented Sep 9, 2026

Copy link
Copy Markdown
Member Author

The trailing NUL byte property is already documented in:

  • PyBytes_AsString: "The last byte in the buffer is always null, regardless of whether there are any other null bytes."
  • PyBytes_AsStringAndSize: "The buffer refers to an internal buffer of obj, which includes an additional null byte at the end (not counted in length)."

For consistency with the page.
@ngoldbaum

Copy link
Copy Markdown
Contributor

Seems worth mentioning to me!

@encukou

encukou commented Sep 10, 2026

Copy link
Copy Markdown
Member

I'd rather keep this in the function docs. Or if it needs to be here, then as a CPython implementation detail. (In another implementation, PyBytes_AsString might realloc to add the null byte.)

@vstinner

Copy link
Copy Markdown
Member Author

I'd rather keep this in the function docs. Or if it needs to be here, then as a CPython implementation detail. (In another implementation, PyBytes_AsString might realloc to add the null byte.)

Ah ok, I see you point. It sounds inefficient to me to duplicate the whole string in PyBytes_AsString() to add an extra null byte. It would be more efficient to always allocate that byte. But I take your point and I modified to note to document it as a CPython implementation detail.

@encukou encukou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds inefficient to me to duplicate the whole string in PyBytes_AsString() to add an extra null byte.

In CPython, yes. I'm thinking about Jython-ish implementations where bytes could often reuse storage of a “native” object.

Comment thread Doc/c-api/bytes.rst Outdated
vstinner and others added 2 commits September 15, 2026 13:58
Co-authored-by: Petr Viktorin <encukou@gmail.com>
@vstinner

Copy link
Copy Markdown
Member Author

Oh, in fact bytearray also has this trailing null byte. I update my PR to add a similar implementation detail note to bytearray.

Comment thread Doc/c-api/bytearray.rst Outdated
vstinner and others added 2 commits September 15, 2026 14:13
Co-authored-by: Petr Viktorin <encukou@gmail.com>
@vstinner

Copy link
Copy Markdown
Member Author

I updated the branch to retrieve a fix for the Docs CI.

@vstinner
vstinner enabled auto-merge (squash) September 15, 2026 12:30
@vstinner
vstinner merged commit 2fcb0e2 into python:main Sep 15, 2026
30 checks passed
@vstinner
vstinner deleted the bytes_nul branch September 15, 2026 12:36
@miss-islington-app

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@github-project-automation github-project-automation Bot moved this from Todo to Done in Docs PRs Sep 15, 2026
@bedevere-app

bedevere-app Bot commented Sep 15, 2026

Copy link
Copy Markdown

GH-157550 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 15, 2026
@bedevere-app

bedevere-app Bot commented Sep 15, 2026

Copy link
Copy Markdown

GH-157551 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Sep 15, 2026
@bedevere-app

bedevere-app Bot commented Sep 15, 2026

Copy link
Copy Markdown

GH-157552 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Sep 15, 2026
vstinner added a commit that referenced this pull request Sep 15, 2026
…H-157236) (#157551)

gh-156939: Document that PyBytesObject ends with a NUL byte (GH-157236)

Document as an implementation detail that PyBytesObject and ByteArrayObject end
with a NUL byte.
(cherry picked from commit 2fcb0e2)

Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
vstinner added a commit that referenced this pull request Sep 15, 2026
…H-157236) (#157552)

gh-156939: Document that PyBytesObject ends with a NUL byte (GH-157236)

Document as an implementation detail that PyBytesObject and ByteArrayObject end
with a NUL byte.
(cherry picked from commit 2fcb0e2)

Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
@vstinner

Copy link
Copy Markdown
Member Author

Thanks for your help on that doc change @encukou.

@ngoldbaum spotted that RustPython omits this trailing null bytes in its C API! RustPython/RustPython#8685

hugovk pushed a commit that referenced this pull request Sep 15, 2026
…H-157236) (#157550)

Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news topic-C-API

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants