Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conversion from int to EnumType to be py3.10+ compatible #66

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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