6060
6161
6262def compile (structure : type [Structure ]) -> type [Structure ]:
63- return Compiler (structure .cs ).compile (structure )
63+ return Compiler (structure .__cs__ ).compile (structure )
6464
6565
6666class Compiler :
@@ -119,7 +119,7 @@ def generate_source(self) -> str:
119119 """
120120
121121 if any (field .bits for field in self .fields ):
122- preamble += "bit_reader = BitBuffer(stream, cls.cs .endian)\n "
122+ preamble += "bit_reader = BitBuffer(stream, cls.__cs__ .endian)\n "
123123
124124 read_code = "\n " .join (self ._generate_fields ())
125125
@@ -224,22 +224,22 @@ def align_to_field(field: Field) -> Iterator[str]:
224224 yield from flush ()
225225
226226 if self .align :
227- yield f"stream.seek(-stream.tell() & (cls.alignment - 1), { io .SEEK_CUR } )"
227+ yield f"stream.seek(-stream.tell() & (cls.__alignment__ - 1), { io .SEEK_CUR } )"
228228
229229 def _generate_structure (self , field : Field ) -> Iterator [str ]:
230230 template = f"""
231- { "_s = stream.tell()" if field .type .dynamic else "" }
231+ { "_s = stream.tell()" if field .type .__dynamic__ else "" }
232232 r["{ field ._name } "] = { self ._map_field (field )} ._read(stream, context=r)
233- { f's["{ field ._name } "] = stream.tell() - _s' if field .type .dynamic else "" }
233+ { f's["{ field ._name } "] = stream.tell() - _s' if field .type .__dynamic__ else "" }
234234 """
235235
236236 yield dedent (template )
237237
238238 def _generate_array (self , field : Field ) -> Iterator [str ]:
239239 template = f"""
240- { "_s = stream.tell()" if field .type .dynamic else "" }
240+ { "_s = stream.tell()" if field .type .__dynamic__ else "" }
241241 r["{ field ._name } "] = { self ._map_field (field )} ._read(stream, context=r)
242- { f's["{ field ._name } "] = stream.tell() - _s' if field .type .dynamic else "" }
242+ { f's["{ field ._name } "] = stream.tell() - _s' if field .type .__dynamic__ else "" }
243243 """
244244
245245 yield dedent (template )
@@ -253,8 +253,8 @@ def _generate_bits(self, field: Field) -> Iterator[str]:
253253 field_type = field_type .type
254254
255255 if issubclass (field_type , Char ):
256- field_type = field_type .cs .uint8
257- lookup = "cls.cs .uint8"
256+ field_type = field_type .__cs__ .uint8
257+ lookup = "cls.__cs__ .uint8"
258258
259259 template = f"""
260260 _t = { lookup }
@@ -283,13 +283,13 @@ def _generate_packed(self, fields: list[Field]) -> Iterator[str]:
283283 read_type = _get_read_type (self .cs , field_type .type )
284284
285285 if issubclass (read_type , (Char , Wchar , Int )):
286- count *= read_type .size
286+ count *= read_type .__size__
287287 getter = f"buf[{ size } :{ size + count } ]"
288288 else :
289289 getter = f"data[{ slice_index } :{ slice_index + count } ]"
290290 slice_index += count
291291 elif issubclass (read_type , (Char , Wchar , Int )):
292- getter = f"buf[{ size } :{ size + read_type .size } ]"
292+ getter = f"buf[{ size } :{ size + read_type .__size__ } ]"
293293 else :
294294 getter = f"data[{ slice_index } ]"
295295 slice_index += 1
@@ -308,8 +308,8 @@ def _generate_packed(self, fields: list[Field]) -> Iterator[str]:
308308
309309 if issubclass (field_type .type , Int ):
310310 reads .append (f"_b = { getter } " )
311- item_parser = parser_template .format (type = "_et" , getter = f"_b[i:i + { field_type .type .size } ]" )
312- list_comp = f"[{ item_parser } for i in range(0, { count } , { field_type .type .size } )]"
311+ item_parser = parser_template .format (type = "_et" , getter = f"_b[i:i + { field_type .type .__size__ } ]" )
312+ list_comp = f"[{ item_parser } for i in range(0, { count } , { field_type .type .__size__ } )]"
313313 elif issubclass (field_type .type , Pointer ):
314314 item_parser = "_et.__new__(_et, e, stream, r)"
315315 list_comp = f"[{ item_parser } for e in { getter } ]"
@@ -329,13 +329,13 @@ def _generate_packed(self, fields: list[Field]) -> Iterator[str]:
329329 reads .append (f'r["{ field ._name } "] = { parser } ' )
330330 reads .append ("" ) # Generates a newline in the resulting code
331331
332- size += field_type .size
332+ size += field_type .__size__
333333
334334 fmt = _optimize_struct_fmt (info )
335335 if fmt == "x" or (len (fmt ) == 2 and fmt [1 ] == "x" ):
336336 unpack = ""
337337 else :
338- unpack = f'data = _struct(cls.cs .endian, "{ fmt } ").unpack(buf)\n '
338+ unpack = f'data = _struct(cls.__cs__ .endian, "{ fmt } ").unpack(buf)\n '
339339
340340 template = f"""
341341 buf = stream.read({ size } )
@@ -382,9 +382,9 @@ def _generate_struct_info(cs: cstruct, fields: list[Field], align: bool = False)
382382 # Other types are byte based
383383 # We don't actually unpack anything here but slice directly out of the buffer
384384 elif issubclass (read_type , (Char , Wchar , Int )):
385- yield field , count * read_type .size , "x"
385+ yield field , count * read_type .__size__ , "x"
386386
387- size = count * read_type .size
387+ size = count * read_type .__size__
388388 imaginary_offset += size
389389 if current_offset is not None :
390390 current_offset += size
0 commit comments