Skip to content

Conversation

@cmaloney
Copy link
Contributor

@cmaloneycmaloney commented Nov 13, 2025

If the underlying I/O class keeps a reference to the memory raise BufferError.

Uses gh-139871 to implement.

--
If want to maintain closer compatibility when a BufferError occurs can fall back to a copy (copying is as safe as the original code). I have a slight preference to erroring as I think keeping a reference to the memory is uncommon (and probably unintended).

If the underlying I/O class keeps a reference to the memory raise BufferError.
}
returnres;
}
res=PyObject_CallMethod(b, "take_bytes", NULL);
Copy link
ContributorAuthor

@cmaloneycmaloneyNov 13, 2025

Choose a reason for hiding this comment

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

@vstinner : Not sure how common this "resize/discard then take_bytes" is going to be; might make sense to change to take_bytes(n=None, /, *, discard=False)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

for now planning to keep that in back pocket until need many ways (ba.resize(n) or del ba[:n] gives the same capability)

Copy link
Member

@vstinnervstinner left a comment

Choose a reason for hiding this comment

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

Can you also update RawIOBase.read() in the _pyio module?

@cmaloney
Copy link
ContributorAuthor

Have that in #141539 which does all the cases .take_bytes can be used in _pyio :).

cmaloneyand others added 2 commits November 18, 2025 14:35
Co-authored-by: Victor Stinner <vstinner@python.org>
Copy link
Member

@vstinnervstinner left a comment

Choose a reason for hiding this comment

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

LGTM

@vstinnervstinner enabled auto-merge (squash) November 20, 2025 17:06
@vstinnervstinner merged commit e90061f into python:mainNov 20, 2025
46 checks passed
@cmaloneycmaloney deleted the gh60107_take_bytes branch November 20, 2025 17:37
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
If the underlying I/O class keeps a reference to the memory, raise BufferError. Co-authored-by: Victor Stinner <vstinner@python.org>
ashm-dev pushed a commit to ashm-dev/cpython that referenced this pull request Dec 8, 2025
If the underlying I/O class keeps a reference to the memory, raise BufferError. Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@cmaloney@vstinner