Add minimal C-api implementation that builds with Pyo3#7562
Add minimal C-api implementation that builds with Pyo3#7562bschoenmaeckers wants to merge 24 commits intoRustPython:mainfrom
Conversation
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Could you try with hpy if you dont mind? We are looking for sustainable way |
That would require significant changes in codebase that are using the CPython api spec. I would like to explore the possibility to use the ab3/abi3t api. Would you be willing to accept that? |
youknowone
left a comment
There was a problem hiding this comment.
This way will not work.
Just in case, the code can be generated, but I hope we carefully review the decisions. unlikely-to-happen decisions must be reviewed and justified by document(comment) the decisions.
If you are not familiar enough to the project to decide good way, please start from a smaller and simpler issue.
We only need abi3t, because we are compatible to free-threading.
We have to minimize the surface of C API. if HPy helps it, we need HPy.
Ideally c api must be very thin wrappers to RustPython features. "code" must not be included in this crate.
crates/capi/src/object.rs
Outdated
| use std::sync::LazyLock; | ||
|
|
||
| pub struct PyTypeObject { | ||
| ty: LazyLock<&'static Py<PyType>>, |
There was a problem hiding this comment.
This will cause significant performance draw down.
PyType also already has flags inside.
Will you create all the new type wrapper for every static types? That will not work.
There was a problem hiding this comment.
Will you create all the new type wrapper for every static types? That will not work.
I was just trying to get the static type objects working. These are static exported pointers so I came up with this. If you have a better idea to statically create pointers to the TypeZoo, please, I'm very open to suggestions.
There was a problem hiding this comment.
PyType also already has flags inside.
I got rid of the flags field in PyTypeObject, and map the RustPythons PyTypeFlags to the format PyType_GetFlags expects. Note that this does currently not map all the available flags.
There was a problem hiding this comment.
The static type pointers are now dynamically set at runtime. They are now basically a static pointer to a Py<PyType> instance. What do you think?
There was a problem hiding this comment.
Good try 👍 That will be better.
| use std::cell::RefCell; | ||
|
|
||
| thread_local! { | ||
| static VM: RefCell<Option<ThreadedVirtualMachine>> = const { RefCell::new(None) }; |
There was a problem hiding this comment.
C API must be simple API set. It must not contains any state ideally. And never the VM.
There was a problem hiding this comment.
The VM_CURRENT static in vm/thread.rs is scoped. So I cannot set and unset it in PyGILState_Ensure/ PyGILState_Release. (These methods have nothing to do with the GIL in free-threaded world despite GIL in the name)
There was a problem hiding this comment.
I could use VM_STACK instead... 🤔
There was a problem hiding this comment.
Indeed. Let's try to expose it
There was a problem hiding this comment.
VM_STACK only stores pointers (NonNull<VirtualMachine>) to a vm, so I need some other place to store it. What do you recommend?
I wanted to create as little of modificaties to the existing code base without significant motivation. But I definitely agree with you. |
| let (response_tx, response_rx) = mpsc::channel(); | ||
| tx.send(response_tx).expect("Failed to send VM request"); | ||
| response_rx.recv().expect("Failed to receive VM response") | ||
| } |
There was a problem hiding this comment.
This is also somewhat cursed. I want to be able to create ThreadedVirtualMachine scoped to the thread where PyGILState_Ensure is called. This may be from a different thread than where the main interpreter is running. So I need a way to share the Interpreter between different threads.
That's actually a good view in general. In this case, C APIs must be jsut C API for real RustPython features. |
16f8074 to
850cd07
Compare
This is my shot at implementing a minimal Cpython compatible C-api. I've implemented the bare minimum to get the included Pyo3 example running where most of the api is stubbed. I'm not familiar with the rest of the RustPython code base so let me know what you think and where I did stupid things.
Please take extra care reviewing the pylifecycle.rs & pystate.rs files where I try to setup the RustPython interpreter.
xref #5604