Skip to content

Use generated ctypes wrappers in Python/C API tests#16068

Open
jakelishman wants to merge 3 commits intoQiskit:mainfrom
jakelishman:c/ctypes/2
Open

Use generated ctypes wrappers in Python/C API tests#16068
jakelishman wants to merge 3 commits intoQiskit:mainfrom
jakelishman:c/ctypes/2

Conversation

@jakelishman
Copy link
Copy Markdown
Member

@jakelishman jakelishman commented Apr 20, 2026

Now we have generated ctypes wrappers, we don't need to manually redefine the wrapper objects. With the argument type definitions being more structured now, it is simpler to specify out variables with the pointed-to type, then pass it with ctypes.byref, rather than trying to construct the reference type and pass it by value; the strict pointer-type matching of ctypes makes byref more reliable.

AI/LLM disclosure

  • I didn't use LLM tooling, or only used it privately.
  • I used the following tool to help write this PR description:
  • I used the following tool to generate or modify code:

This is minor code reorganisation to avoid the `cext` build script
getting out of hand; we're about to add an extra `ctypes`-like backend
to it, which coudl do with being in its own module.
@jakelishman jakelishman requested a review from a team as a code owner April 20, 2026 16:04
@qiskit-bot
Copy link
Copy Markdown
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@jakelishman jakelishman added type: qa Issues and PRs that relate to testing and code quality Changelog: None Do not include in the GitHub Release changelog. on hold Can not fix yet labels Apr 20, 2026
@jakelishman jakelishman added this to the 2.5.0 milestone Apr 20, 2026
jakelishman and others added 2 commits April 20, 2026 17:38
This is similar to how Python exposes its own C API through `ctypes` as
`ctypes.pythonapi`.  It's unlikely to be immediately useful as a feature
for downstream users, but it simplifies the process of us testing our
own code from Python space.

I had originally hoped / thought that `numba` would support calling
these `ctypes` functions, but while `numba` (as of current version 0.65)
does have support for calling `ctypes` functions defined purely in terms
of primitive non-pointer integral types, it doesn't yet support
`ctypes.py_object` or arbitrary pointers.
Now we have generated `ctypes` wrappers, we don't need to manually
redefine the wrapper objects.  With the argument type definitions being
more structured now, it is simpler to specify out variables with the
pointed-to type, then pass it with `ctypes.byref`, rather than trying
to construct the reference type and pass it by value; the strict
pointer-type matching of `ctypes` makes `byref` more reliable.
@ShellyGarion ShellyGarion added the C API Related to the C API label Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C API Related to the C API Changelog: None Do not include in the GitHub Release changelog. on hold Can not fix yet type: qa Issues and PRs that relate to testing and code quality

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

3 participants