Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/pyfdb/pyfdb_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ def read(self, len: int = -1) -> bytes:
Parameters
----------
`len`: int
The amount of bytes to read. If -1 is entered the whole data handle is read.
Number of bytes to read. Defaults to -1, which reads the entire buffer.
If the requested size exceeds the available data, the result is zero-padded to match the requested size.

Returns
-------
Expand All @@ -272,10 +273,16 @@ def read(self, len: int = -1) -> bytes:
"DataHandle: Read occured before the handle was opened. Must be opened first."
)

if len == -1:
len = self.dataHandle.size()
# NB: Some data handles do not implement the size() method. In this case, we just read the requested amount of bytes.
try:
dh_size = self.dataHandle.size()
array_size = dh_size if len == -1 else min(len, dh_size)
except RuntimeError:
if len == -1:
raise
array_size = len

buffer = bytearray(min(len, self.dataHandle.size()))
buffer = bytearray(array_size)
read_bytes = self.dataHandle.read(buffer)

if read_bytes == 0:
Expand Down
Loading