-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-44439: _ZipWriteFile.write() handle buffer protocol correctly #29468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
f53ae11
d103eb0
d6d5548
6ba267a
bdb8487
451e24f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import array | ||
import contextlib | ||
import importlib.util | ||
import io | ||
|
@@ -1718,6 +1719,14 @@ def test_non_existent_file_raises_OSError(self): | |
# quickly. | ||
self.assertRaises(OSError, zipfile.ZipFile, TESTFN) | ||
|
||
def test_issue44439(self): | ||
q = array.array('Q', [1, 2, 3, 4, 5]) | ||
LENGTH = len(q) * q.itemsize | ||
with zipfile.ZipFile(io.BytesIO(), 'w') as zip: | ||
with zip.open('data', 'w') as data: | ||
self.assertEqual(data.write(q), LENGTH) | ||
self.assertEqual(data._file_size, LENGTH) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
def test_empty_file_raises_BadZipFile(self): | ||
f = open(TESTFN, 'w', encoding='utf-8') | ||
f.close() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1147,8 +1147,15 @@ def writable(self): | |
def write(self, data): | ||
if self.closed: | ||
raise ValueError('I/O operation on closed file.') | ||
nbytes = len(data) | ||
|
||
# Accept any data that supports the buffer protocol | ||
if isinstance(data, (bytes, bytearray)): | ||
nbytes = len(data) | ||
else: | ||
data = memoryview(data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's recommended to manually release a # Accept any data that supports the buffer protocol
with memoryview(data) as data:
nbytes = data.nbytes
self._file_size += nbytes
self._crc = crc32(data, self._crc)
if self._compressor:
data = self._compressor.compress(data)
self._compress_size += len(data)
self._fileobj.write(data)
return nbytes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. Let @serhiy-storchaka decide. update: I ran a benchmark, the performances are no significant different. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eryksun is right. But it may be simpler to merge the current solution and fix issues with releasing memoryviews in all places uniformly later. |
||
nbytes = data.nbytes | ||
self._file_size += nbytes | ||
|
||
self._crc = crc32(data, self._crc) | ||
if self._compressor: | ||
data = self._compressor.compress(data) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix in `_ZipWriteFile.write()` method, when the input data is an object that | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
supports the buffer protocol, the file length may be wrong. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to move this test to AbstractWriterTests and test with different compressions.