Skip to content

Synchronize file position when opening a BufferedReader#2

Open
bemoody wants to merge 2 commits into
mainfrom
buffered-open
Open

Synchronize file position when opening a BufferedReader#2
bemoody wants to merge 2 commits into
mainfrom
buffered-open

Conversation

@bemoody
Copy link
Copy Markdown

@bemoody bemoody commented May 6, 2026

If we pass an existing file object to plibflac.Decoder, we want to start decoding at the current position in the input stream. If the input is a buffered file object, the input position might not be the same as the position of the underlying file descriptor.

(We do want to use direct file descriptor I/O if we can, bypassing the Python buffering layer, because it's significantly faster and more parallelizable.)

Previously the code was supposed to handle this but didn't actually work as intended - calling seek on a BufferedReader isn't guaranteed to set the position of the underlying FD.

Fixes #1

Benjamin Moody added 2 commits May 6, 2026 14:33
When reading from a buffered file object (created by open(file, "rb")
or similar), we want to use direct file descriptor I/O for efficiency.
However, we want to start reading from the current position in the
buffered stream (if the caller previously called read() or seek() on
this file object, the buffered stream position isn't necessarily equal
to the underlying file descriptor's position.)

Calling seek() on the buffered file object isn't enough to do this:
the effect on the raw file descriptor is not documented, and in
current versions of CPython it doesn't perform a real seek if the
destination is within the buffer.  So instead, call seek() on the raw
file object.

Afterwards, the buffer will be out of sync with the underlying file
(which was always true, it just wasn't obvious.)
Regression test for issue #1: previously, the decoder would not
correctly set the file position if the input was a buffered file
object that already had some data in its buffer.
@bemoody bemoody mentioned this pull request May 6, 2026
@tauroid
Copy link
Copy Markdown

tauroid commented May 7, 2026

Makes sense I think, just see this comment: #1 (comment)

Which is effectively "can we also put the BufferedReader (/ Writer) back in a sane state on close()?" Maybe the "recover original state" step is unnecessary if the raw position is never depended on; that's not quite clear to me.

Sign up for free to 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.

Bug with BufferedReader

2 participants