-
Notifications
You must be signed in to change notification settings - Fork 778
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
Expose PyCapsule API #1193
Comments
But isn't |
That's one use, but it's more general (from the docstring):
|
Yep exactly. Numpy uses it to share pointers to api functions; we can stash whatever we want in capsules 😄 |
The cpython crate has already exposed the PyCapsule API, we can take inspiration from it: https://docs.rs/cpython/0.6.0/cpython/struct.PyCapsule.html |
Perhaps. Some things we might be able to extend about that API:
I was imagining we'd want an api roughly like this: impl PyCapsule {
pub fn new(py: Python, value: Box<T>, name: CString) -> &Self;
pub fn new_with_static(py: Python, value: &'static T, name: CString) -> &Self;
pub fn new_with_destructor(py: Python, value: Box<T>, name: CString, destructor: impl FnOnce(Box<T>)) -> &Self;
pub fn name(&self) -> CString;
pub fn pointer(&self, name: &CStr) -> *mut std::os::raw::c_void;
} Usage of |
For reference I've also used the |
The capsule API (https://docs.python.org/3.8/c-api/capsule.html) can be useful for safely storing Rust data in a way that's not usable by Python without leaking it. This is possible because the capsule creation takes a destructor function.
Today it came up on Gitter that for
rust-numpy
it may be useful to do zero-copy transfer of arrays from Rust to Python, by stashing the backing storage (e.g. vec) in a capsule which is set as the base object to the numpy array.The text was updated successfully, but these errors were encountered: