Fix returning too much data when grabbing a chunk that overlaps a patched section and the end of a file.
This commit is contained in:
parent
88e9d0406e
commit
1db7de771e
|
@ -286,7 +286,7 @@ class FileBytes:
|
||||||
|
|
||||||
# Determine start of slice
|
# Determine start of slice
|
||||||
if key.start is None:
|
if key.start is None:
|
||||||
start = 0 if step > 0 else self.__patchlength
|
start = 0 if step > 0 else (self.__patchlength - 1)
|
||||||
elif key.start < 0:
|
elif key.start < 0:
|
||||||
start = self.__patchlength + key.start
|
start = self.__patchlength + key.start
|
||||||
else:
|
else:
|
||||||
|
@ -407,7 +407,7 @@ class FileBytes:
|
||||||
|
|
||||||
# Append any amount of data we need to read past the end of the file.
|
# Append any amount of data we need to read past the end of the file.
|
||||||
if len(data) < stop - start:
|
if len(data) < stop - start:
|
||||||
data.extend([0] * (stop - len(data)))
|
data.extend([0] * ((stop - start) - len(data)))
|
||||||
else:
|
else:
|
||||||
data = [0] * (stop - start)
|
data = [0] * (stop - start)
|
||||||
|
|
||||||
|
@ -418,23 +418,25 @@ class FileBytes:
|
||||||
|
|
||||||
return bytes(data)
|
return bytes(data)
|
||||||
elif start > stop and step == -1:
|
elif start > stop and step == -1:
|
||||||
|
start += 1
|
||||||
|
stop += 1
|
||||||
if not modifications:
|
if not modifications:
|
||||||
# This is just a continguous read, reversed
|
# This is just a continguous read, reversed
|
||||||
self.__handle.seek(stop + 1)
|
self.__handle.seek(stop)
|
||||||
return self.__handle.read(start - stop)[::-1]
|
return self.__handle.read(start - stop)[::-1]
|
||||||
else:
|
else:
|
||||||
if (stop + 1) < self.__filelength:
|
if stop < self.__filelength:
|
||||||
self.__handle.seek(stop + 1)
|
self.__handle.seek(stop)
|
||||||
data = [x for x in self.__handle.read(start - stop)]
|
data = [x for x in self.__handle.read(start - stop)]
|
||||||
|
|
||||||
# Append any amount of data we need to read past the end of the file.
|
# Append any amount of data we need to read past the end of the file.
|
||||||
if len(data) < start - stop:
|
if len(data) < start - stop:
|
||||||
data.extend([0] * (start - len(data)))
|
data.extend([0] * ((start - stop) - len(data)))
|
||||||
else:
|
else:
|
||||||
data = [0] * (start - stop)
|
data = [0] * (start - stop)
|
||||||
|
|
||||||
# Now we have to modify the data with our own overlay.
|
# Now we have to modify the data with our own overlay.
|
||||||
for index, off in enumerate(range(stop + 1, start + 1)):
|
for index, off in enumerate(range(stop, start)):
|
||||||
if off in self.__patches:
|
if off in self.__patches:
|
||||||
data[index] = self.__patches[off]
|
data[index] = self.__patches[off]
|
||||||
|
|
||||||
|
|
|
@ -298,6 +298,18 @@ class TestFileBytes(unittest.TestCase):
|
||||||
b,
|
b,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_read_after_append(self) -> None:
|
||||||
|
fb = FileBytes(io.BytesIO(b"0123456789"))
|
||||||
|
|
||||||
|
# Do some simple appending.
|
||||||
|
fb.append(b"abcdef")
|
||||||
|
|
||||||
|
# Now try to read over the boundary.
|
||||||
|
self.assertEqual(
|
||||||
|
fb[5:15],
|
||||||
|
b"56789abcde",
|
||||||
|
)
|
||||||
|
|
||||||
def test_modify_variants(self) -> None:
|
def test_modify_variants(self) -> None:
|
||||||
fb = FileBytes(io.BytesIO(b"0123456789"))
|
fb = FileBytes(io.BytesIO(b"0123456789"))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue