← 输入框|github.com|刷新
Skip to content

gh-71448: Support extension in BytesIO.truncate - #160933

Open
cmaloney wants to merge 3 commits into
python:mainfrom
cmaloney:bytesio_truncate_extend
Open

cmaloney wants to merge 3 commits into
python:mainfrom
cmaloney:bytesio_truncate_extend

Conversation

@cmaloney

@cmaloney cmaloney commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

BytesIO.truncate() past the end now zero-fills, as IOBase.truncate() documents. A buffer shared with the caller (initial bytes, getvalue()) is unshared first, so the caller's bytes are never written.

To support this, resize_buffer_lock_held() is split into realloc_buffer_lock_held() (exact size) and prepare_write_lock_held() (private, overallocated).

_pyio.StringIO also extends now, via _pyio.BytesIO; the C StringIO does not. _pyio.StringIO and _io.StringIO already differ in truncate units (code points vs UTF-8 bytes). For now leaving that gap.

cmaloney and others added 2 commits October 29, 2026 14:50
… helpers

realloc_buffer_lock_held() reallocates to an exact size, unsharing if
needed; prepare_write_lock_held() ensures a private buffer of at least
the given size with the existing overallocation policy.  Callers no
longer open-code the shared/unshared choice.  No behavior change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
truncate() past the end now zero-fills, as IOBase.truncate() documents.
A buffer shared with the caller is unshared first, even when the new
size fits the current allocation.

_pyio.StringIO inherits this through _pyio.BytesIO; the C StringIO does
not extend.  The two already disagree on truncate units (code points vs
UTF-8 bytes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cmaloney cmaloney changed the title gh-71448: Support extension in BytesIO.truncate() gh-71448: Support extension in BytesIO.truncate Oct 29, 2026
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 29, 2026
@read-the-docs-community

read-the-docs-community Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35510179 | 📁 Comparing 9e0377c against main (ef1fce0)

  🔍 Preview build  

2 files changed
± library/io.html
± whatsnew/changelog.html

… buffers

The loop wrote to both streams first, so neither started shared, and
which path the second size took depended on the build: getvalue()
leaves the buffer shared on free-threading (sticky buf_shared) but not
on the default build once its result is dropped.  Use a fresh shared
and private stream for each size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant