Merge pull request #4274 from ThomasWaldmann/memoryview-cm-1.1

correctly release memoryview, see #4243
This commit is contained in:
TW 2019-01-29 19:14:45 +01:00 committed by GitHub
commit 24d9421b97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 13 deletions

View File

@ -1363,19 +1363,24 @@ class LoggedIO:
with open(backup_filename, 'rb') as backup_fd: with open(backup_filename, 'rb') as backup_fd:
# note: file must not be 0 size (windows can't create 0 size mapping) # note: file must not be 0 size (windows can't create 0 size mapping)
with mmap.mmap(backup_fd.fileno(), 0, access=mmap.ACCESS_READ) as mm: with mmap.mmap(backup_fd.fileno(), 0, access=mmap.ACCESS_READ) as mm:
# memoryview context manager is problematic, see https://bugs.python.org/issue35686
data = memoryview(mm) data = memoryview(mm)
d = data
try:
with open(filename, 'wb') as fd: with open(filename, 'wb') as fd:
fd.write(MAGIC) fd.write(MAGIC)
while len(data) >= self.header_fmt.size: while len(d) >= self.header_fmt.size:
crc, size, tag = self.header_fmt.unpack(data[:self.header_fmt.size]) crc, size, tag = self.header_fmt.unpack(d[:self.header_fmt.size])
if size < self.header_fmt.size or size > len(data): if size < self.header_fmt.size or size > len(d):
data = data[1:] d = d[1:]
continue continue
if crc32(data[4:size]) & 0xffffffff != crc: if crc32(d[4:size]) & 0xffffffff != crc:
data = data[1:] d = d[1:]
continue continue
fd.write(data[:size]) fd.write(d[:size])
data = data[size:] d = d[size:]
finally:
del d
data.release() data.release()
def read(self, segment, offset, id, read_data=True): def read(self, segment, offset, id, read_data=True):