Issue #23688: Added support of arbitrary bytes-like objects and avoided

unnecessary copying of memoryview in gzip.GzipFile.write().
Original patch by Wolfgang Maier.
This commit is contained in:
Serhiy Storchaka 2015-03-23 14:59:48 +02:00
parent 77d899726f
commit bca63b362d
4 changed files with 56 additions and 8 deletions

View file

@ -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()