Skip to content

Commit 2ca63aa

Browse files
committed
Update linting
1 parent e433934 commit 2ca63aa

1 file changed

Lines changed: 23 additions & 24 deletions

File tree

dissect/util/stream.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ def seekable(self) -> bool:
5656
return True
5757

5858
def seek(self, pos: int, whence: int = io.SEEK_SET) -> int:
59-
"""Seek the stream to the specified position."""
59+
"""Seek the stream to the specified position.
60+
61+
Returns:
62+
The new stream position after seeking.
63+
"""
6064
with self._read_lock:
6165
pos = self._seek(pos, whence)
6266
self._set_pos(pos)
@@ -95,7 +99,7 @@ def tell(self) -> int:
9599

96100
def _fill_buf(self) -> None:
97101
"""Fill the alignment buffer if we can."""
98-
if self._buf_size or self.size is not None and (self.size <= self._pos or self.size <= self._pos_align):
102+
if self._buf_size or (self.size is not None and (self.size <= self._pos or self.size <= self._pos_align)):
99103
# Don't fill the buffer if:
100104
# - We already have a buffer
101105
# - The stream position is at the end (or beyond) the stream size
@@ -104,9 +108,10 @@ def _fill_buf(self) -> None:
104108
self._buf_size = self._readinto(self._pos_align, self._buf)
105109

106110
def readinto(self, b: bytearray) -> int:
107-
"""Read bytes into a pre-allocated bytes-like object b.
111+
"""Read bytes into a pre-allocated bytes-like object ``b``.
108112
109-
Returns an int representing the number of bytes read (0 for EOF).
113+
Returns:
114+
The number of bytes read (0 for EOF).
110115
"""
111116
with self._read_lock:
112117
return self._readinto_unlocked(b)
@@ -125,13 +130,10 @@ def _readinto_unlocked(self, b: bytearray) -> int:
125130
if size is not None:
126131
remaining = size - self._pos
127132

128-
if n == -1:
129-
n = remaining
130-
else:
131-
n = min(n, remaining)
133+
n = remaining if n == -1 else min(n, remaining)
132134

133135
# Short path for when it turns out we don't need to read anything
134-
if n == 0 or size is not None and size <= self._pos:
136+
if n == 0 or (size is not None and size <= self._pos):
135137
return 0
136138

137139
# Read misaligned start from buffer
@@ -182,8 +184,9 @@ def _read(self, offset: int, length: int) -> bytes:
182184
def _readinto(self, offset: int, buf: memoryview) -> int:
183185
"""Provide an aligned ``readinto`` implementation for this stream.
184186
185-
For backwards compatibility, ``AlignedStream`` provides a default ``_readinto`` implementation, implemented in `_readinto_fallback`, that
186-
falls back on ``_read``. However, subclasses should override the ``_readinto`` method instead of ``_readinto_fallback``.
187+
For backwards compatibility, ``AlignedStream`` provides a default ``_readinto`` implementation, implemented
188+
in ``_readinto_fallback``, that falls back on ``_read``. However, subclasses should override the ``_readinto``
189+
method instead of ``_readinto_fallback``.
187190
"""
188191
return self._readinto_fallback(offset, buf)
189192

@@ -244,7 +247,6 @@ def peek(self, n: int = 0) -> bytes:
244247

245248
def close(self) -> None:
246249
"""Close the stream. Does nothing by default."""
247-
pass
248250

249251

250252
class RangeStream(AlignedStream):
@@ -547,19 +549,16 @@ def __init__(self, fh: BinaryIO, size: int | None = None, align: int = STREAM_BU
547549
self._lookup: list[int] = []
548550
self._has_readinto = hasattr(self._fh, "readinto")
549551

550-
def add(self, offset: int, data: bytes | BinaryIO, size: int | None = None) -> None:
552+
def add(self, offset: int, data: bytes, size: int | None = None) -> None:
551553
"""Add an overlay at the given offset.
552554
553555
Args:
554556
offset: The offset in bytes to add an overlay at.
555-
data: The bytes or file-like object to overlay
557+
data: The data to overlay.
556558
size: Optional size specification of the overlay, if it can't be inferred.
557559
"""
558-
if not hasattr(data, "read"):
559-
size = size or len(data)
560-
data = io.BytesIO(data)
561-
elif size is None:
562-
size = data.size if hasattr(data, "size") else data.seek(0, io.SEEK_END)
560+
size = size or len(data)
561+
data = memoryview(data)
563562

564563
if not size:
565564
return None
@@ -607,8 +606,9 @@ def _readinto(self, offset: int, buf: memoryview) -> int:
607606
prev_overlay_remaining = prev_overlay_size - offset_in_prev_overlay
608607
prev_overlay_read_size = min(length, prev_overlay_remaining)
609608

610-
prev_overlay_data.seek(offset_in_prev_overlay)
611-
buf[:prev_overlay_read_size] = prev_overlay_data.read(prev_overlay_read_size)
609+
buf[:prev_overlay_read_size] = prev_overlay_data[
610+
offset_in_prev_overlay : offset_in_prev_overlay + prev_overlay_read_size
611+
]
612612
n += prev_overlay_read_size
613613

614614
offset += prev_overlay_read_size
@@ -634,13 +634,12 @@ def _readinto(self, offset: int, buf: memoryview) -> int:
634634

635635
# read remaining from overlay
636636
next_overlay_read_size = min(next_overlay_size, length - gap_to_next_overlay)
637-
next_overlay_data.seek(0)
638-
buf[:next_overlay_read_size] = next_overlay_data.read(next_overlay_read_size)
637+
buf[:next_overlay_read_size] = next_overlay_data[:next_overlay_read_size]
639638
n += next_overlay_read_size
639+
buf = buf[next_overlay_read_size:]
640640

641641
offset += next_overlay_read_size + gap_to_next_overlay
642642
length -= next_overlay_read_size + gap_to_next_overlay
643-
buf = buf[next_overlay_read_size + gap_to_next_overlay :]
644643
else:
645644
# Next overlay is too far away, complete read
646645
fh.seek(offset)

0 commit comments

Comments
 (0)