Skip to content

Commit 1aaa4c2

Browse files
committed
Added resolve_enum, akin to Ruby implementation
1 parent c31ca97 commit 1aaa4c2

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

kaitaistruct.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,18 @@ def process_rotate_left(data, amount, group_size):
363363
for i in range(len(r)):
364364
r[i] = (r[i] << amount) & 0xff | (r[i] >> anti_amount)
365365
return bytes(r)
366+
367+
# ========================================================================
368+
# Misc
369+
# ========================================================================
370+
371+
@staticmethod
372+
def resolve_enum(enum_obj, value):
373+
"""Resolves value using enum: if the value is not found in the map,
374+
we'll just use literal value per se. Works around problem with Python
375+
enums throwing an exception when encountering unknown value.
376+
"""
377+
try:
378+
return enum_obj(value)
379+
except ValueError:
380+
return value

0 commit comments

Comments
 (0)