Skip to content

Commit

Permalink
Fail a bit more gracefully when data is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Jul 15, 2024
1 parent b401685 commit c78a689
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions tivars/var.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ def load_bytes(self, data: bytes | BytesIO):
if hasattr(data, "read"):
data = BytesIO(data.read())

else:
data = BytesIO(data)
data = BytesIO(data.ljust(len(self), b'\x00'))

# Read magic
self.raw.magic = data.read(8)
Expand Down Expand Up @@ -713,10 +712,9 @@ def load_bytes(self, data: bytes | BytesIO):
"""

if hasattr(data, "read"):
data = BytesIO(data.read())
data = data.read()

else:
data = BytesIO(data)
data = BytesIO(data.ljust(TIEntry.flash_meta_length + 4, b'\x00'))

# Read meta length
self.raw.meta_length = data.read(2)
Expand Down Expand Up @@ -1251,13 +1249,19 @@ def save(self, filename: str = None):
:param filename: A filename to save to (defaults to the var's name and extension)
"""

if not filename:
filename = self.filename

elif "." not in filename:
filename += f".{self.extension}"

if self._model:
for index, entry in enumerate(self.entries):
if entry.get_min_os() > self._model.OS("latest"):
warn(f"Entry #{index + 1} is not supported by {self._model}.",
UserWarning)

with open(filename or self.filename, 'wb+') as file:
with open(filename, 'wb+') as file:
file.write(self.bytes())


Expand Down

0 comments on commit c78a689

Please sign in to comment.