Skip to content

Commit 7da4075

Browse files
author
Josh Watson
committed
Improve exception handling
Changed all Exception objects raised to more specific classes implemented in pyevmasm. Additionally, disassemble_all now catches a ParseError exception that could be thrown by Instruction.parse_operand, which would cause the generator to raise an exception instead of returning None.
1 parent 117a110 commit 7da4075

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

pyevmasm/evmasm.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class UnknownMnemonicError(Exception):
3535
class UnknownOpcodeError(Exception):
3636
pass
3737

38+
class AssembleError(Exception):
39+
pass
40+
41+
class ParseError(Exception):
42+
pass
43+
3844

3945
class InstructionTable(dict):
4046
"""
@@ -365,7 +371,7 @@ def parse_operand(self, buf):
365371
operand |= next(buf)
366372
self._operand = operand
367373
except StopIteration:
368-
raise Exception("Not enough data for decoding")
374+
raise ParseError("Not enough data for decoding")
369375

370376
@property
371377
def operand_size(self):
@@ -557,8 +563,8 @@ def assemble_one(asmcode, pc=0):
557563
assert len(asmcode) == 2
558564
instr.operand = int(asmcode[1], 0)
559565
return instr
560-
except BaseException:
561-
raise Exception("Something wrong at pc %d" % pc)
566+
except:
567+
raise AssembleError("Something wrong at pc %d" % pc)
562568

563569

564570
def assemble_all(asmcode, pc=0):
@@ -627,10 +633,13 @@ def disassemble_one(bytecode, pc=0):
627633
instruction = instruction_table[opcode]
628634
instruction.pc = pc
629635

630-
if instruction.has_operand:
631-
instruction.parse_operand(bytecode)
632-
633-
return instruction
636+
try:
637+
if instruction.has_operand:
638+
instruction.parse_operand(bytecode)
639+
except ParseError:
640+
instruction = None
641+
finally:
642+
return instruction
634643

635644

636645
def disassemble_all(bytecode, pc=0):

0 commit comments

Comments
 (0)