mirror of
https://github.com/python/cpython.git
synced 2025-08-27 12:16:04 +00:00
bpo-44439: _ZipWriteFile.write() handle buffer protocol correctly (GH-29468)
Co-authored-by: Marco Ribeiro <marcoffee@users.noreply.github.com>
This commit is contained in:
parent
591f6754b5
commit
36dd7396fc
3 changed files with 19 additions and 1 deletions
|
@ -1147,8 +1147,15 @@ class _ZipWriteFile(io.BufferedIOBase):
|
|||
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)
|
||||
nbytes = data.nbytes
|
||||
self._file_size += nbytes
|
||||
|
||||
self._crc = crc32(data, self._crc)
|
||||
if self._compressor:
|
||||
data = self._compressor.compress(data)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue