-
Notifications
You must be signed in to change notification settings - Fork 1
sample cbindgen #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sample cbindgen #20
Conversation
I expect not as-is, but clinic could be modified to handle cbindgen generated headers, so I wouldn't worry to much about that. Independently of that however, I may have been unclear when saying I'd want a proof of concept, so sorry for being unclear! I may be misunderstanding cbindgen, but I think if we'd use cbindgen, we'd want to use it so we can define types in Rust? In other words, define PyObject, PyMethodDef, etc. in Rust, then generate the header to be used by CPython. cc @Gankra (hopefully you don't mind the ping!) As-is I don't think we need
It doesn't need to know the signature nor name of the module initialization function as those are pre-defined (see https://docs.python.org/3.15/c-api/extension-modules.html#c.PyInit_modulename). Sorry again for the confusion! |
|
This is a third option of question in pre-PEP: What about Argument Clinic? Rather than either integrating Rust support into clinic or creating a Rust procedural macro capable of parsing a similar DSL, generating C header is totally possible and clinic can use it as C input as before. This generation is only will be useful for clinic and no usage out of it. (Please let me know if clinic doesn't work in that way) Because type and function generation have different dependencies and benefits, it is better to think of them as “cbindgen types” and “cbindgen functions”, rather than everything together simply as “cbindgen.” This is about cbindgen functions for clinic.
This is also totally right. And about "cbindgen types". They can be fully separated and have its own profits of reliable type definitions. |
Aha! That makes much more sense. Thank you for the explanation, that helped a lot. So this would allow us to automatically generate the The only other part of this that isn't clear to me (sorry for having so many questions!) is how we'd tie this back to the Rust module definition. Clinic works by parsing the comments above implementation functions and generating both a function handling argument parsing, and also a macro for the For an example, the I suppose we could modify clinic to either output Rust code, or output to a C file instead of a header? |
|
Thank you! Thanks to your explanation, I realized that I had been making an incorrect assumption about the role and behavior of clinic. _sha2_sha256 must have a C ABI, but _sha2_sha256_impl is preferably a Rust function for better ergonomics. As you said, applying this without modifying clinic would be difficult. Since cbindgen only works on If the goal is minimally patching clinic, it must be patched to take Rust file as input but still can produce a C (header) file as output, that would seem like a viable solution. On the Rust side, if we define |
That header wouldn't be compiled into anything however. Right now clinic only works for the sha2 module because |
|
That's right. making proper Rust output looks a lot better. Thank you for the review! |
|
Thank you for talking through the solution with me and your patience! |
|
(Everything y'all discussed makes sense, and yes totally fine to ping me!) |
Please check if you can use Modules/_base64/_base64.rs.h for clinic input