From daebed063cddf2b164d809280144b6f1cf81c9ea Mon Sep 17 00:00:00 2001 From: Riccardo Mori Date: Mon, 14 Oct 2024 18:04:55 +0200 Subject: [PATCH] Fix conversion from int to EnumType to be py3.10+ compatible --- src/qbindiff/loader/types.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/qbindiff/loader/types.py b/src/qbindiff/loader/types.py index c545f37..a39133e 100644 --- a/src/qbindiff/loader/types.py +++ b/src/qbindiff/loader/types.py @@ -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