Skip to content

Introspection APIs#231

Draft
mildsunrise wants to merge 1 commit intopython-cffi:mainfrom
mildsunrise:introspection-apis
Draft

Introspection APIs#231
mildsunrise wants to merge 1 commit intopython-cffi:mainfrom
mildsunrise:introspection-apis

Conversation

@mildsunrise
Copy link

@mildsunrise mildsunrise commented Feb 24, 2026

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]: Like list_types() but for enum types (I'd extend the tuple returned by list_types() but I figured we wouldn't want to break back compatibility).

  • ffi.list_globals() -> list[CGlobal]: Lists defined globals as CGlobal objects, which expose the kind of global (int_constant, enum, constant, variable, function, python_function), their 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 that dlopen would 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 FFIBuilder as well.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant