Skip to content

Commit 9e72d96

Browse files
committed
Fix tests
1 parent 9bf45e6 commit 9e72d96

6 files changed

Lines changed: 46 additions & 13 deletions

File tree

dissect/cstruct/cstruct.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def _next_anonymous(self) -> str:
220220
return name
221221

222222
def _add_attr(self, name: str, value: Any, replace: bool = False) -> None:
223-
if not replace and (name in self.__dict__ and self.__dict__[name] != value):
223+
if not replace and ((existing := self.__dict__.get(name)) is not None and existing != value):
224224
raise ValueError(f"Attribute already exists: {name}")
225225
setattr(self, name, value)
226226

dissect/cstruct/tools/stubgen.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ def generate_cstruct_stub(cs: cstruct, module_prefix: str = "", cls_name: str =
9191
stub = f"{name}: TypeAlias = {type_.__name__}"
9292
elif issubclass(type_, (types.Enum, types.Flag)):
9393
stub = generate_enum_stub(type_, cs_prefix=cs_prefix, module_prefix=module_prefix)
94-
elif issubclass(typedef, types.Pointer):
95-
typehint = generate_typehint(typedef, prefix=cs_prefix, module_prefix=module_prefix)
94+
elif issubclass(type_, types.Pointer):
95+
typehint = generate_typehint(type_, prefix=cs_prefix, module_prefix=module_prefix)
9696
stub = f"{name}: TypeAlias = {typehint}"
9797
elif issubclass(type_, types.Structure):
9898
stub = generate_structure_stub(type_, cs_prefix=cs_prefix, module_prefix=module_prefix)
@@ -167,9 +167,10 @@ def generate_structure_stub(
167167
module_prefix: str = "",
168168
) -> str:
169169
result = [f"class {name_prefix}{structure.__name__}({module_prefix}{structure.__base__.__name__}):"]
170-
171170
indent = " " * 4
172171

172+
all_types = structure.cs.typedefs | structure.cs.types
173+
173174
args = ["self"]
174175
for field_name, field in structure.fields.items():
175176
type_name = field.type.__name__
@@ -180,7 +181,7 @@ def generate_structure_stub(
180181
while issubclass(nested_type, types.BaseArray):
181182
nested_type = nested_type.type
182183

183-
if issubclass(nested_type, types.Structure) and type_name not in structure.cs.typedefs:
184+
if issubclass(nested_type, types.Structure) and type_name not in all_types:
184185
inlined = True
185186
inline_stub = generate_structure_stub(nested_type, cs_prefix=cs_prefix, module_prefix=module_prefix)
186187

tests/test_annotations.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
from dissect.cstruct.cstruct import cstruct
99
from dissect.cstruct.types.base import BaseType
1010

11+
CS = cstruct()
12+
1113

1214
@pytest.mark.parametrize(
1315
"name",
14-
[name for name in cstruct().typedefs if " " not in name],
16+
[name for name in CS.types | CS.typedefs if " " not in name],
1517
)
1618
def test_cstruct_type_annotation(name: str, monkeypatch: pytest.MonkeyPatch) -> None:
19+
"""Verify that all default types defined in cstruct have type annotations."""
1720
with (
1821
patch("typing.TYPE_CHECKING", True),
1922
patch("dissect.cstruct.types.base.MetaType.__getitem__", lambda self, item: self),

tests/test_basic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ def test_load_init() -> None:
4949
"""
5050
# load with first positional argument
5151
cs = cstruct(cdef)
52-
assert "test" in cs.typedefs
52+
assert "test" in cs.types
5353
assert cs.endian == "<"
5454

5555
# load from keyword argument and big endian
5656
cs = cstruct(load=cdef, endian=">")
57-
assert "test" in cs.typedefs
57+
assert "test" in cs.types
5858
a = cs.test(a=0xBADC0DE, b=0xACCE55ED)
5959
assert len(bytes(a)) == 12
6060
assert bytes(a) == a.dumps()
6161
assert bytes(a) == b"\x0b\xad\xc0\xde\x00\x00\x00\x00\xac\xce\x55\xed"
6262

6363
# load using positional argument and little endian
6464
cs = cstruct(cdef, endian="<")
65-
assert "test" in cs.typedefs
65+
assert "test" in cs.types
6666
a = cs.test(a=0xBADC0DE, b=0xACCE55ED)
6767
assert len(bytes(a)) == 12
6868
assert bytes(a) == a.dumps()
@@ -81,7 +81,7 @@ def test_load_init_kwargs_only() -> None:
8181
cs = cstruct(cdef, ">")
8282

8383
cs = cstruct(cdef, endian=">")
84-
assert "test" in cs.typedefs
84+
assert "test" in cs.types
8585
assert cs.endian == ">"
8686

8787

tests/test_parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def test_conditional_ifdef(cs: cstruct) -> None:
188188
"""
189189
cs.load(cdef)
190190

191-
assert "test" in cs.typedefs
191+
assert "test" in cs.types
192192

193193

194194
def test_conditional_ifndef(cs: cstruct) -> None:
@@ -218,7 +218,7 @@ def test_conditional_ifndef_guard(cs: cstruct) -> None:
218218
cs.load(cdef)
219219

220220
assert "__MYGUARD" in cs.consts
221-
assert "myStruct" in cs.typedefs
221+
assert "myStruct" in cs.types
222222

223223

224224
def test_conditional_nested() -> None:
@@ -265,7 +265,7 @@ def test_conditional_in_struct(cs: cstruct) -> None:
265265
"""
266266
cs.load(cdef)
267267

268-
assert "t_bitfield" in cs.typedefs
268+
assert "t_bitfield" in cs.types
269269
assert "fval" in cs.t_bitfield.fields
270270
assert "bit0" in cs.t_bitfield.fields["fval"].type.fields
271271
assert "bit1" in cs.t_bitfield.fields["fval"].type.fields

tests/test_tools_stubgen.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,35 @@ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
339339
""",
340340
id="pointer alias",
341341
),
342+
pytest.param(
343+
"""
344+
struct A {
345+
uint8 a;
346+
};
347+
348+
struct Test {
349+
A x;
350+
};
351+
""",
352+
"""
353+
class cstruct(cstruct):
354+
class A(Structure):
355+
a: cstruct.uint8
356+
@overload
357+
def __init__(self, a: cstruct.uint8 | None = ...): ...
358+
@overload
359+
def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
360+
361+
class Test(Structure):
362+
x: cstruct.A
363+
@overload
364+
def __init__(self, x: cstruct.A | None = ...): ...
365+
@overload
366+
def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
367+
368+
""",
369+
id="reference other struct",
370+
),
342371
],
343372
)
344373
def test_generate_cstruct_stub(cs: cstruct, cdef: str, expected: str) -> None:

0 commit comments

Comments
 (0)