Repository: fox-it/dissect.cstruct

Issue:
Having an enum or flag with a value `default` breaks
```python
cdef = """
enum test {
    default = 0,
};

struct test2 {
    test a;
};
"""
cs.load(cdef)
```

Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/fox-it/dissect.cstruct/pull/108?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fox-it) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 92.46%. Comparing base [(`b5ce35d`)](https://app.codecov.io/gh/fox-it/dissect.cstruct/commit/b5ce35dae42a8f11eee7dd8723f0be08107527db?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fox-it) to head [(`fde09e4`)](https://app.codecov.io/gh/fox-it/dissect.cstruct/commit/fde09e4486d4d7d388877c612c6a4d17ef126849?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fox-it).

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main     #108   +/-   ##
=======================================
  Coverage   92.46%   92.46%           
=======================================
  Files          20       20           
  Lines        2176     2176           
=======================================
  Hits         2012     2012           
  Misses        164      164           
```

| [Flag](https://app.codecov.io/gh/fox-it/dissect.cstruct/pull/108/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fox-it) | Coverage Δ | |
|---|---|---|
| [unittests](https://app.codecov.io/gh/fox-it/dissect.cstruct/pull/108/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fox-it) | `92.46% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fox-it#carryforward-flags-in-the-pull-request-comment) to find out more.

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/fox-it/dissect.cstruct/pull/108?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fox-it).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=fox-it).


Patch:
diff --git a/dissect/cstruct/types/base.py b/dissect/cstruct/types/base.py
index 53c57b8..4ef40b9 100644
--- a/dissect/cstruct/types/base.py
+++ b/dissect/cstruct/types/base.py
@@ -59,7 +59,7 @@ class MetaType(type):
 
         return cls.size
 
-    def default(cls) -> BaseType:
+    def __default__(cls) -> BaseType:
         """Return the default value of this type."""
         return cls()
 
@@ -179,7 +179,7 @@ class MetaType(type):
             stream: The stream to read from.
             array: The array to write.
         """
-        return cls._write_array(stream, array + [cls.default()])
+        return cls._write_array(stream, array + [cls.__default__()])
 
 
 class _overload:
@@ -225,8 +225,10 @@ class ArrayMetaType(MetaType):
     num_entries: int | Expression | None
     null_terminated: bool
 
-    def default(cls) -> BaseType:
-        return type.__call__(cls, [cls.type.default()] * (cls.num_entries if isinstance(cls.num_entries, int) else 0))
+    def __default__(cls) -> BaseType:
+        return type.__call__(
+            cls, [cls.type.__default__()] * (cls.num_entries if isinstance(cls.num_entries, int) else 0)
+        )
 
     def _read(cls, stream: BinaryIO, context: dict[str, Any] | None = None) -> Array:
         if cls.null_terminated:
diff --git a/dissect/cstruct/types/char.py b/dissect/cstruct/types/char.py
index 268d690..117deb6 100644
--- a/dissect/cstruct/types/char.py
+++ b/dissect/cstruct/types/char.py
@@ -25,7 +25,7 @@ class CharArray(bytes, BaseType, metaclass=ArrayMetaType):
         return stream.write(data)
 
     @classmethod
-    def default(cls) -> CharArray:
+    def __default__(cls) -> CharArray:
         return type.__call__(cls, b"\x00" * (0 if cls.dynamic or cls.null_terminated else cls.num_entries))
 
 
@@ -75,5 +75,5 @@ class Char(bytes, BaseType):
         return stream.write(data)
 
     @classmethod
-    def default(cls) -> Char:
+    def __default__(cls) -> Char:
         return type.__call__(cls, b"\x00")
diff --git a/dissect/cstruct/types/enum.py b/dissect/cstruct/types/enum.py
index eea2b7a..a282584 100644
--- a/dissect/cstruct/types/enum.py
+++ b/dissect/cstruct/types/enum.py
@@ -27,7 +27,7 @@ class EnumMetaType(EnumMeta, MetaType):
     ) -> EnumMetaType:
         if name is None:
             if value is None:
-                value = cls.type.default()
+                value = cls.type.__default__()
 
             if not isinstance(value, int):
                 # value is a parsable value
@@ -82,7 +82,7 @@ class EnumMetaType(EnumMeta, MetaType):
 
     def _write_0(cls, stream: BinaryIO, array: list[BaseType]) -> int:
         data = [entry.value if isinstance(entry, Enum) else entry for entry in array]
-        return cls._write_array(stream, data + [cls.type.default()])
+        return cls._write_array(stream, data + [cls.type.__default__()])
 
 
 def _fix_alias_members(cls: type[Enum]) -> None:
diff --git a/dissect/cstruct/types/pointer.py b/dissect/cstruct/types/pointer.py
index e398ff6..bb85286 100644
--- a/dissect/cstruct/types/pointer.py
+++ b/dissect/cstruct/types/pointer.py
@@ -66,8 +66,8 @@ class Pointer(int, BaseType):
         return type.__call__(self.__class__, int.__or__(self, other), self._stream, self._context)
 
     @classmethod
-    def default(cls) -> Pointer:
-        return cls.__new__(cls, cls.cs.pointer.default(), None, None)
+    def __default__(cls) -> Pointer:
+        return cls.__new__(cls, cls.cs.pointer.__default__(), None, None)
 
     @classmethod
     def _read(cls, stream: BinaryIO, context: dict[str, Any] | None = None) -> Pointer:
diff --git a/dissect/cstruct/types/structure.py b/dissect/cstruct/types/structure.py
index 6dbc5cf..5aae944 100644
--- a/dissect/cstruct/types/structure.py
+++ b/dissect/cstruct/types/structure.py
@@ -325,7 +325,7 @@ class StructureMetaType(MetaType):
 
             value = getattr(data, field.name, None)
             if value is None:
-                value = field_type.default()
+                value = field_type.__default__()
 
             if field.bits:
                 if isinstance(field_type, EnumMetaType):
@@ -560,7 +560,7 @@ class Union(Structure, metaclass=UnionMetaType):
             buf.seek(field.offset)
 
         if (value := getattr(self, attr)) is None:
-            value = field.type.default()
+            value = field.type.__default__()
 
         field.type._write(buf, value)
 
@@ -774,7 +774,7 @@ def _generate_structure__init__(fields: list[Field]) -> FunctionType:
     template: FunctionType = _make_structure__init__(len(field_names))
     return type(template)(
         template.__code__.replace(
-            co_consts=(None, *[field.type.default() for field in fields]),
+            co_consts=(None, *[field.type.__default__() for field in fields]),
             co_names=(*field_names,),
             co_varnames=("self", *field_names),
         ),
@@ -794,7 +794,7 @@ def _generate_union__init__(fields: list[Field]) -> FunctionType:
     template: FunctionType = _make_union__init__(len(field_names))
     return type(template)(
         template.__code__.replace(
-            co_consts=(None, *sum([(field.name, field.type.default()) for field in fields], ())),
+            co_consts=(None, *sum([(field.name, field.type.__default__()) for field in fields], ())),
             co_varnames=("self", *field_names),
         ),
         template.__globals__,
diff --git a/dissect/cstruct/types/wchar.py b/dissect/cstruct/types/wchar.py
index ecb98dc..1a95eb0 100644
--- a/dissect/cstruct/types/wchar.py
+++ b/dissect/cstruct/types/wchar.py
@@ -20,7 +20,7 @@ class WcharArray(str, BaseType, metaclass=ArrayMetaType):
         return stream.write(data.encode(Wchar.__encoding_map__[cls.cs.endian]))
 
     @classmethod
-    def default(cls) -> WcharArray:
+    def __default__(cls) -> WcharArray:
         return type.__call__(cls, "\x00" * (0 if cls.dynamic or cls.null_terminated else cls.num_entries))
 
 
@@ -75,5 +75,5 @@ class Wchar(str, BaseType):
         return stream.write(data.encode(cls.__encoding_map__[cls.cs.endian]))
 
     @classmethod
-    def default(cls) -> Wchar:
+    def __default__(cls) -> Wchar:
         return type.__call__(cls, "\x00")


Test patch:
diff --git a/tests/test_types_base.py b/tests/test_types_base.py
index 8ef8708..93d4250 100644
--- a/tests/test_types_base.py
+++ b/tests/test_types_base.py
@@ -98,7 +98,7 @@ def test_custom_array_type(cs: cstruct, compiled: bool) -> None:
 
         class ArrayType(BaseType, metaclass=ArrayMetaType):
             @classmethod
-            def default(cls) -> CustomType:
+            def __default__(cls) -> CustomType:
                 return cls.type()
 
             @classmethod
diff --git a/tests/test_types_char.py b/tests/test_types_char.py
index 71b0775..ea2aec9 100644
--- a/tests/test_types_char.py
+++ b/tests/test_types_char.py
@@ -46,6 +46,6 @@ def test_char_eof(cs: cstruct) -> None:
 
 
 def test_char_default(cs: cstruct) -> None:
-    assert cs.char.default() == b"\x00"
-    assert cs.char[4].default() == b"\x00\x00\x00\x00"
-    assert cs.char[None].default() == b""
+    assert cs.char.__default__() == b"\x00"
+    assert cs.char[4].__default__() == b"\x00\x00\x00\x00"
+    assert cs.char[None].__default__() == b""
diff --git a/tests/test_types_custom.py b/tests/test_types_custom.py
index 4cf624c..e6ee542 100644
--- a/tests/test_types_custom.py
+++ b/tests/test_types_custom.py
@@ -13,8 +13,8 @@ class EtwPointer(BaseType):
     size: int | None
 
     @classmethod
-    def default(cls) -> int:
-        return cls.cs.uint64.default()
+    def __default__(cls) -> int:
+        return cls.cs.uint64.__default__()
 
     @classmethod
     def _read(cls, stream: BinaryIO, context: dict[str, Any] | None = None) -> BaseType:
@@ -82,10 +82,10 @@ def test_custom_default(cs: cstruct) -> None:
     cs.add_custom_type("EtwPointer", EtwPointer)
 
     cs.EtwPointer.as_64bit()
-    assert cs.EtwPointer.default() == 0
+    assert cs.EtwPointer.__default__() == 0
 
     cs.EtwPointer.as_32bit()
-    assert cs.EtwPointer.default() == 0
+    assert cs.EtwPointer.__default__() == 0
 
-    assert cs.EtwPointer[1].default() == [0]
-    assert cs.EtwPointer[None].default() == []
+    assert cs.EtwPointer[1].__default__() == [0]
+    assert cs.EtwPointer[None].__default__() == []
diff --git a/tests/test_types_enum.py b/tests/test_types_enum.py
index 9045e2b..f04a26f 100644
--- a/tests/test_types_enum.py
+++ b/tests/test_types_enum.py
@@ -418,6 +418,21 @@ def test_enum_default(cs: cstruct) -> None:
     """
     cs.load(cdef)
 
-    assert cs.test.default() == cs.test.A == cs.test(0)
-    assert cs.test[1].default() == [cs.test.A]
-    assert cs.test[None].default() == []
+    assert cs.test.__default__() == cs.test.A == cs.test(0)
+    assert cs.test[1].__default__() == [cs.test.A]
+    assert cs.test[None].__default__() == []
+
+
+def test_enum_default_default(cs: cstruct) -> None:
+    cdef = """
+    enum test {
+        default = 0,
+    };
+
+    struct test2 {
+        test a;
+    };
+    """
+    cs.load(cdef)
+
+    assert cs.test.__default__() == cs.test.default == cs.test(0)
diff --git a/tests/test_types_flag.py b/tests/test_types_flag.py
index 935aac9..6e8127c 100644
--- a/tests/test_types_flag.py
+++ b/tests/test_types_flag.py
@@ -266,6 +266,6 @@ def test_flag_default(cs: cstruct) -> None:
     """
     cs.load(cdef)
 
-    assert cs.test.default() == cs.test(0)
-    assert cs.test[1].default() == [cs.test(0)]
-    assert cs.test[None].default() == []
+    assert cs.test.__default__() == cs.test(0)
+    assert cs.test[1].__default__() == [cs.test(0)]
+    assert cs.test[None].__default__() == []
diff --git a/tests/test_types_int.py b/tests/test_types_int.py
index 329c9d7..524fa80 100644
--- a/tests/test_types_int.py
+++ b/tests/test_types_int.py
@@ -403,10 +403,10 @@ def test_int_struct_unsigned_be(cs: cstruct, compiled: bool) -> None:
 
 
 def test_int_default(cs: cstruct) -> None:
-    assert cs.int24.default() == 0
-    assert cs.uint24.default() == 0
-    assert cs.int128.default() == 0
-    assert cs.uint128.default() == 0
+    assert cs.int24.__default__() == 0
+    assert cs.uint24.__default__() == 0
+    assert cs.int128.__default__() == 0
+    assert cs.uint128.__default__() == 0
 
-    assert cs.int24[1].default() == [0]
-    assert cs.int24[None].default() == []
+    assert cs.int24[1].__default__() == [0]
+    assert cs.int24[None].__default__() == []
diff --git a/tests/test_types_leb128.py b/tests/test_types_leb128.py
index 30e6dae..1c425be 100644
--- a/tests/test_types_leb128.py
+++ b/tests/test_types_leb128.py
@@ -192,8 +192,8 @@ def test_leb128_unsigned_write_amount_written(cs: cstruct) -> None:
 
 
 def test_leb128_default(cs: cstruct) -> None:
-    assert cs.uleb128.default() == 0
-    assert cs.ileb128.default() == 0
+    assert cs.uleb128.__default__() == 0
+    assert cs.ileb128.__default__() == 0
 
-    assert cs.uleb128[1].default() == [0]
-    assert cs.uleb128[None].default() == []
+    assert cs.uleb128[1].__default__() == [0]
+    assert cs.uleb128[None].__default__() == []
diff --git a/tests/test_types_packed.py b/tests/test_types_packed.py
index 5e4d2f2..2126d5e 100644
--- a/tests/test_types_packed.py
+++ b/tests/test_types_packed.py
@@ -172,17 +172,17 @@ def test_packed_float_struct_be(cs: cstruct, compiled: bool) -> None:
 
 
 def test_packed_default(cs: cstruct) -> None:
-    assert cs.int8.default() == 0
-    assert cs.uint8.default() == 0
-    assert cs.int16.default() == 0
-    assert cs.uint16.default() == 0
-    assert cs.int32.default() == 0
-    assert cs.uint32.default() == 0
-    assert cs.int64.default() == 0
-    assert cs.uint64.default() == 0
-    assert cs.float16.default() == 0.0
-    assert cs.float.default() == 0.0
-    assert cs.double.default() == 0.0
-
-    assert cs.int8[2].default() == [0, 0]
-    assert cs.int8[None].default() == []
+    assert cs.int8.__default__() == 0
+    assert cs.uint8.__default__() == 0
+    assert cs.int16.__default__() == 0
+    assert cs.uint16.__default__() == 0
+    assert cs.int32.__default__() == 0
+    assert cs.uint32.__default__() == 0
+    assert cs.int64.__default__() == 0
+    assert cs.uint64.__default__() == 0
+    assert cs.float16.__default__() == 0.0
+    assert cs.float.__default__() == 0.0
+    assert cs.double.__default__() == 0.0
+
+    assert cs.int8[2].__default__() == [0, 0]
+    assert cs.int8[None].__default__() == []
diff --git a/tests/test_types_pointer.py b/tests/test_types_pointer.py
index 65be5dd..269cda4 100644
--- a/tests/test_types_pointer.py
+++ b/tests/test_types_pointer.py
@@ -242,10 +242,10 @@ def test_pointer_default(cs: cstruct) -> None:
     cs.pointer = cs.uint8
 
     ptr = cs._make_pointer(cs.uint8)
-    assert isinstance(ptr.default(), Pointer)
-    assert ptr.default() == 0
-    assert ptr[1].default() == [0]
-    assert ptr[None].default() == []
+    assert isinstance(ptr.__default__(), Pointer)
+    assert ptr.__default__() == 0
+    assert ptr[1].__default__() == [0]
+    assert ptr[None].__default__() == []
 
     with pytest.raises(NullPointerDereference):
-        ptr.default().dereference()
+        ptr.__default__().dereference()
diff --git a/tests/test_types_structure.py b/tests/test_types_structure.py
index d2d65d0..8d251eb 100644
--- a/tests/test_types_structure.py
+++ b/tests/test_types_structure.py
@@ -599,9 +599,9 @@ def test_structure_default(cs: cstruct, compiled: bool) -> None:
 
     assert verify_compiled(cs.test, compiled)
 
-    assert cs.test() == cs.test.default()
+    assert cs.test() == cs.test.__default__()
 
-    obj = cs.test.default()
+    obj = cs.test.__default__()
     assert obj.t_int == 0
     assert obj.t_int_array == [0, 0]
     assert obj.t_bytesint == 0
@@ -625,11 +625,11 @@ def test_structure_default(cs: cstruct, compiled: bool) -> None:
     for name in obj.fields.keys():
         assert isinstance(getattr(obj, name), BaseType)
 
-    assert cs.test_nested() == cs.test_nested.default()
+    assert cs.test_nested() == cs.test_nested.__default__()
 
-    obj = cs.test_nested.default()
-    assert obj.t_struct == cs.test.default()
-    assert obj.t_struct_array == [cs.test.default(), cs.test.default()]
+    obj = cs.test_nested.__default__()
+    assert obj.t_struct == cs.test.__default__()
+    assert obj.t_struct_array == [cs.test.__default__(), cs.test.__default__()]
 
     assert obj.dumps() == b"\x00" * 171
 
@@ -678,7 +678,7 @@ def test_structure_default_dynamic(cs: cstruct, compiled: bool) -> None:
 
     assert verify_compiled(cs.test, compiled)
 
-    assert cs.test() == cs.test.default()
+    assert cs.test() == cs.test.__default__()
 
     obj = cs.test()
     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:
     for name in obj.fields.keys():
         assert isinstance(getattr(obj, name), BaseType)
 
-    assert cs.test_nested() == cs.test_nested.default()
+    assert cs.test_nested() == cs.test_nested.__default__()
 
-    obj = cs.test_nested.default()
+    obj = cs.test_nested.__default__()
     assert obj.t_struct_n == obj.t_struct_array_d == []
 
     assert obj.dumps() == b"\x00" * 21
diff --git a/tests/test_types_union.py b/tests/test_types_union.py
index 0caf59a..c36eb52 100644
--- a/tests/test_types_union.py
+++ b/tests/test_types_union.py
@@ -414,7 +414,7 @@ def test_union_default(cs: cstruct) -> None:
     """
     cs.load(cdef)
 
-    assert cs.test() == cs.test.default()
+    assert cs.test() == cs.test.__default__()
 
     obj = cs.test()
     assert obj.a == 0
@@ -425,11 +425,11 @@ def test_union_default(cs: cstruct) -> None:
     for name in obj.fields.keys():
         assert isinstance(getattr(obj, name), BaseType)
 
-    assert cs.test_nested() == cs.test_nested.default()
+    assert cs.test_nested() == cs.test_nested.__default__()
 
-    obj = cs.test_nested.default()
-    assert obj.t_union == cs.test.default()
-    assert obj.t_union_array == [cs.test.default(), cs.test.default()]
+    obj = cs.test_nested.__default__()
+    assert obj.t_union == cs.test.__default__()
+    assert obj.t_union_array == [cs.test.__default__(), cs.test.__default__()]
 
     assert obj.dumps() == b"\x00" * 24
 
diff --git a/tests/test_types_void.py b/tests/test_types_void.py
index 6e82585..2b55456 100644
--- a/tests/test_types_void.py
+++ b/tests/test_types_void.py
@@ -33,12 +33,12 @@ def test_void_array_write(cs: cstruct) -> None:
 
 
 def test_void_default(cs: cstruct) -> None:
-    assert cs.void() == cs.void.default()
+    assert cs.void() == cs.void.__default__()
     assert not cs.void()
-    assert not cs.void.default()
+    assert not cs.void.__default__()
 
-    assert cs.void[1].default() == [cs.void()]
-    assert cs.void[None].default() == []
+    assert cs.void[1].__default__() == [cs.void()]
+    assert cs.void[None].__default__() == []
 
 
 def test_void_struct(cs: cstruct, compiled: bool) -> None:
diff --git a/tests/test_types_wchar.py b/tests/test_types_wchar.py
index 65a63d1..4c8b9e0 100644
--- a/tests/test_types_wchar.py
+++ b/tests/test_types_wchar.py
@@ -78,6 +78,6 @@ def test_wchar_eof(cs: cstruct) -> None:
 
 
 def test_wchar_default(cs: cstruct) -> None:
-    assert cs.wchar.default() == "\x00"
-    assert cs.wchar[4].default() == "\x00\x00\x00\x00"
-    assert cs.wchar[None].default() == ""
+    assert cs.wchar.__default__() == "\x00"
+    assert cs.wchar[4].__default__() == "\x00\x00\x00\x00"
+    assert cs.wchar[None].__default__() == ""