Okay, I got nerd-sniped and have now convinced myself that it is absolutely not worth the trouble of resolving this in _io/_pyio.
Even if we can resolve this in _io/_pyio, any attempt to use the base-class implementations will result in more indirection, not less, as the implementation will eventually have to come around to GzipFile.read() or GzipFile.read1(). In addition to simplifying the call stack, this PR makes reading the code simpler as well, as the chain of delegation is now more straightforward.
This adds two more copies of the snippet:
if self.mode != READ: import errno raise OSError(errno.EBADF, "read() on write-only GzipFile object")
We could just use self._check_can_read():
| class BaseStream(io.BufferedIOBase): | |
| """Mode-checking helper functions.""" | |
| def _check_not_closed(self): | |
| if self.closed: | |
| raise ValueError("I/O operation on closed file") | |
| def _check_can_read(self): | |
| if not self.readable(): | |
| raise io.UnsupportedOperation("File not open for reading") |
That exception subclasses OSError and ValueError, but doesn't set EBADF. I will include that change in this PR if asked by a core dev, but I will not expand the scope at present.
effigies deleted the fix/gzipfile-readinto branch
March 8, 2025 09:26