Skip to content

Commit

Permalink
read_bytes(): check if the request is satisfiable, change error msg
Browse files Browse the repository at this point in the history
Resolve #61

The changed error message reflects more the meaning of the error and
less the specific implementation. All we can tell now is how many bytes
were requested and how many are actually _available_: we haven't
necessarily "gotten" any bytes yet, so it should not say "but got only
%d bytes".
  • Loading branch information
generalmimon committed Apr 8, 2022
1 parent ba2b030 commit 349a861
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions kaitaistruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,21 @@ def read_bytes(self, n):
"requested invalid %d amount of bytes" %
(n,)
)
r = self._io.read(n)
if len(r) < n:

is_satisfiable = True
if self._io.seekable():
num_bytes_available = self.size() - self.pos()
is_satisfiable = (n <= num_bytes_available)

if is_satisfiable:
r = self._io.read(n)
num_bytes_available = len(r)
is_satisfiable = (n <= num_bytes_available)

if not is_satisfiable:
raise EOFError(
"requested %d bytes, but got only %d bytes" %
(n, len(r))
"requested %d bytes, but only %d bytes available" %
(n, num_bytes_available)
)
return r

Expand Down

0 comments on commit 349a861

Please sign in to comment.