Skip to content

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

Closed
cmaloney wants to merge 2 commits into
python:mainfrom
cmaloney:bytesio_turncate2
Closed

cmaloney wants to merge 2 commits into
python:mainfrom
cmaloney:bytesio_turncate2

Conversation

@cmaloney

@cmaloney cmaloney commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Truncate beyond current size of I/O objects is documented to change the amount of allocated space. BytesIO supported truncate to shrink but not truncate to extend. Update BytesIO to support extension and fully implement the IOBase.truncate() API.

Truncate beyond current size of I/O objects is documented to change the
amount of allocated space. BytesIO supported truncate to shrink but not
truncate to extend. Update BytesIO to support extension and fully
implement the `IOBase.truncate()` API.
@read-the-docs-community

read-the-docs-community Bot commented Jun 26, 2026 •

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 5, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 7, 2026
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Oct 11, 2026
@cmaloney

Copy link
Copy Markdown
Contributor Author

@eendebakpt could you take a look at this one?

@eendebakpt

Copy link
Copy Markdown
Contributor

Could you check the following cases?

data = b"abcdef"
b = io.BytesIO(data)   # buf is `data` itself (exact bytes, no copy)
b.truncate(3)          # 3 < alloc 6: quick exit, still shared
b.truncate(5)          # 5 < alloc 6: quick exit, memset
data                   # b'abc\x00\x00f'  -- an immutable constant mutated

and

b = io.BytesIO(b"abc")
b.truncate(10**9)      # Segmentation fault?

@cmaloney

Copy link
Copy Markdown
Contributor Author

Created gh-160781 for some underlying pieces which should be backported that found while updating this

@cmaloney

cmaloney commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Closing until gh-160781 is done. Making this work well on large sizes is a lot easier with that. Will make a new PR after that which covers the extended cases here.

Ended up refactoring resize_buffer_lock_held into two helpers which de-duplicates code and makes the extend easier, but that needs the large size edge cases first (and pulling into this PR is a lot more work then opening a new one)

@cmaloney cmaloney closed this Oct 27, 2026
@cmaloney
cmaloney deleted the bytesio_turncate2 branch October 29, 2026 19:52
@cmaloney

Copy link
Copy Markdown
Contributor Author

Replacement PR is up: #160933. Properly unshares branches, handles large truncates gracefully.

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.

2 participants