GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-156939: Document that PyBytesObject ends with a NUL byte by vstinner · Pull Request #157236 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Sep 9, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Issue: C API: Detect buffer overflow in PyBytesWriter #156939

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner
pythongh-156939: Document that PyBytesObject ends with a NUL byte
9ae141e
@vstinner
vstinner requested a review from ZeroIntensity as a code owner September 9, 2026 20:04
@vstinner vstinner added topic-C-API needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 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
@bedevere-app bedevere-app Bot mentioned this pull request Sep 9, 2026
C API: Detect buffer overflow in PyBytesWriter #156939
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Sep 9, 2026
@github-project-automation github-project-automation Bot added this to Docs PRs 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 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34569933 | 📁 Comparing 1544e00 against main (9398655)

  🔍 Preview build  

57 files changed · + 8 added · ± 48 modified · - 1 deleted

+ Added

  • builtins/constants.html
  • builtins/exceptions.html
  • builtins/functions.html
  • builtins/index.html
  • builtins/stdtypes.html
  • builtins/threadsafety.html
  • builtins/time-complexity.html
  • library/tkinter.dialogs.html

± Modified

  • glossary.html
  • index.html
  • c-api/bytearray.html
  • c-api/bytes.html
  • c-api/complex.html
  • c-api/typeobj.html
  • extending/index.html
  • faq/general.html
  • howto/functional.html
  • library/bz2.html
  • and 38 more...

- Deleted

  • library/dialog.html
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Don't add the note as an implementation detail
7a2b992
@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)."
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Replace NUL with null
65550b7
For consistency with the page.
@ngoldbaum

ngoldbaum commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Seems worth mentioning to me!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ngoldbaum ngoldbaum mentioned this pull request Sep 10, 2026
Several C API functions do not return NUL-terminated buffers RustPython/RustPython#8685
Open
@vstinner
Add the note as a CPython implementation detail
1d3f14c
@vstinner

vstinner commented Sep 14, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

encukou
encukou approved these changes Sep 15, 2026
View reviewed changes

@encukou encukou left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/c-api/bytes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Sep 15, 2026
vstinner and others added 2 commits September 15, 2026 13:58
@vstinner @encukou
Update Doc/c-api/bytes.rst
6b25e03
Co-authored-by: Petr Viktorin <[email protected]>
@vstinner
Add similar note to bytearray
6756375
@vstinner

vstinner commented Sep 15, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

encukou
encukou reviewed Sep 15, 2026
View reviewed changes
Comment thread Doc/c-api/bytearray.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

vstinner and others added 2 commits September 15, 2026 14:13
@vstinner @encukou
Update Doc/c-api/bytearray.rst
34ecdfd
Co-authored-by: Petr Viktorin <[email protected]>
@vstinner
Merge branch 'main' into bytes_nul
1544e00
@vstinner

vstinner commented Sep 15, 2026

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner deleted the bytes_nul branch September 15, 2026 12:36
@miss-islington-app

miss-islington-app Bot commented Sep 15, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Sep 15, 2026
@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Sep 15, 2026
vstinner added a commit that referenced this pull request Sep 15, 2026
@miss-islington @vstinner @encukou
[3.14] gh-156939: Document that PyBytesObject ends with a NUL byte (G…
2327e11
…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 <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
vstinner added a commit that referenced this pull request Sep 15, 2026
@miss-islington @vstinner @encukou
[3.13] gh-156939: Document that PyBytesObject ends with a NUL byte (G…
4266e34
…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 <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
@vstinner

vstinner commented Sep 15, 2026

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

hugovk pushed a commit that referenced this pull request Sep 15, 2026
@miss-islington @vstinner @encukou
[3.15] gh-156939: Document that PyBytesObject ends with a NUL byte (G…
ec7bf22
…H-157236) (#157550)

Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
@vstinner vstinner mentioned this pull request Sep 17, 2026
gh-156939: Document that PyASCIIObject ends with null byte #157708
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@encukou encukou encukou approved these changes
@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity ZeroIntensity is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

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

Projects

Docs PRs
Status: Done

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@vstinner @ngoldbaum @encukou
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.