Skip to content

Commit

Permalink
Add invalid file warnings for entry loading
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Apr 27, 2024
1 parent 9d1c572 commit 8d87f57
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions tivars/var.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,17 +821,25 @@ def open(cls, filename: str) -> 'TIEntry':
UserWarning)

with open(filename, 'rb') as file:
file.seek(55)
# Use header for sanity check
header = TIHeader()
header.load_from_file(file)
file.seek(2, 1)

entry = cls()
entry.load_bytes(file.read(cls.next_entry_length(file)))

file.seek(2, 1)

if file.read():
warn("The selected var file contains multiple entries; only the first will be loaded. "
"Use load_from_file to select a particular entry, or load the entire file into a TIVar object.",
UserWarning)
if remaining := file.read():
if remaining.startswith(b"\x0B\x00") or remaining.startswith(b"\x0D\x00"):
warn("The selected var file contains multiple entries; only the first will be loaded. "
"Use load_from_file to select a particular entry, or load the entire file into a TIVar object.",
UserWarning)

else:
warn(f"The selected var file contains unexpected additional data: {remaining}.",
BytesWarning)

return entry

Expand Down

0 comments on commit 8d87f57

Please sign in to comment.