mirror of https://github.com/python/cpython.git
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:
parent
77d899726f
commit
bca63b362d
|
@ -137,6 +137,10 @@ The module defines the following items:
|
||||||
.. versionchanged:: 3.4
|
.. versionchanged:: 3.4
|
||||||
Added support for the ``'x'`` and ``'xb'`` modes.
|
Added support for the ``'x'`` and ``'xb'`` modes.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.5
|
||||||
|
Added support for writing arbitrary
|
||||||
|
:term:`bytes-like objects <bytes-like object>`.
|
||||||
|
|
||||||
|
|
||||||
.. function:: compress(data, compresslevel=9)
|
.. function:: compress(data, compresslevel=9)
|
||||||
|
|
||||||
|
|
19
Lib/gzip.py
19
Lib/gzip.py
|
@ -334,17 +334,20 @@ def write(self,data):
|
||||||
if self.fileobj is None:
|
if self.fileobj is None:
|
||||||
raise ValueError("write() on closed GzipFile object")
|
raise ValueError("write() on closed GzipFile object")
|
||||||
|
|
||||||
# Convert data type if called by io.BufferedWriter.
|
if isinstance(data, bytes):
|
||||||
if isinstance(data, memoryview):
|
length = len(data)
|
||||||
data = data.tobytes()
|
else:
|
||||||
|
# accept any data that supports the buffer protocol
|
||||||
|
data = memoryview(data)
|
||||||
|
length = data.nbytes
|
||||||
|
|
||||||
if len(data) > 0:
|
if length > 0:
|
||||||
self.size = self.size + len(data)
|
self.fileobj.write(self.compress.compress(data))
|
||||||
|
self.size += length
|
||||||
self.crc = zlib.crc32(data, self.crc) & 0xffffffff
|
self.crc = zlib.crc32(data, self.crc) & 0xffffffff
|
||||||
self.fileobj.write( self.compress.compress(data) )
|
self.offset += length
|
||||||
self.offset += len(data)
|
|
||||||
|
|
||||||
return len(data)
|
return length
|
||||||
|
|
||||||
def read(self, size=-1):
|
def read(self, size=-1):
|
||||||
self._check_closed()
|
self._check_closed()
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
import os
|
import os
|
||||||
import io
|
import io
|
||||||
import struct
|
import struct
|
||||||
|
import array
|
||||||
gzip = support.import_module('gzip')
|
gzip = support.import_module('gzip')
|
||||||
|
|
||||||
data1 = b""" int length=DEFAULTALLOC, err = Z_OK;
|
data1 = b""" int length=DEFAULTALLOC, err = Z_OK;
|
||||||
|
@ -43,6 +44,14 @@ def tearDown(self):
|
||||||
|
|
||||||
|
|
||||||
class TestGzip(BaseTest):
|
class TestGzip(BaseTest):
|
||||||
|
def write_and_read_back(self, data, mode='b'):
|
||||||
|
b_data = bytes(data)
|
||||||
|
with gzip.GzipFile(self.filename, 'w'+mode) as f:
|
||||||
|
l = f.write(data)
|
||||||
|
self.assertEqual(l, len(b_data))
|
||||||
|
with gzip.GzipFile(self.filename, 'r'+mode) as f:
|
||||||
|
self.assertEqual(f.read(), b_data)
|
||||||
|
|
||||||
def test_write(self):
|
def test_write(self):
|
||||||
with gzip.GzipFile(self.filename, 'wb') as f:
|
with gzip.GzipFile(self.filename, 'wb') as f:
|
||||||
f.write(data1 * 50)
|
f.write(data1 * 50)
|
||||||
|
@ -57,6 +66,34 @@ def test_write(self):
|
||||||
# Test multiple close() calls.
|
# Test multiple close() calls.
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
# The following test_write_xy methods test that write accepts
|
||||||
|
# the corresponding bytes-like object type as input
|
||||||
|
# and that the data written equals bytes(xy) in all cases.
|
||||||
|
def test_write_memoryview(self):
|
||||||
|
self.write_and_read_back(memoryview(data1 * 50))
|
||||||
|
m = memoryview(bytes(range(256)))
|
||||||
|
data = m.cast('B', shape=[8,8,4])
|
||||||
|
self.write_and_read_back(data)
|
||||||
|
|
||||||
|
def test_write_bytearray(self):
|
||||||
|
self.write_and_read_back(bytearray(data1 * 50))
|
||||||
|
|
||||||
|
def test_write_array(self):
|
||||||
|
self.write_and_read_back(array.array('I', data1 * 40))
|
||||||
|
|
||||||
|
def test_write_incompatible_type(self):
|
||||||
|
# Test that non-bytes-like types raise TypeError.
|
||||||
|
# Issue #21560: attempts to write incompatible types
|
||||||
|
# should not affect the state of the fileobject
|
||||||
|
with gzip.GzipFile(self.filename, 'wb') as f:
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
f.write('')
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
f.write([])
|
||||||
|
f.write(data1)
|
||||||
|
with gzip.GzipFile(self.filename, 'rb') as f:
|
||||||
|
self.assertEqual(f.read(), data1)
|
||||||
|
|
||||||
def test_read(self):
|
def test_read(self):
|
||||||
self.test_write()
|
self.test_write()
|
||||||
# Try reading.
|
# Try reading.
|
||||||
|
|
|
@ -23,6 +23,10 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #23688: Added support of arbitrary bytes-like objects and avoided
|
||||||
|
unnecessary copying of memoryview in gzip.GzipFile.write().
|
||||||
|
Original patch by Wolfgang Maier.
|
||||||
|
|
||||||
- Issue #23252: Added support for writing ZIP files to unseekable streams.
|
- Issue #23252: Added support for writing ZIP files to unseekable streams.
|
||||||
|
|
||||||
- Issue #21526: Tkinter now supports new boolean type in Tcl 8.5.
|
- Issue #21526: Tkinter now supports new boolean type in Tcl 8.5.
|
||||||
|
|
Loading…
Reference in New Issue