Skip to content

Commit 0616530

Browse files
committed
Allow read_from_binary to stop on error
Setting the stop_on_error parameter will let it stop and gracefully return on a decode error.
1 parent 9a25b50 commit 0616530

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

riscvmodel/code.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ def decode(machinecode: int):
2626
raise MachineDecodeError()
2727

2828

29-
def read_from_binary(fname: str):
29+
def read_from_binary(fname: str, *, stoponerror: bool = False):
3030
with open(fname, "rb") as f:
3131
insn = f.read(4)
3232
while insn:
33-
yield decode(int.from_bytes(insn, 'little'))
33+
try:
34+
yield decode(int.from_bytes(insn, 'little'))
35+
except MachineDecodeError as e:
36+
if stoponerror:
37+
return
38+
raise(e)
3439
insn = f.read(4)
3540

3641
def machinsn_decode():

0 commit comments

Comments
 (0)