New submission from liquidpele <liquidp...@gmail.com>:
reecepeg@3c22fb7d6b37 Pulpmill % python3 --version Python 3.9.1 When buffering a small file, calling peek() can read in the entire underlying thing and close it, so then following with a read() throws an error about the file being closed already even though peek should not have that effect. Reproducible Steps: >>> r = BufferedReader(requests.get("https://google.com", stream=True).raw) >>> r.peek(2)[:2] b'\x1f\x8b' >>> r.peek() Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: peek of closed file >>> r.read() Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: read of closed file However, in the case of a larger stream it appears to work since the underlying stream isn't closed yet: >>> r = BufferedReader(requests.get("https://amazon.com", stream=True).raw) >>> r.peek(2)[:2] b'\x1f\x8b' >>> r.peek(2)[:2] b'\x1f\x8b' This seems inconsistent at best. Best I can tell, the issue is here and needs to take the current buffer offset into account. https://github.com/python/cpython/blob/main/Modules/_io/bufferedio.c#L845 ---------- components: IO messages: 397907 nosy: liquidpele priority: normal severity: normal status: open title: io.BufferedReader:peek() closes underlying file, breaking peek/read expectations versions: Python 3.9 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue44687> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com