You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python: to_i on a enum may fail, if a enum is not parsed as known #815
Also, IntEnums can be interacted to numbers without explicit casting at all - they inherit int. Should we allow such for all the targets and determine if casting is needed automatically and apply it implicitly?
I've just hit this issue when porting serialization support to Python. If an enum value cannot be converted to an integer, it cannot be serialized. And the current enumToInt implementation only works for known values from the enum definition:
@staticmethoddefresolve_enum(enum_obj, value):
"""Resolves value using enum: if the value is not found in the map, we'll just use literal value per se. Works around problem with Python enums throwing an exception when encountering unknown value. """try:
returnenum_obj(value)
exceptValueError:
returnvalue
As a result, the value of an "enum attribute" in KS terms can in Python be represented either as an object instance of the enum class, or a plain integer (int). But integers don't have any value property:
ERROR: test_read_write_roundtrip (specwrite.test_enum_for_unknown_id.TestEnumForUnknownId)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\temp\kaitai_struct\tests\spec\python\specwrite\common_spec.py", line 26, in test_read_write_roundtrip
orig_ks._write(new_io)
File "C:\temp\kaitai_struct\runtime\python\kaitaistruct.py", line 67, in _write
self._write__seq(io)
File "C:\temp\kaitai_struct\tests\compiled\python\testwrite\enum_for_unknown_id.py", line 32, in _write__seq
self._io.write_u1(self.one.value)
AttributeError: 'int' object has no attribute 'value'
IntEnum doesn't require using .value. It is as god as an int. Also there is an IntFlag, which can be used, if an enum has a certain structure (I mean athe compiler can try to detect this, but IMHO such int fields should be converted into structs of bit-sized ints where it makes sense, so maybe also emit warnings with the replacement KS code).
Activity
generalmimon commentedon Feb 21, 2023
I've just hit this issue when porting serialization support to Python. If an enum value cannot be converted to an integer, it cannot be serialized. And the current
enumToIntimplementation only works for known values from the enum definition:PythonTranslator.scala:87-88When the KS-generated code parses an enum field, it first reads the integer value, which goes through the
doEnumByIdtranslator method:PythonTranslator.scala:60-61So in case of Python, the
resolve_enumruntime method is used:kaitaistruct.py:436-445As a result, the value of an "enum attribute" in KS terms can in Python be represented either as an object instance of the enum class, or a plain integer (
int). But integers don't have anyvalueproperty:KOLANICH commentedon Feb 21, 2023
IntEnumdoesn't require using.value. It is as god as anint. Also there is anIntFlag, which can be used, if an enum has a certain structure (I mean athe compiler can try to detect this, but IMHO such int fields should be converted into structs of bit-sized ints where it makes sense, so maybe also emit warnings with the replacement KS code).EnumToI: also test the expression `{enum_value}.to_i.to_s`
Add EnumToIInvalid for the `to_i` method on invalid enum values
Pull tests (to_i on enums), compiler (#815) and runtimes
Python: make enums extend `IntEnum`, fix `to_i` on invalid enum vals