2828TYPEDEFS = {}
2929"""A cache for parsed type definitions, indexed by name."""
3030
31+
3132def definition_to_type (definition : str ) -> type [obj ]:
3233 """Converts a C struct definition to a struct object."""
3334 parser = c_parser .CParser ()
@@ -39,7 +40,9 @@ def definition_to_type(definition: str) -> type[obj]:
3940 try :
4041 ast = parser .parse (definition )
4142 except c_parser .ParseError as e :
42- raise ValueError ("Invalid definition. Please add the necessary includes if using non-standard type definitions." ) from e
43+ raise ValueError (
44+ "Invalid definition. Please add the necessary includes if using non-standard type definitions."
45+ ) from e
4346
4447 # We assume that the root declaration is the last one.
4548 root = ast .ext [- 1 ].type
@@ -122,7 +125,11 @@ def arr_to_type(arr: c_ast.ArrayDecl) -> type[obj]:
122125
123126def type_decl_to_type (decl : c_ast .TypeDecl , parent : c_ast .Struct | None = None ) -> type [obj ]:
124127 """Converts a C type declaration to a type."""
125- if not isinstance (decl , c_ast .TypeDecl ) and not isinstance (decl , c_ast .PtrDecl ) and not isinstance (decl , c_ast .ArrayDecl ):
128+ if (
129+ not isinstance (decl , c_ast .TypeDecl )
130+ and not isinstance (decl , c_ast .PtrDecl )
131+ and not isinstance (decl , c_ast .ArrayDecl )
132+ ):
126133 raise TypeError ("Definition must be a type declaration." )
127134
128135 if isinstance (decl , c_ast .PtrDecl ):
@@ -158,7 +165,7 @@ def to_uniform_name(name: str) -> str:
158165 """Converts a name to a uniform name."""
159166 name = name .replace ("unsigned" , "u" )
160167 name = name .replace ("_Bool" , "bool" )
161- name = name .replace ("uchar" , "ubyte" ) # uchar is not a valid ctypes type
168+ name = name .replace ("uchar" , "ubyte" ) # uchar is not a valid ctypes type
162169
163170 # We have to convert each intX, uintX, intX_t, uintX_t to the original char, short etc.
164171 name = name .replace ("uint8_t" , "ubyte" )
@@ -184,15 +191,15 @@ def expand_includes(definition: str) -> str:
184191 f .write (definition )
185192 f .flush ()
186193
187- result = subprocess .run (["cc" , "-std=c99" , "-E" , f .name ], capture_output = True , text = True , check = True ) # noqa: S607
194+ result = subprocess .run (["cc" , "-std=c99" , "-E" , f .name ], capture_output = True , text = True , check = True ) # noqa: S607
188195
189196 return result .stdout
190197
191198
192199def cleanup_attributes (definition : str ) -> str :
193200 """Cleans up attributes in a C definition."""
194201 # Remove __attribute__ ((...)) from the definition.
195- pattern = r"__attribute__\s*\(\((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*\)\)" # ChatGPT provided this, don't ask me
202+ pattern = r"__attribute__\s*\(\((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*\)\)" # ChatGPT provided this, don't ask me
196203 return re .sub (pattern , "" , definition )
197204
198205
0 commit comments