[issue23688] unnecessary copying of memoryview in gzip.GzipFile.write?

Serhiy Storchaka report at bugs.python.org
Mon Mar 23 15:37:02 CET 2015


Serhiy Storchaka added the comment:

Here is a patch that restores support on non-contiguous memoryviews.

It would be better to drop support of non-contiguous data, because it worked 
only by accident. Needed support of only bytes-like memoryviews written by 
BufferedWriter.

----------
Added file: http://bugs.python.org/file38656/gzip_write_noncontiguous.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23688>
_______________________________________
-------------- next part --------------
diff -r 7e179ee91af0 Lib/gzip.py
--- a/Lib/gzip.py	Mon Mar 23 15:26:49 2015 +0200
+++ b/Lib/gzip.py	Mon Mar 23 16:27:08 2015 +0200
@@ -340,6 +340,8 @@ class GzipFile(io.BufferedIOBase):
             # accept any data that supports the buffer protocol
             data = memoryview(data)
             length = data.nbytes
+            if not data.contiguous:
+                data = bytes(data)
 
         if length > 0:
             self.fileobj.write(self.compress.compress(data))
diff -r 7e179ee91af0 Lib/test/test_gzip.py
--- a/Lib/test/test_gzip.py	Mon Mar 23 15:26:49 2015 +0200
+++ b/Lib/test/test_gzip.py	Mon Mar 23 16:27:08 2015 +0200
@@ -74,6 +74,7 @@ class TestGzip(BaseTest):
         m = memoryview(bytes(range(256)))
         data = m.cast('B', shape=[8,8,4])
         self.write_and_read_back(data)
+        self.write_and_read_back(memoryview(data1 * 50)[::-1])
 
     def test_write_bytearray(self):
         self.write_and_read_back(bytearray(data1 * 50))


More information about the Python-bugs-list mailing list