diff options
author | Chris Markiewicz <markiewicz@stanford.edu> | 2025-03-07 22:04:45 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-07 21:04:45 -0600 |
commit | 72e5b25efb580fb1f0fdfade516be90d90822164 (patch) | |
tree | 64dae27e381d598dec62d62d48c6e35d65bc6a94 /Lib/gzip.py | |
parent | 78790811989ab47319e2ee725e0c435b3cdd21ab (diff) | |
download | cpython-72e5b25efb580fb1f0fdfade516be90d90822164.tar.gz cpython-72e5b25efb580fb1f0fdfade516be90d90822164.zip |
gh-128646: Implement GzipFile.readinto[1]() methods (GH-128647)
The new methods simply delegate to the underlying buffer, much like the existing GzipFile.read[1] methods. This avoids extra allocations caused by the BufferedIOBase.readinto implementation previously used.
This commit also factors out a common readability check rather than copying it an additional two times.
Diffstat (limited to 'Lib/gzip.py')
-rw-r--r-- | Lib/gzip.py | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py index 7e384f8a568..d681ef6b488 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -325,11 +325,15 @@ class GzipFile(_compression.BaseStream): return length - def read(self, size=-1): - self._check_not_closed() + def _check_read(self, caller): if self.mode != READ: import errno - raise OSError(errno.EBADF, "read() on write-only GzipFile object") + msg = f"{caller}() on write-only GzipFile object" + raise OSError(errno.EBADF, msg) + + def read(self, size=-1): + self._check_not_closed() + self._check_read("read") return self._buffer.read(size) def read1(self, size=-1): @@ -337,19 +341,25 @@ class GzipFile(_compression.BaseStream): Reads up to a buffer's worth of data if size is negative.""" self._check_not_closed() - if self.mode != READ: - import errno - raise OSError(errno.EBADF, "read1() on write-only GzipFile object") + self._check_read("read1") if size < 0: size = io.DEFAULT_BUFFER_SIZE return self._buffer.read1(size) + def readinto(self, b): + self._check_not_closed() + self._check_read("readinto") + return self._buffer.readinto(b) + + def readinto1(self, b): + self._check_not_closed() + self._check_read("readinto1") + return self._buffer.readinto1(b) + def peek(self, n): self._check_not_closed() - if self.mode != READ: - import errno - raise OSError(errno.EBADF, "peek() on write-only GzipFile object") + self._check_read("peek") return self._buffer.peek(n) @property |