Skip to content

Commit ed1daf2

Browse files
authored
Rename default() to __default__() (#108)
1 parent b5ce35d commit ed1daf2

19 files changed

+99
-82
lines changed

dissect/cstruct/types/base.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __len__(cls) -> int:
5959

6060
return cls.size
6161

62-
def default(cls) -> BaseType:
62+
def __default__(cls) -> BaseType:
6363
"""Return the default value of this type."""
6464
return cls()
6565

@@ -179,7 +179,7 @@ def _write_0(cls, stream: BinaryIO, array: list[BaseType]) -> int:
179179
stream: The stream to read from.
180180
array: The array to write.
181181
"""
182-
return cls._write_array(stream, array + [cls.default()])
182+
return cls._write_array(stream, array + [cls.__default__()])
183183

184184

185185
class _overload:
@@ -225,8 +225,10 @@ class ArrayMetaType(MetaType):
225225
num_entries: int | Expression | None
226226
null_terminated: bool
227227

228-
def default(cls) -> BaseType:
229-
return type.__call__(cls, [cls.type.default()] * (cls.num_entries if isinstance(cls.num_entries, int) else 0))
228+
def __default__(cls) -> BaseType:
229+
return type.__call__(
230+
cls, [cls.type.__default__()] * (cls.num_entries if isinstance(cls.num_entries, int) else 0)
231+
)
230232

231233
def _read(cls, stream: BinaryIO, context: dict[str, Any] | None = None) -> Array:
232234
if cls.null_terminated:

dissect/cstruct/types/char.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def _write(cls, stream: BinaryIO, data: bytes) -> int:
2525
return stream.write(data)
2626

2727
@classmethod
28-
def default(cls) -> CharArray:
28+
def __default__(cls) -> CharArray:
2929
return type.__call__(cls, b"\x00" * (0 if cls.dynamic or cls.null_terminated else cls.num_entries))
3030

3131

@@ -75,5 +75,5 @@ def _write(cls, stream: BinaryIO, data: bytes | int | str) -> int:
7575
return stream.write(data)
7676

7777
@classmethod
78-
def default(cls) -> Char:
78+
def __default__(cls) -> Char:
7979
return type.__call__(cls, b"\x00")

dissect/cstruct/types/enum.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __call__(
2727
) -> EnumMetaType:
2828
if name is None:
2929
if value is None:
30-
value = cls.type.default()
30+
value = cls.type.__default__()
3131

3232
if not isinstance(value, int):
3333
# value is a parsable value
@@ -82,7 +82,7 @@ def _write_array(cls, stream: BinaryIO, array: list[Enum]) -> int:
8282

8383
def _write_0(cls, stream: BinaryIO, array: list[BaseType]) -> int:
8484
data = [entry.value if isinstance(entry, Enum) else entry for entry in array]
85-
return cls._write_array(stream, data + [cls.type.default()])
85+
return cls._write_array(stream, data + [cls.type.__default__()])
8686

8787

8888
def _fix_alias_members(cls: type[Enum]) -> None:

dissect/cstruct/types/pointer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ def __or__(self, other: int) -> Pointer:
6666
return type.__call__(self.__class__, int.__or__(self, other), self._stream, self._context)
6767

6868
@classmethod
69-
def default(cls) -> Pointer:
70-
return cls.__new__(cls, cls.cs.pointer.default(), None, None)
69+
def __default__(cls) -> Pointer:
70+
return cls.__new__(cls, cls.cs.pointer.__default__(), None, None)
7171

7272
@classmethod
7373
def _read(cls, stream: BinaryIO, context: dict[str, Any] | None = None) -> Pointer:

dissect/cstruct/types/structure.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def _write(cls, stream: BinaryIO, data: Structure) -> int:
325325

326326
value = getattr(data, field.name, None)
327327
if value is None:
328-
value = field_type.default()
328+
value = field_type.__default__()
329329

330330
if field.bits:
331331
if isinstance(field_type, EnumMetaType):
@@ -560,7 +560,7 @@ def _rebuild(self, attr: str) -> None:
560560
buf.seek(field.offset)
561561

562562
if (value := getattr(self, attr)) is None:
563-
value = field.type.default()
563+
value = field.type.__default__()
564564

565565
field.type._write(buf, value)
566566

@@ -774,7 +774,7 @@ def _generate_structure__init__(fields: list[Field]) -> FunctionType:
774774
template: FunctionType = _make_structure__init__(len(field_names))
775775
return type(template)(
776776
template.__code__.replace(
777-
co_consts=(None, *[field.type.default() for field in fields]),
777+
co_consts=(None, *[field.type.__default__() for field in fields]),
778778
co_names=(*field_names,),
779779
co_varnames=("self", *field_names),
780780
),
@@ -794,7 +794,7 @@ def _generate_union__init__(fields: list[Field]) -> FunctionType:
794794
template: FunctionType = _make_union__init__(len(field_names))
795795
return type(template)(
796796
template.__code__.replace(
797-
co_consts=(None, *sum([(field.name, field.type.default()) for field in fields], ())),
797+
co_consts=(None, *sum([(field.name, field.type.__default__()) for field in fields], ())),
798798
co_varnames=("self", *field_names),
799799
),
800800
template.__globals__,

dissect/cstruct/types/wchar.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def _write(cls, stream: BinaryIO, data: str) -> int:
2020
return stream.write(data.encode(Wchar.__encoding_map__[cls.cs.endian]))
2121

2222
@classmethod
23-
def default(cls) -> WcharArray:
23+
def __default__(cls) -> WcharArray:
2424
return type.__call__(cls, "\x00" * (0 if cls.dynamic or cls.null_terminated else cls.num_entries))
2525

2626

@@ -75,5 +75,5 @@ def _write(cls, stream: BinaryIO, data: str) -> int:
7575
return stream.write(data.encode(cls.__encoding_map__[cls.cs.endian]))
7676

7777
@classmethod
78-
def default(cls) -> Wchar:
78+
def __default__(cls) -> Wchar:
7979
return type.__call__(cls, "\x00")

tests/test_types_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def _read(cls, stream: BinaryIO, context: dict | None = None) -> CustomType:
9898

9999
class ArrayType(BaseType, metaclass=ArrayMetaType):
100100
@classmethod
101-
def default(cls) -> CustomType:
101+
def __default__(cls) -> CustomType:
102102
return cls.type()
103103

104104
@classmethod

tests/test_types_char.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ def test_char_eof(cs: cstruct) -> None:
4646

4747

4848
def test_char_default(cs: cstruct) -> None:
49-
assert cs.char.default() == b"\x00"
50-
assert cs.char[4].default() == b"\x00\x00\x00\x00"
51-
assert cs.char[None].default() == b""
49+
assert cs.char.__default__() == b"\x00"
50+
assert cs.char[4].__default__() == b"\x00\x00\x00\x00"
51+
assert cs.char[None].__default__() == b""

tests/test_types_custom.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class EtwPointer(BaseType):
1313
size: int | None
1414

1515
@classmethod
16-
def default(cls) -> int:
17-
return cls.cs.uint64.default()
16+
def __default__(cls) -> int:
17+
return cls.cs.uint64.__default__()
1818

1919
@classmethod
2020
def _read(cls, stream: BinaryIO, context: dict[str, Any] | None = None) -> BaseType:
@@ -82,10 +82,10 @@ def test_custom_default(cs: cstruct) -> None:
8282
cs.add_custom_type("EtwPointer", EtwPointer)
8383

8484
cs.EtwPointer.as_64bit()
85-
assert cs.EtwPointer.default() == 0
85+
assert cs.EtwPointer.__default__() == 0
8686

8787
cs.EtwPointer.as_32bit()
88-
assert cs.EtwPointer.default() == 0
88+
assert cs.EtwPointer.__default__() == 0
8989

90-
assert cs.EtwPointer[1].default() == [0]
91-
assert cs.EtwPointer[None].default() == []
90+
assert cs.EtwPointer[1].__default__() == [0]
91+
assert cs.EtwPointer[None].__default__() == []

tests/test_types_enum.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,21 @@ def test_enum_default(cs: cstruct) -> None:
418418
"""
419419
cs.load(cdef)
420420

421-
assert cs.test.default() == cs.test.A == cs.test(0)
422-
assert cs.test[1].default() == [cs.test.A]
423-
assert cs.test[None].default() == []
421+
assert cs.test.__default__() == cs.test.A == cs.test(0)
422+
assert cs.test[1].__default__() == [cs.test.A]
423+
assert cs.test[None].__default__() == []
424+
425+
426+
def test_enum_default_default(cs: cstruct) -> None:
427+
cdef = """
428+
enum test {
429+
default = 0,
430+
};
431+
432+
struct test2 {
433+
test a;
434+
};
435+
"""
436+
cs.load(cdef)
437+
438+
assert cs.test.__default__() == cs.test.default == cs.test(0)

tests/test_types_flag.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,6 @@ def test_flag_default(cs: cstruct) -> None:
266266
"""
267267
cs.load(cdef)
268268

269-
assert cs.test.default() == cs.test(0)
270-
assert cs.test[1].default() == [cs.test(0)]
271-
assert cs.test[None].default() == []
269+
assert cs.test.__default__() == cs.test(0)
270+
assert cs.test[1].__default__() == [cs.test(0)]
271+
assert cs.test[None].__default__() == []

tests/test_types_int.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,10 @@ def test_int_struct_unsigned_be(cs: cstruct, compiled: bool) -> None:
403403

404404

405405
def test_int_default(cs: cstruct) -> None:
406-
assert cs.int24.default() == 0
407-
assert cs.uint24.default() == 0
408-
assert cs.int128.default() == 0
409-
assert cs.uint128.default() == 0
406+
assert cs.int24.__default__() == 0
407+
assert cs.uint24.__default__() == 0
408+
assert cs.int128.__default__() == 0
409+
assert cs.uint128.__default__() == 0
410410

411-
assert cs.int24[1].default() == [0]
412-
assert cs.int24[None].default() == []
411+
assert cs.int24[1].__default__() == [0]
412+
assert cs.int24[None].__default__() == []

tests/test_types_leb128.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ def test_leb128_unsigned_write_amount_written(cs: cstruct) -> None:
192192

193193

194194
def test_leb128_default(cs: cstruct) -> None:
195-
assert cs.uleb128.default() == 0
196-
assert cs.ileb128.default() == 0
195+
assert cs.uleb128.__default__() == 0
196+
assert cs.ileb128.__default__() == 0
197197

198-
assert cs.uleb128[1].default() == [0]
199-
assert cs.uleb128[None].default() == []
198+
assert cs.uleb128[1].__default__() == [0]
199+
assert cs.uleb128[None].__default__() == []

tests/test_types_packed.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,17 @@ def test_packed_float_struct_be(cs: cstruct, compiled: bool) -> None:
172172

173173

174174
def test_packed_default(cs: cstruct) -> None:
175-
assert cs.int8.default() == 0
176-
assert cs.uint8.default() == 0
177-
assert cs.int16.default() == 0
178-
assert cs.uint16.default() == 0
179-
assert cs.int32.default() == 0
180-
assert cs.uint32.default() == 0
181-
assert cs.int64.default() == 0
182-
assert cs.uint64.default() == 0
183-
assert cs.float16.default() == 0.0
184-
assert cs.float.default() == 0.0
185-
assert cs.double.default() == 0.0
186-
187-
assert cs.int8[2].default() == [0, 0]
188-
assert cs.int8[None].default() == []
175+
assert cs.int8.__default__() == 0
176+
assert cs.uint8.__default__() == 0
177+
assert cs.int16.__default__() == 0
178+
assert cs.uint16.__default__() == 0
179+
assert cs.int32.__default__() == 0
180+
assert cs.uint32.__default__() == 0
181+
assert cs.int64.__default__() == 0
182+
assert cs.uint64.__default__() == 0
183+
assert cs.float16.__default__() == 0.0
184+
assert cs.float.__default__() == 0.0
185+
assert cs.double.__default__() == 0.0
186+
187+
assert cs.int8[2].__default__() == [0, 0]
188+
assert cs.int8[None].__default__() == []

tests/test_types_pointer.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ def test_pointer_default(cs: cstruct) -> None:
242242
cs.pointer = cs.uint8
243243

244244
ptr = cs._make_pointer(cs.uint8)
245-
assert isinstance(ptr.default(), Pointer)
246-
assert ptr.default() == 0
247-
assert ptr[1].default() == [0]
248-
assert ptr[None].default() == []
245+
assert isinstance(ptr.__default__(), Pointer)
246+
assert ptr.__default__() == 0
247+
assert ptr[1].__default__() == [0]
248+
assert ptr[None].__default__() == []
249249

250250
with pytest.raises(NullPointerDereference):
251-
ptr.default().dereference()
251+
ptr.__default__().dereference()

tests/test_types_structure.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,9 @@ def test_structure_default(cs: cstruct, compiled: bool) -> None:
599599

600600
assert verify_compiled(cs.test, compiled)
601601

602-
assert cs.test() == cs.test.default()
602+
assert cs.test() == cs.test.__default__()
603603

604-
obj = cs.test.default()
604+
obj = cs.test.__default__()
605605
assert obj.t_int == 0
606606
assert obj.t_int_array == [0, 0]
607607
assert obj.t_bytesint == 0
@@ -625,11 +625,11 @@ def test_structure_default(cs: cstruct, compiled: bool) -> None:
625625
for name in obj.fields.keys():
626626
assert isinstance(getattr(obj, name), BaseType)
627627

628-
assert cs.test_nested() == cs.test_nested.default()
628+
assert cs.test_nested() == cs.test_nested.__default__()
629629

630-
obj = cs.test_nested.default()
631-
assert obj.t_struct == cs.test.default()
632-
assert obj.t_struct_array == [cs.test.default(), cs.test.default()]
630+
obj = cs.test_nested.__default__()
631+
assert obj.t_struct == cs.test.__default__()
632+
assert obj.t_struct_array == [cs.test.__default__(), cs.test.__default__()]
633633

634634
assert obj.dumps() == b"\x00" * 171
635635

@@ -678,7 +678,7 @@ def test_structure_default_dynamic(cs: cstruct, compiled: bool) -> None:
678678

679679
assert verify_compiled(cs.test, compiled)
680680

681-
assert cs.test() == cs.test.default()
681+
assert cs.test() == cs.test.__default__()
682682

683683
obj = cs.test()
684684
assert obj.t_int_array_n == obj.t_int_array_d == []
@@ -694,9 +694,9 @@ def test_structure_default_dynamic(cs: cstruct, compiled: bool) -> None:
694694
for name in obj.fields.keys():
695695
assert isinstance(getattr(obj, name), BaseType)
696696

697-
assert cs.test_nested() == cs.test_nested.default()
697+
assert cs.test_nested() == cs.test_nested.__default__()
698698

699-
obj = cs.test_nested.default()
699+
obj = cs.test_nested.__default__()
700700
assert obj.t_struct_n == obj.t_struct_array_d == []
701701

702702
assert obj.dumps() == b"\x00" * 21

tests/test_types_union.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def test_union_default(cs: cstruct) -> None:
414414
"""
415415
cs.load(cdef)
416416

417-
assert cs.test() == cs.test.default()
417+
assert cs.test() == cs.test.__default__()
418418

419419
obj = cs.test()
420420
assert obj.a == 0
@@ -425,11 +425,11 @@ def test_union_default(cs: cstruct) -> None:
425425
for name in obj.fields.keys():
426426
assert isinstance(getattr(obj, name), BaseType)
427427

428-
assert cs.test_nested() == cs.test_nested.default()
428+
assert cs.test_nested() == cs.test_nested.__default__()
429429

430-
obj = cs.test_nested.default()
431-
assert obj.t_union == cs.test.default()
432-
assert obj.t_union_array == [cs.test.default(), cs.test.default()]
430+
obj = cs.test_nested.__default__()
431+
assert obj.t_union == cs.test.__default__()
432+
assert obj.t_union_array == [cs.test.__default__(), cs.test.__default__()]
433433

434434
assert obj.dumps() == b"\x00" * 24
435435

tests/test_types_void.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ def test_void_array_write(cs: cstruct) -> None:
3333

3434

3535
def test_void_default(cs: cstruct) -> None:
36-
assert cs.void() == cs.void.default()
36+
assert cs.void() == cs.void.__default__()
3737
assert not cs.void()
38-
assert not cs.void.default()
38+
assert not cs.void.__default__()
3939

40-
assert cs.void[1].default() == [cs.void()]
41-
assert cs.void[None].default() == []
40+
assert cs.void[1].__default__() == [cs.void()]
41+
assert cs.void[None].__default__() == []
4242

4343

4444
def test_void_struct(cs: cstruct, compiled: bool) -> None:

tests/test_types_wchar.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ def test_wchar_eof(cs: cstruct) -> None:
7878

7979

8080
def test_wchar_default(cs: cstruct) -> None:
81-
assert cs.wchar.default() == "\x00"
82-
assert cs.wchar[4].default() == "\x00\x00\x00\x00"
83-
assert cs.wchar[None].default() == ""
81+
assert cs.wchar.__default__() == "\x00"
82+
assert cs.wchar[4].__default__() == "\x00\x00\x00\x00"
83+
assert cs.wchar[None].__default__() == ""

0 commit comments

Comments
 (0)