Skip to content

__bytes__ fails on Python 3.13 when backing memory is bytearray #6

@renato-osec

Description

@renato-osec

MemoryResolver.resolve() returns bytearray slices, which is ultimately what array_impl.to_bytes() uses. Python 3.13 enforces that __bytes__ must return bytes, not bytearray, so this error turns up

  File "libdestruct/common/obj.py", line 136, in __bytes__
    return self.to_bytes()
  File "libdestruct/common/array/array_impl.py", line 63, in to_bytes
    return b"".join(bytes(x) for x in self)
TypeError: __bytes__ returned non-bytes (type bytearray)

Minimal script derived from what I was trying to do:

from libdestruct import inflater
from libdestruct.c.struct_parser import definition_to_type

MyStruct = definition_to_type("""
    struct Foo {
        char name[8];
    };
""")

lib = inflater(bytearray(b"AAAAAAAA"))
provola = lib.inflate(MyStruct, 0)
bytes(provola)

Fix: memory_resolver.py:48:

-        return self.memory[address : address + size]
+        return bytes(self.memory[address : address + size])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions