Skip to content

add support for recursive structs #36

@denballakh

Description

@denballakh

My usecase boils down to this:

s = c.Struct(
    'type' / u32,
    'value' / c.Switch(c.this.type, {
        1: u32,
        2: f32,
        3: u32, # will be replaced later
    })
)
s.value.cases[3] = s # make it recursive

This works perfectly, but your project enters infinite recursion and crashes:

Traceback (most recent call last):
  File "D:\GitHub\Space Rangers\ranger-tools\rangers\scr.py", line 328, in <module>
    editor_panel = WxConstructHexEditor(frame, construct=s, binary=b'')
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Programs\Python\3.12\Lib\site-packages\construct_editor\wx_widgets\wx_construct_hex_editor.py", line 89, in __init__
    self._init_gui_construct_editor(hsizer, construct)
  File "D:\Programs\Python\3.12\Lib\site-packages\construct_editor\wx_widgets\wx_construct_hex_editor.py", line 121, in _init_gui_construct_editor
    self.construct_editor: WxConstructEditor = WxConstructEditor(
                                               ^^^^^^^^^^^^^^^^^^
  File "D:\Programs\Python\3.12\Lib\site-packages\construct_editor\wx_widgets\wx_construct_editor.py", line 288, in __init__
    ConstructEditor.__init__(self, construct, self._model)
  File "D:\Programs\Python\3.12\Lib\site-packages\construct_editor\core\construct_editor.py", line 17, in __init__
    self.change_construct(construct)
  File "D:\Programs\Python\3.12\Lib\site-packages\construct_editor\core\construct_editor.py", line 129, in change_construct
    self._construct = include_metadata(constr)
                      ^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Programs\Python\3.12\Lib\site-packages\construct_editor\core\preprocessor.py", line 261, in include_metadata
    constr.subcon = include_metadata(constr.subcon, bitwise)  # type: ignore
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Programs\Python\3.12\Lib\site-packages\construct_editor\core\preprocessor.py", line 210, in include_metadata
    new_subcons.append(include_metadata(subcon, bitwise))
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Programs\Python\3.12\Lib\site-packages\construct_editor\core\preprocessor.py", line 261, in include_metadata
    constr.subcon = include_metadata(constr.subcon, bitwise)  # type: ignore
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Programs\Python\3.12\Lib\site-packages\construct_editor\core\preprocessor.py", line 246, in include_metadata
    new_cases[key] = include_metadata(subcon, bitwise)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions