Does the raw passed to io.BufferedReader(raw) have to be "non seekable"?

It said in the document io — Core tools for working with streams — Python 3.14.0 documentation

class io.BufferedReader(raw, buff er_size=DEFAULT_BUFFER_SIZE )
A buffered binary stream providing higher-level access to a readable, non seekable RawIOBase raw binary stream.

Below I create an instance by passing a seekable raw stream without problem?

>>> file = open("test.txt", "rb", buffering=0)
>>> file.seekable()
True
>>> fb = io.BufferedReader(file)
>>> fb.seekable()
True

1 Like

They could be improved, but I think the point of the docs, is that passing a non-seekable stream to an io.BufferedRandom will error (in use, if not on object creation - I’ve not tested this).

What BufferedReader does with seekables is much of a muchness. But if a BufferedReader can’t assume the underlying base stream has .seek and .tell methods, it won’t be as efficient as a BufferedRandom on a seekable.

2 Likes

:+1: BufferedReader.seekable attribute just returns raw.seekable. The docs I think are trying to differentiate that BufferedWriter and BufferedRandom require “seekable” (seek and tell should work on the fd) while BufferedReader does not. Definitely welcome a patch to make that clearer :slight_smile:

re: Efficiency shouldn’t actually be effected in this case. Read-only I/O is a lot simpler to buffer than write or read-write.

1 Like