Skip to content

Commit ec5d9a6

Browse files
committed
fixed a bug with cs.BytesInteger (eg. cs.Int24ub)
1 parent f3a3179 commit ec5d9a6

2 files changed

Lines changed: 50 additions & 17 deletions

File tree

construct_editor/core/preprocessor.py

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import copy
3+
import enum
34
import io
45
import typing as t
56

@@ -16,6 +17,30 @@ class GuiMetaData(t.TypedDict):
1617
child_gui_metadata: t.Optional["GuiMetaData"]
1718

1819

20+
class IntWithGuiMetadata(int):
21+
pass
22+
23+
24+
class FloatWithGuiMetadata(float):
25+
pass
26+
27+
28+
class BytesWithGuiMetadata(bytes):
29+
pass
30+
31+
32+
class BytearrayWithGuiMetadata(bytearray):
33+
pass
34+
35+
36+
class StrWithGuiMetadata(str):
37+
pass
38+
39+
40+
class NoneWithGuiMetadata:
41+
pass
42+
43+
1944
class ObjProxyWithGuiMetaData(wrapt.ObjectProxy):
2045
__slots__ = "__construct_editor_metadata__"
2146

@@ -26,10 +51,6 @@ def __init__(self, wrapped: t.Any, gui_metadata: GuiMetaData):
2651
)
2752

2853

29-
class BytesWithGuiMetadata(bytes):
30-
pass
31-
32-
3354
def get_gui_metadata(obj: t.Any) -> t.Optional[GuiMetaData]:
3455
"""Get the GUI metadata if they are available"""
3556
try:
@@ -40,22 +61,34 @@ def get_gui_metadata(obj: t.Any) -> t.Optional[GuiMetaData]:
4061

4162
def add_gui_metadata(obj: t.Any, gui_metadata: GuiMetaData) -> t.Any:
4263
"""
43-
Append gui_metadata to an object.
44-
45-
With immutable types like str, bytes, int, ... it is not possible to add
46-
metadata to a type dynammically. With other types like enums it is possible,
47-
but enums are singleton, so that the metadata of objects is overwritten by
48-
the next object with the same value.
49-
50-
Because auf this for any object an proxy object is created, so that the
51-
metadata can be added to this proxy object.
64+
Append the private field "__construct_editor_metadata__" to an object
5265
"""
53-
if isinstance(obj, bytes):
54-
# bytes has to be a subtype of bytes or the `stream_write` will raise an error...
66+
obj_type = type(obj)
67+
if isinstance(obj, enum.Enum):
68+
obj = ObjProxyWithGuiMetaData(obj, gui_metadata)
69+
elif (obj_type is int) or (obj_type is bool):
70+
obj = IntWithGuiMetadata(obj)
71+
obj.__construct_editor_metadata__ = gui_metadata
72+
elif obj_type is float:
73+
obj = FloatWithGuiMetadata(obj)
74+
obj.__construct_editor_metadata__ = gui_metadata
75+
elif obj_type is bytes:
5576
obj = BytesWithGuiMetadata(obj)
5677
obj.__construct_editor_metadata__ = gui_metadata
78+
elif obj_type is bytearray:
79+
obj = BytearrayWithGuiMetadata(obj)
80+
obj.__construct_editor_metadata__ = gui_metadata
81+
elif obj_type is str:
82+
obj = StrWithGuiMetadata(obj)
83+
obj.__construct_editor_metadata__ = gui_metadata
84+
elif obj is None:
85+
obj = NoneWithGuiMetadata()
86+
obj.__construct_editor_metadata__ = gui_metadata
5787
else:
58-
obj = ObjProxyWithGuiMetaData(obj, gui_metadata)
88+
try:
89+
obj.__construct_editor_metadata__ = gui_metadata # type: ignore
90+
except AttributeError:
91+
raise ValueError(f"add_gui_metadata dont work with type of {type(obj)}")
5992
return obj
6093

6194

construct_editor/gallery/test_dataclass_struct.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class DataclassStructTest(cst.DataclassMixin):
1414
@dataclasses.dataclass
1515
class Nested(cst.DataclassMixin):
1616
nested_width: int = cst.csfield(cs.Int16sb)
17-
nested_height: int = cst.csfield(cs.Int16sb)
17+
nested_height: int = cst.csfield(cs.Int24ub)
1818
nested_bytes: bytes = cst.csfield(cs.Bytes(2))
1919
nested_array: t.List[int] = cst.csfield(cs.Array(2, cs.Int8sb))
2020

0 commit comments

Comments
 (0)