5858
5959
6060def compile (structure : type [Structure ]) -> type [Structure ]:
61- return Compiler (structure .cs ).compile (structure )
61+ return Compiler (structure .__cs__ ).compile (structure )
6262
6363
6464class Compiler :
@@ -163,7 +163,7 @@ def align_to_field(field: Field) -> Iterator[str]:
163163 field_type = field .type
164164
165165 if isinstance (field_type , EnumMetaType ):
166- field_type = field_type .type
166+ field_type = field_type .__type__
167167
168168 if not issubclass (field_type , SUPPORTED_TYPES ):
169169 raise TypeError (f"Unsupported type for compiler: { field_type } " )
@@ -188,7 +188,7 @@ def align_to_field(field: Field) -> Iterator[str]:
188188
189189 # Array of structures and multi-dimensional arrays
190190 elif issubclass (field_type , (Array , CharArray , WcharArray )) and (
191- issubclass (field_type .type , Structure ) or issubclass (field_type .type , BaseArray ) or is_dynamic
191+ issubclass (field_type .__type__ , Structure ) or issubclass (field_type .__type__ , BaseArray ) or is_dynamic
192192 ):
193193 yield from flush ()
194194 yield from align_to_field (field )
@@ -222,22 +222,22 @@ def align_to_field(field: Field) -> Iterator[str]:
222222 yield from flush ()
223223
224224 if self .align :
225- yield f"stream.seek(-stream.tell() & (cls.alignment - 1), { io .SEEK_CUR } )"
225+ yield f"stream.seek(-stream.tell() & (cls.__alignment__ - 1), { io .SEEK_CUR } )"
226226
227227 def _generate_structure (self , field : Field ) -> Iterator [str ]:
228228 template = f"""
229- { "_s = stream.tell()" if field .type .dynamic else "" }
229+ { "_s = stream.tell()" if field .type .__dynamic__ else "" }
230230 r["{ field ._name } "] = { self ._map_field (field )} ._read(stream, context=r, endian=endian)
231- { f's["{ field ._name } "] = stream.tell() - _s' if field .type .dynamic else "" }
231+ { f's["{ field ._name } "] = stream.tell() - _s' if field .type .__dynamic__ else "" }
232232 """
233233
234234 yield dedent (template )
235235
236236 def _generate_array (self , field : Field ) -> Iterator [str ]:
237237 template = f"""
238- { "_s = stream.tell()" if field .type .dynamic else "" }
238+ { "_s = stream.tell()" if field .type .__dynamic__ else "" }
239239 r["{ field ._name } "] = { self ._map_field (field )} ._read(stream, context=r, endian=endian)
240- { f's["{ field ._name } "] = stream.tell() - _s' if field .type .dynamic else "" }
240+ { f's["{ field ._name } "] = stream.tell() - _s' if field .type .__dynamic__ else "" }
241241 """
242242
243243 yield dedent (template )
@@ -247,12 +247,12 @@ def _generate_bits(self, field: Field) -> Iterator[str]:
247247 read_type = "_t"
248248 field_type = field .type
249249 if issubclass (field_type , (Enum , Flag )):
250- read_type += ".type "
251- field_type = field_type .type
250+ read_type += ".__type__ "
251+ field_type = field_type .__type__
252252
253253 if issubclass (field_type , Char ):
254- field_type = field_type .cs .uint8
255- lookup = "cls.cs .uint8"
254+ field_type = field_type .__cs__ .uint8
255+ lookup = "cls.__cs__ .uint8"
256256
257257 template = f"""
258258 _t = { lookup }
@@ -277,17 +277,17 @@ def _generate_packed(self, fields: list[Field]) -> Iterator[str]:
277277 read_type = _get_read_type (self .cs , field_type )
278278
279279 if issubclass (field_type , (Array , CharArray , WcharArray )):
280- count = field_type .num_entries
281- read_type = _get_read_type (self .cs , field_type .type )
280+ count = field_type .__num_entries__
281+ read_type = _get_read_type (self .cs , field_type .__type__ )
282282
283283 if issubclass (read_type , (Char , Wchar , Int )):
284- count *= read_type .size
284+ count *= read_type .__size__
285285 getter = f"buf[{ size } :{ size + count } ]"
286286 else :
287287 getter = f"data[{ slice_index } :{ slice_index + count } ]"
288288 slice_index += count
289289 elif issubclass (read_type , (Char , Wchar , Int )):
290- getter = f"buf[{ size } :{ size + read_type .size } ]"
290+ getter = f"buf[{ size } :{ size + read_type .__size__ } ]"
291291 else :
292292 getter = f"data[{ slice_index } ]"
293293 slice_index += 1
@@ -302,13 +302,13 @@ def _generate_packed(self, fields: list[Field]) -> Iterator[str]:
302302 # Create the final reading code
303303 if issubclass (field_type , Array ):
304304 reads .append (f"_t = { self ._map_field (field )} " )
305- reads .append ("_et = _t.type " )
305+ reads .append ("_et = _t.__type__ " )
306306
307- if issubclass (field_type .type , Int ):
307+ if issubclass (field_type .__type__ , Int ):
308308 reads .append (f"_b = { getter } " )
309- item_parser = parser_template .format (type = "_et" , getter = f"_b[i:i + { field_type .type . size } ]" )
310- list_comp = f"[{ item_parser } for i in range(0, { count } , { field_type .type . size } )]"
311- elif issubclass (field_type .type , Pointer ):
309+ item_parser = parser_template .format (type = "_et" , getter = f"_b[i:i + { field_type .__type__ . __size__ } ]" )
310+ list_comp = f"[{ item_parser } for i in range(0, { count } , { field_type .__type__ . __size__ } )]"
311+ elif issubclass (field_type .__type__ , Pointer ):
312312 item_parser = "_et.__new__(_et, e, stream, context=r, endian=endian)"
313313 list_comp = f"[{ item_parser } for e in { getter } ]"
314314 else :
@@ -327,7 +327,7 @@ def _generate_packed(self, fields: list[Field]) -> Iterator[str]:
327327 reads .append (f'r["{ field ._name } "] = { parser } ' )
328328 reads .append ("" ) # Generates a newline in the resulting code
329329
330- size += field_type .size
330+ size += field_type .__size__
331331
332332 fmt = _optimize_struct_fmt (info )
333333 if fmt == "x" or (len (fmt ) == 2 and fmt [1 ] == "x" ):
@@ -370,19 +370,19 @@ def _generate_struct_info(cs: cstruct, fields: list[Field], align: bool = False)
370370
371371 # Array of more complex types are handled elsewhere
372372 if issubclass (read_type , (Array , CharArray , WcharArray )):
373- count = read_type .num_entries
374- read_type = _get_read_type (cs , read_type .type )
373+ count = read_type .__num_entries__
374+ read_type = _get_read_type (cs , read_type .__type__ )
375375
376376 # Take the pack char for Packed
377377 if issubclass (read_type , Packed ):
378- yield field , count , read_type .packchar
378+ yield field , count , read_type .__fmt__
379379
380380 # Other types are byte based
381381 # We don't actually unpack anything here but slice directly out of the buffer
382382 elif issubclass (read_type , (Char , Wchar , Int )):
383- yield field , count * read_type .size , "x"
383+ yield field , count * read_type .__size__ , "x"
384384
385- size = count * read_type .size
385+ size = count * read_type .__size__
386386 imaginary_offset += size
387387 if current_offset is not None :
388388 current_offset += size
@@ -416,7 +416,7 @@ def _optimize_struct_fmt(info: Iterator[tuple[Field, int, str]]) -> str:
416416
417417def _get_read_type (cs : cstruct , type_ : type [BaseType ]) -> type [BaseType ]:
418418 if issubclass (type_ , (Enum , Flag )):
419- type_ = type_ .type
419+ type_ = type_ .__type__
420420
421421 if issubclass (type_ , Pointer ):
422422 type_ = cs .pointer
0 commit comments