summaryrefslogtreecommitdiffstats
path: root/Lib/gzip.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-03-23 12:59:48 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-03-23 12:59:48 (GMT)
commitbca63b362d23f154a5ed7fe43e4146977bba181e (patch)
tree149fca58f15a3d6d4b38311f081f49976671074b /Lib/gzip.py
parent77d899726f4f7c78757e4214c147e699b285a8a7 (diff)
downloadcpython-bca63b362d23f154a5ed7fe43e4146977bba181e.zip
cpython-bca63b362d23f154a5ed7fe43e4146977bba181e.tar.gz
cpython-bca63b362d23f154a5ed7fe43e4146977bba181e.tar.bz2
Issue #23688: Added support of arbitrary bytes-like objects and avoided
unnecessary copying of memoryview in gzip.GzipFile.write(). Original patch by Wolfgang Maier.
Diffstat (limited to 'Lib/gzip.py')
-rw-r--r--Lib/gzip.py19
1 files changed, 11 insertions, 8 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py
index f934d4f..21d83e6 100644
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -334,17 +334,20 @@ class GzipFile(io.BufferedIOBase):
if self.fileobj is None:
raise ValueError("write() on closed GzipFile object")
- # Convert data type if called by io.BufferedWriter.
- if isinstance(data, memoryview):
- data = data.tobytes()
+ if isinstance(data, bytes):
+ length = len(data)
+ else:
+ # accept any data that supports the buffer protocol
+ data = memoryview(data)
+ length = data.nbytes
- if len(data) > 0:
- self.size = self.size + len(data)
+ if length > 0:
+ self.fileobj.write(self.compress.compress(data))
+ self.size += length
self.crc = zlib.crc32(data, self.crc) & 0xffffffff
- self.fileobj.write( self.compress.compress(data) )
- self.offset += len(data)
+ self.offset += length
- return len(data)
+ return length
def read(self, size=-1):
self._check_closed()