Skip to content

Commit

Permalink
Fix conversion from int to EnumType to be py3.10+ compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
patacca committed Oct 14, 2024
1 parent 99c2257 commit daebed0
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/qbindiff/loader/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,23 @@ class InstructionGroup(IntEnum):
def fromint(cls, value: int):
"""Cast an integer to InstructionGroup type"""
# Return an invalid group if cast is not possible
return (
InstructionGroup(value) if value in InstructionGroup else InstructionGroup.GRP_INVALID
)
try:
return InstructionGroup(value)
except ValueError:
return InstructionGroup.GRP_INVALID

@classmethod
def from_capstone(cls, capstone_group: int):
"""Cast a capstone group to InstructionGroup type"""
# Wrap capstone group using our custom type
# Note: This only works because the mappings between the enums are the same
if capstone_group in InstructionGroup:
try:
return InstructionGroup(capstone_group)

# Raise an exception if cast is not possible
raise ValueError(
f"Misalignment between capstone group {capstone_group} and InstructionGroup"
)
except ValueError:
# Raise an exception if cast is not possible
raise ValueError(
f"Misalignment between capstone group {capstone_group} and InstructionGroup"
)


@enum_tools.documentation.document_enum
Expand Down

0 comments on commit daebed0

Please sign in to comment.