dissect.cstruct 4.5.dev2__tar.gz → 4.5.dev4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/PKG-INFO +1 -1
  2. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/types/enum.py +6 -6
  3. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/types/structure.py +10 -10
  4. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect.cstruct.egg-info/PKG-INFO +1 -1
  5. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_types_structure.py +16 -5
  6. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_types_union.py +12 -0
  7. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/.git-blame-ignore-revs +0 -0
  8. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/.gitattributes +0 -0
  9. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/COPYRIGHT +0 -0
  10. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/LICENSE +0 -0
  11. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/MANIFEST.in +0 -0
  12. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/README.md +0 -0
  13. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/__init__.py +0 -0
  14. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/bitbuffer.py +0 -0
  15. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/compiler.py +0 -0
  16. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/cstruct.py +0 -0
  17. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/exceptions.py +0 -0
  18. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/expression.py +0 -0
  19. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/parser.py +0 -0
  20. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/tools/__init__.py +0 -0
  21. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/tools/stubgen.py +0 -0
  22. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/types/__init__.py +0 -0
  23. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/types/base.py +0 -0
  24. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/types/char.py +0 -0
  25. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/types/flag.py +0 -0
  26. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/types/int.py +0 -0
  27. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/types/leb128.py +0 -0
  28. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/types/packed.py +0 -0
  29. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/types/pointer.py +0 -0
  30. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/types/void.py +0 -0
  31. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/types/wchar.py +0 -0
  32. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect/cstruct/utils.py +0 -0
  33. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect.cstruct.egg-info/SOURCES.txt +0 -0
  34. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect.cstruct.egg-info/dependency_links.txt +0 -0
  35. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect.cstruct.egg-info/entry_points.txt +0 -0
  36. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect.cstruct.egg-info/requires.txt +0 -0
  37. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/dissect.cstruct.egg-info/top_level.txt +0 -0
  38. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/examples/disk.py +0 -0
  39. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/examples/mirai.py +0 -0
  40. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/examples/pe.py +0 -0
  41. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/examples/protobuf.py +0 -0
  42. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/examples/secdesc.py +0 -0
  43. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/pyproject.toml +0 -0
  44. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/setup.cfg +0 -0
  45. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/__init__.py +0 -0
  46. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/_data/testdef.txt +0 -0
  47. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/_docs/Makefile +0 -0
  48. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/_docs/__init__.py +0 -0
  49. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/_docs/conf.py +0 -0
  50. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/_docs/index.rst +0 -0
  51. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/conftest.py +0 -0
  52. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_align.py +0 -0
  53. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_annotations.py +0 -0
  54. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_basic.py +0 -0
  55. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_bitbuffer.py +0 -0
  56. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_bitfield.py +0 -0
  57. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_compiler.py +0 -0
  58. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_ctypes.py +0 -0
  59. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_expression.py +0 -0
  60. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_parser.py +0 -0
  61. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_tools_stubgen.py +0 -0
  62. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_types_base.py +0 -0
  63. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_types_char.py +0 -0
  64. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_types_custom.py +0 -0
  65. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_types_enum.py +0 -0
  66. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_types_flag.py +0 -0
  67. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_types_int.py +0 -0
  68. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_types_leb128.py +0 -0
  69. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_types_packed.py +0 -0
  70. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_types_pointer.py +0 -0
  71. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_types_void.py +0 -0
  72. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_types_wchar.py +0 -0
  73. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/test_utils.py +0 -0
  74. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tests/utils.py +0 -0
  75. {dissect_cstruct-4.5.dev2 → dissect_cstruct-4.5.dev4}/tox.ini +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dissect.cstruct
3
- Version: 4.5.dev2
3
+ Version: 4.5.dev4
4
4
  Summary: A Dissect module implementing a parser for C-like structures: structure parsing in Python made easy
5
5
  Author-email: Dissect Team <dissect@fox-it.com>
6
6
  License: Apache License 2.0
@@ -76,12 +76,12 @@ class EnumMetaType(EnumMeta, MetaType):
76
76
 
77
77
  __len__ = MetaType.__len__
78
78
 
79
- if not PY_312:
80
- # Backport __contains__ from CPython 3.12
81
- def __contains__(cls, value: Any) -> bool:
82
- if isinstance(value, cls):
83
- return True
84
- return value in cls._value2member_map_
79
+ def __contains__(cls, value: Any) -> bool:
80
+ # We used to let stdlib enum handle `__contains__``` but this commit is incompatible with our API:
81
+ # https://github.com/python/cpython/commit/8a9aee71268c77867d3cc96d43cbbdcbe8c0e1e8
82
+ if isinstance(value, cls):
83
+ return True
84
+ return value in cls._value2member_map_
85
85
 
86
86
  def _read(cls, stream: BinaryIO, context: dict[str, Any] | None = None) -> Self:
87
87
  return cls(cls.type._read(stream, context))
@@ -673,7 +673,9 @@ def _make_structure__init__(fields: list[str]) -> str:
673
673
  fields: List of field names.
674
674
  """
675
675
  field_args = ", ".join(f"{field} = None" for field in fields)
676
- field_init = "\n".join(f" self.{name} = {name} if {name} is not None else {i}" for i, name in enumerate(fields))
676
+ field_init = "\n".join(
677
+ f" self.{name} = {name} if {name} is not None else _{i}_default" for i, name in enumerate(fields)
678
+ )
677
679
 
678
680
  code = f"def __init__(self{', ' + field_args or ''}):\n"
679
681
  return code + (field_init or " pass")
@@ -688,7 +690,8 @@ def _make_union__init__(fields: list[str]) -> str:
688
690
  """
689
691
  field_args = ", ".join(f"{field} = None" for field in fields)
690
692
  field_init = "\n".join(
691
- f" object.__setattr__(self, '{name}', {name} if {name} is not None else {i})" for i, name in enumerate(fields)
693
+ f" object.__setattr__(self, '{name}', {name} if {name} is not None else _{i}_default)"
694
+ for i, name in enumerate(fields)
692
695
  )
693
696
 
694
697
  code = f"def __init__(self{', ' + field_args or ''}):\n"
@@ -780,11 +783,10 @@ def _generate_structure__init__(fields: list[Field]) -> FunctionType:
780
783
  template: FunctionType = _make_structure__init__(len(field_names))
781
784
  return type(template)(
782
785
  template.__code__.replace(
783
- co_consts=(None, *[field.type.__default__() for field in fields]),
784
- co_names=(*field_names,),
786
+ co_names=tuple(chain.from_iterable(zip((f"__{name}_default__" for name in field_names), field_names))),
785
787
  co_varnames=("self", *field_names),
786
788
  ),
787
- template.__globals__,
789
+ template.__globals__ | {f"__{field._name}_default__": field.type.__default__() for field in fields},
788
790
  argdefs=template.__defaults__,
789
791
  )
790
792
 
@@ -800,13 +802,11 @@ def _generate_union__init__(fields: list[Field]) -> FunctionType:
800
802
  template: FunctionType = _make_union__init__(len(field_names))
801
803
  return type(template)(
802
804
  template.__code__.replace(
803
- co_consts=(
804
- None,
805
- *sum([(field._name, field.type.__default__()) for field in fields], ()),
806
- ),
805
+ co_consts=(None, *field_names),
806
+ co_names=("object", "__setattr__", *(f"__{name}_default__" for name in field_names)),
807
807
  co_varnames=("self", *field_names),
808
808
  ),
809
- template.__globals__,
809
+ template.__globals__ | {f"__{field._name}_default__": field.type.__default__() for field in fields},
810
810
  argdefs=template.__defaults__,
811
811
  )
812
812
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dissect.cstruct
3
- Version: 4.5.dev2
3
+ Version: 4.5.dev4
4
4
  Summary: A Dissect module implementing a parser for C-like structures: structure parsing in Python made easy
5
5
  Author-email: Dissect Team <dissect@fox-it.com>
6
6
  License: Apache License 2.0
@@ -750,11 +750,11 @@ def test_codegen_make_init() -> None:
750
750
  result = _make__init__([f"_{n}" for n in range(5)])
751
751
  expected = """
752
752
  def __init__(self, _0 = None, _1 = None, _2 = None, _3 = None, _4 = None):
753
- self._0 = _0 if _0 is not None else 0
754
- self._1 = _1 if _1 is not None else 1
755
- self._2 = _2 if _2 is not None else 2
756
- self._3 = _3 if _3 is not None else 3
757
- self._4 = _4 if _4 is not None else 4
753
+ self._0 = _0 if _0 is not None else _0_default
754
+ self._1 = _1 if _1 is not None else _1_default
755
+ self._2 = _2 if _2 is not None else _2_default
756
+ self._3 = _3 if _3 is not None else _3_default
757
+ self._4 = _4 if _4 is not None else _4_default
758
758
  """
759
759
  assert result == dedent(expected[1:].rstrip())
760
760
 
@@ -765,3 +765,14 @@ def test_codegen_make_init() -> None:
765
765
  cached = structure._make_structure__init__(5)
766
766
  assert structure._make_structure__init__.cache_info() == (1, 1, 128, 1)
767
767
  assert result is cached
768
+
769
+
770
+ def test_codegen_hashable(cs: cstruct) -> None:
771
+ hashable_fields = [Field("a", cs.uint8), Field("b", cs.uint8)]
772
+ unhashable_fields = [Field("a", cs.uint8[2]), Field("b", cs.uint8)]
773
+
774
+ with pytest.raises(TypeError, match="unhashable type: 'uint8\\[2\\]'"):
775
+ hash(unhashable_fields[0].type.__default__())
776
+
777
+ assert hash(structure._generate_structure__init__(hashable_fields).__code__)
778
+ assert hash(structure._generate_structure__init__(unhashable_fields).__code__)
@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING
5
5
 
6
6
  import pytest
7
7
 
8
+ from dissect.cstruct.types import structure
8
9
  from dissect.cstruct.types.base import Array, BaseType
9
10
  from dissect.cstruct.types.structure import Field, Union, UnionProxy
10
11
 
@@ -535,3 +536,14 @@ def test_union_partial_initialization_dynamic(cs: cstruct) -> None:
535
536
 
536
537
  with pytest.raises(NotImplementedError, match="Initializing a dynamic union is not yet supported"):
537
538
  cs.test(x=1)
539
+
540
+
541
+ def test_codegen_hashable(cs: cstruct) -> None:
542
+ hashable_fields = [Field("a", cs.uint8), Field("b", cs.uint8)]
543
+ unhashable_fields = [Field("a", cs.uint8[2]), Field("b", cs.uint8)]
544
+
545
+ with pytest.raises(TypeError, match="unhashable type: 'uint8\\[2\\]'"):
546
+ hash(unhashable_fields[0].type.__default__())
547
+
548
+ assert hash(structure._generate_union__init__(hashable_fields).__code__)
549
+ assert hash(structure._generate_union__init__(unhashable_fields).__code__)