Skip to content

Python: to_i on a enum may fail, if a enum is not parsed as known #815

Closed

Description

@KOLANICH

Activity

  1. generalmimon commented on Feb 21, 2023

    @generalmimon
    Member

    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:

    PythonTranslator.scala:87-88

      override def enumToInt(v: Ast.expr, et: EnumType): String =
        s"${translate(v)}.value"

    When the KS-generated code parses an enum field, it first reads the integer value, which goes through the doEnumById translator method:

    PythonTranslator.scala:60-61

      override def doEnumById(enumTypeAbs: List[String], id: String): String =
        s"${PythonCompiler.kstreamName}.resolve_enum(${PythonCompiler.types2class(enumTypeAbs)}, $id)"

    So in case of Python, the resolve_enum runtime method is used:

    kaitaistruct.py:436-445

        @staticmethod
        def resolve_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:
                return enum_obj(value)
            except ValueError:
                return value

    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'
    
    class EnumForUnknownId(ReadWriteKaitaiStruct):
        # ...
        def _read(self):
            self.one = KaitaiStream.resolve_enum(EnumForUnknownId.Animal, self._io.read_u1())
    
        def _write__seq(self, io=None):
            # ...
            self._io.write_u1(self.one.value)
  2. KOLANICH commented on Feb 21, 2023

    @KOLANICH
    Author

    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).

  3. added a commit that references this issue on Oct 9, 2023
  4. added this to the v0.11 milestone on Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions