Skip to content

Commit e6d8085

Browse files
committed
Fix conversion from int to EnumType to be py3.10+ compatible
1 parent 99c2257 commit e6d8085

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/qbindiff/loader/types.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,23 @@ class InstructionGroup(IntEnum):
130130
def fromint(cls, value: int):
131131
"""Cast an integer to InstructionGroup type"""
132132
# Return an invalid group if cast is not possible
133-
return (
134-
InstructionGroup(value) if value in InstructionGroup else InstructionGroup.GRP_INVALID
135-
)
133+
try:
134+
return InstructionGroup(value)
135+
except ValueError:
136+
return InstructionGroup.GRP_INVALID
136137

137138
@classmethod
138139
def from_capstone(cls, capstone_group: int):
139140
"""Cast a capstone group to InstructionGroup type"""
140141
# Wrap capstone group using our custom type
141142
# Note: This only works because the mappings between the enums are the same
142-
if capstone_group in InstructionGroup:
143+
try:
143144
return InstructionGroup(capstone_group)
144-
145-
# Raise an exception if cast is not possible
146-
raise ValueError(
147-
f"Misalignment between capstone group {capstone_group} and InstructionGroup"
148-
)
145+
except ValueError:
146+
# Raise an exception if cast is not possible
147+
raise ValueError(
148+
f"Misalignment between capstone group {capstone_group} and InstructionGroup"
149+
)
149150

150151

151152
@enum_tools.documentation.document_enum

0 commit comments

Comments
 (0)