Skip to content

Commit cc3738b

Browse files
committed
Use dunder copy instead
1 parent 52f7881 commit cc3738b

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

dissect/cstruct/cstruct.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,25 @@ def __getattr__(self, attr: str) -> Any:
208208

209209
raise AttributeError(f"Invalid attribute: {attr}")
210210

211+
def __copy__(self) -> cstruct:
212+
cs = cstruct(endian=self.endian, pointer=self.pointer.__name__)
213+
cs._anonymous_count = self._anonymous_count
214+
cs.consts = self.consts.copy()
215+
cs.lookups = self.lookups.copy()
216+
cs.includes = self.includes.copy()
217+
218+
# Update typedefs to point to the new cstruct instance
219+
for name, type in self.typedefs.items():
220+
if name in cs.typedefs:
221+
continue
222+
223+
if isinstance(type, MetaType):
224+
new_type = copy.copy(type)
225+
new_type.cs = cs
226+
cs.typedefs[name] = new_type
227+
228+
return cs
229+
211230
def _next_anonymous(self) -> str:
212231
name = f"__anonymous_{self._anonymous_count}__"
213232
self._anonymous_count += 1
@@ -336,23 +355,7 @@ def copy(self) -> cstruct:
336355
Returns:
337356
A new cstruct instance with the same types and settings as this one.
338357
"""
339-
cs = cstruct(endian=self.endian, pointer=self.pointer.__name__)
340-
cs._anonymous_count = self._anonymous_count
341-
cs.consts = self.consts.copy()
342-
cs.lookups = self.lookups.copy()
343-
cs.includes = self.includes.copy()
344-
345-
# Update typedefs to point to the new cstruct instance
346-
for name, type in self.typedefs.items():
347-
if name in cs.typedefs:
348-
continue
349-
350-
if isinstance(type, MetaType):
351-
new_type = copy.copy(type)
352-
new_type.cs = cs
353-
cs.typedefs[name] = new_type
354-
355-
return cs
358+
return copy.copy(self)
356359

357360
def _make_type(
358361
self,

0 commit comments

Comments
 (0)