@@ -39,14 +39,13 @@ class struct_impl(struct):
3939
4040 def __init__ (self : struct_impl , resolver : Resolver | None = None , ** kwargs : ...) -> None :
4141 """Initialize the struct implementation."""
42- # If we have kwargs and the resolver is None, we provide a fake resolver
4342 if kwargs and resolver is None :
4443 resolver = FakeResolver ()
4544
4645 if not isinstance (resolver , Resolver ):
4746 raise TypeError ("The resolver must be a Resolver instance." )
4847
49- # struct overrides the __init__ method, so we need to call the parent class __init__ method
48+ # struct. __init__ raises by design; bypass it and call obj. __init__ directly.
5049 obj .__init__ (self , resolver )
5150
5251 object .__setattr__ (self , "_struct_name" , self .__class__ .__name__ )
@@ -81,9 +80,7 @@ def __setattr__(self: struct_impl, name: str, value: object) -> None:
8180 object .__setattr__ (self , name , value )
8281
8382 def __new__ (cls : struct_impl , * args : ..., ** kwargs : ...) -> Self :
84- """Create a new struct."""
85- # Skip the __new__ method of the parent class
86- # struct_impl -> struct -> obj becomes struct_impl -> obj
83+ """Create a new struct, bypassing struct.__new__ which is for the user-facing factory."""
8784 return obj .__new__ (cls )
8885
8986 def _inflate_struct_attributes (
@@ -152,13 +149,12 @@ def _inflate_struct_attributes(
152149 max_alignment = max (max_alignment , aligned )
153150 current_offset = _align_offset (current_offset , max_alignment )
154151
155- # For VLA structs, size must be computed dynamically since the count
156- # can change at runtime. Detect VLA by duck-typing: vla_impl has a
157- # _count_member attribute that plain array_impl does not .
152+ # VLA detection uses duck-typing on _count_member to avoid a circular
153+ # import between struct_impl and vla_impl ( vla_impl extends array_impl,
154+ # which imports struct) .
158155 members = object .__getattribute__ (self , "_members" )
159- last_member = list ( members .values ())[ - 1 ] if members else None
156+ last_name , last_member = next ( reversed ( members .items ()), ( None , None ))
160157 if last_member is not None and hasattr (last_member , "_count_member" ):
161- last_name = list (members .keys ())[- 1 ]
162158 object .__setattr__ (self , "_vla_fixed_offset" , self ._member_offsets [last_name ])
163159 else :
164160 object .__setattr__ (self , "size" , current_offset )
0 commit comments