dissect.cstruct 5.0.dev24__py3-none-any.whl → 5.0.dev25__py3-none-any.whl

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.
@@ -21,6 +21,7 @@ from dissect.cstruct.types import (
21
21
  BaseType,
22
22
  Char,
23
23
  Enum,
24
+ Field,
24
25
  Flag,
25
26
  Int,
26
27
  Packed,
@@ -36,10 +37,7 @@ if TYPE_CHECKING:
36
37
  from collections.abc import Generator, Iterable
37
38
  from typing import TypeAlias
38
39
 
39
- from dissect.cstruct.types import (
40
- Array,
41
- Field,
42
- )
40
+ from dissect.cstruct.types import Array
43
41
 
44
42
 
45
43
  T = TypeVar("T", bound=BaseType)
@@ -245,21 +243,62 @@ class cstruct:
245
243
  raise AttributeError(f"'{type(self).__name__}' object has no attribute {attr!r}")
246
244
 
247
245
  def __copy__(self) -> cstruct:
248
- cs = cstruct(endian=self.endian, pointer=self.pointer.__name__)
246
+ cs = cstruct(endian=self.endian)
249
247
  cs._anonymous_count = self._anonymous_count
250
248
  cs.includes = self.includes.copy()
251
249
 
252
- # Update types to point to the new cstruct instance
250
+ # Copy the types as new classes, including the unregistered types they refer to
251
+ copies = {}
253
252
  for name, type_ in self.types.items():
254
- new_type = copy.copy(type_)
255
- new_type.__cs__ = cs
256
- cs.add_type(name, new_type, replace=True)
253
+ cs.add_type(name, cs._copy_type(type_, copies), replace=True)
254
+ cs.pointer = cs._copy_type(self.pointer, copies)
257
255
 
258
256
  for name, value in self.consts.items():
259
257
  cs.add_const(name, value)
260
258
 
261
259
  return cs
262
260
 
261
+ def _copy_type(self, type_: type[BaseType], copies: dict[type[BaseType], type[BaseType]]) -> type[BaseType]:
262
+ if type_ in copies:
263
+ return copies[type_]
264
+
265
+ if issubclass(type_, (Enum, Flag)):
266
+ # Enum classes with members can't be created from their namespace
267
+ factory = self._make_flag if issubclass(type_, Flag) else self._make_enum
268
+ values = {name: member.value for name, member in type_.__members__.items()}
269
+ copies[type_] = factory(type_.__name__, self._copy_type(type_.__type__, copies), values)
270
+ return copies[type_]
271
+
272
+ # A type is registered before its subclasses, e.g. a declarative structure, so the copy of its base exists
273
+ bases = tuple(copies.get(base, base) for base in type_.__bases__)
274
+
275
+ # The namespace of a class doesn't hold its qualified name, e.g. of a declarative structure in a function
276
+ attrs = {**vars(type_), "__qualname__": type_.__qualname__}
277
+
278
+ # Register the copy before copying the types it refers to, so that a structure can point to itself
279
+ if issubclass(type_, Structure):
280
+ attrs["__members__"] = []
281
+
282
+ new_type = copies[type_] = self._make_type(
283
+ type_.__name__,
284
+ bases,
285
+ type_.__size__,
286
+ alignment=type_.__alignment__,
287
+ attrs=attrs,
288
+ )
289
+
290
+ if issubclass(type_, (BaseArray, Pointer)):
291
+ new_type.__type__ = self._copy_type(type_.__type__, copies)
292
+
293
+ if issubclass(type_, Structure):
294
+ new_type.__fields__ = [
295
+ Field(field.name, self._copy_type(field.type, copies), field.bits, field.offset)
296
+ for field in type_.__fields__
297
+ ]
298
+ new_type.commit()
299
+
300
+ return new_type
301
+
263
302
  # Hidden from type checkers, which otherwise accept assigning any attribute, e.g. a misspelled one
264
303
  if not TYPE_CHECKING:
265
304
 
@@ -395,6 +395,10 @@ class _Declarative:
395
395
  def __init_subclass__(cls, *, cs: cstruct | None = None, align: bool | None = None, **kwargs):
396
396
  super().__init_subclass__(**kwargs)
397
397
 
398
+ if "__fields__" in vars(cls):
399
+ # Created by cstruct with its fields, e.g. a copy made by cstruct.copy()
400
+ return
401
+
398
402
  if cs is not None:
399
403
  cls.__cs__ = cs
400
404
  if align is not None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dissect.cstruct
3
- Version: 5.0.dev24
3
+ Version: 5.0.dev25
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-Expression: Apache-2.0
@@ -1,8 +1,8 @@
1
1
  dissect/cstruct/__init__.py,sha256=6WCBCk9iTJiKjzfkYCZAwWkIh3FnxEuCe_y2TSUJ2bU,1905
2
2
  dissect/cstruct/bitbuffer.py,sha256=FpvyoDuio0hLfUtyqb2OUg1SpDVnblxMVNtAbYLPzVk,2781
3
3
  dissect/cstruct/compiler.py,sha256=6mK2CdUnfWdQIu0_1qIL1NCaaqlv5eJcvlamMsGfEAg,54781
4
- dissect/cstruct/cstruct.py,sha256=quHzIZoM6ADQrfrMPsQ-QzxJ-9D2glYHWwSQXn7cysM,27645
5
- dissect/cstruct/declarative.py,sha256=GPU208IsslegjFug9udduu-6fhmV2CIfIf6oY76ycEw,14571
4
+ dissect/cstruct/cstruct.py,sha256=PLGbkE01Hse9ODAoxGkrcuKo7_vMHDBJ8QvL52MD_mM,29432
5
+ dissect/cstruct/declarative.py,sha256=8yyQeP22BoZh6Gy3TASQ1HrVkecMNb3BVKj7oBdb0QU,14714
6
6
  dissect/cstruct/dump.py,sha256=W5ISbaZ-1YmWXKzh-x6pDt5_SlpFRDhD9GGNyv1MnLw,19016
7
7
  dissect/cstruct/exception.py,sha256=8b_nLXacWGdQ3BhZN-DOOEr4gp5Vacni6etwDKqYHis,988
8
8
  dissect/cstruct/expression.py,sha256=HMDKuZmPElZF88dC6FdEpUO_yUTCSw-WU7wI_XsgYEY,13592
@@ -23,10 +23,10 @@ dissect/cstruct/types/pointer.py,sha256=pIxiDGosRu1jhchdcA3Zm7yIXkPUh2UKnIt8kxUy
23
23
  dissect/cstruct/types/structure.py,sha256=T28ZncDWG2QtkQcpKNwfpNmk5_77IgW0idDxHINzcFY,43638
24
24
  dissect/cstruct/types/void.py,sha256=fV4hKk50uop_EKbR1nfy1cdtAzvUYrSymXsdakCyFk8,1222
25
25
  dissect/cstruct/types/wchar.py,sha256=eJyOWdT8Y3k8gM9QNE0NeXBwcAGRcp4aUkIBezAYe0c,4225
26
- dissect_cstruct-5.0.dev24.dist-info/licenses/COPYRIGHT,sha256=H-18RXfshdH9AdHwW2eO1Xa-5s6tY5eipHh5c0whDu4,316
27
- dissect_cstruct-5.0.dev24.dist-info/licenses/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
28
- dissect_cstruct-5.0.dev24.dist-info/METADATA,sha256=TwINfkCJixnZO2mCLvYnMw5DvUJxNIOfdIbf4o7wMeI,12661
29
- dissect_cstruct-5.0.dev24.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
30
- dissect_cstruct-5.0.dev24.dist-info/entry_points.txt,sha256=z53zqZqwD2OLrAkRwrP4wTeiU9CQe7xrMly0T2c0_wQ,71
31
- dissect_cstruct-5.0.dev24.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
32
- dissect_cstruct-5.0.dev24.dist-info/RECORD,,
26
+ dissect_cstruct-5.0.dev25.dist-info/licenses/COPYRIGHT,sha256=H-18RXfshdH9AdHwW2eO1Xa-5s6tY5eipHh5c0whDu4,316
27
+ dissect_cstruct-5.0.dev25.dist-info/licenses/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
28
+ dissect_cstruct-5.0.dev25.dist-info/METADATA,sha256=79oFT7smYtaoI1JjRcK36TiL6ZkCOWbmAMefMM7zk5k,12661
29
+ dissect_cstruct-5.0.dev25.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
30
+ dissect_cstruct-5.0.dev25.dist-info/entry_points.txt,sha256=z53zqZqwD2OLrAkRwrP4wTeiU9CQe7xrMly0T2c0_wQ,71
31
+ dissect_cstruct-5.0.dev25.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
32
+ dissect_cstruct-5.0.dev25.dist-info/RECORD,,