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:
Ma Lin 2022-03-08 17:33:56 +08:00 committed by GitHub
parent 591f6754b5
commit 36dd7396fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 1 deletions

View file

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