Introspection APIs by mildsunrise · Pull Request #231 · python-cffi/cffi
This PR documents the existing introspection API (CType, CField) more thoroughly, and adds some new APIs that would be very useful for a variety of reasons:
-
ffi.list_enums() -> list[str]: Likelist_types()but for enum types (I'd extend the tuple returned bylist_types()but I figured we wouldn't want to break back compatibility). -
ffi.list_globals() -> list[CGlobal]: Lists defined globals asCGlobalobjects, which expose the name and kind of global (int_constant,enum,constant,variable,function,python_function), its numerical value (for integer constants) or the CType that its value would have (for the rest). Note that this is defined on ffi, and thus allows introspecting the API thatdlopenwould have without needing a suitable library that exposes all symbols. -
ffi.includes -> tuple[FFI]: FFI instances of the (directly) included libraries, if any, allowing users to track inherited globals too.
This is fully functional, but I made it a draft since I'd like to gather some initial feedback, and (ideally) write tests and replicate these APIs in cffi.api as well (done).
There are some minor things that would be nice to have (for example make the parsing layer preserve which particular typedef, if any, was used to refer to a type, as well as documentation attached to globals, fields and types). But for now, with these introspection APIs it should be feasible to address #230.
WDYT?