Skip to content
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

feat: check name when getting PyCapsule pointer #3585

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3585.added.md
@@ -0,0 +1 @@
Add `PyCapsule::import_mut()` to import a mutable reference to capsule contents.
30 changes: 27 additions & 3 deletions src/types/capsule.rs
Expand Up @@ -123,8 +123,9 @@ impl PyCapsule {

/// Imports an existing capsule.
///
/// The `name` should match the path to the module attribute exactly in the form
/// of `"module.attribute"`, which should be the same as the name within the capsule.
/// If this capsule represents a module attribute, the `name` should match the path
/// to the module attribute exactly in the form of `"module.attribute"`, which should
/// be the same as the name within the capsule.
Comment on lines -126 to +128
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt this paragraph was oddly prescriptive, given there are other use cases for PyCapsules than module attributes. (For example, we use them in Arrow to transport Array in the Arrow PyCapsule Interface.) If you feel otherwise, I can revert this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It comes from the documentation for PyCapsule_Import

Based on your adjustment, you wanted to clarify that capsules can be used for things other than module attributes. This is true, though this function only succeeds for module attributes as far as I am aware?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see what you are saying. I was looking at this because it's the only API where you can pass a name and have that validated. So instead, I think we want to call GetPointer, right?

///
/// # Safety
///
Expand All @@ -138,6 +139,23 @@ impl PyCapsule {
}
}

/// Imports an existing capsule, returning a mutable reference to the value.
///
/// # Safety
///
/// It must be known that the capsule imported by `name` contains an item of type `T`.
///
/// It must be known that the capsule will not be accessed by any other code
/// while the mutable reference is alive.
pub unsafe fn import_mut<'py, T>(py: Python<'py>, name: &CStr) -> PyResult<&'py mut T> {
let ptr = ffi::PyCapsule_Import(name.as_ptr(), false as c_int);
if ptr.is_null() {
Err(PyErr::fetch(py))
} else {
Ok(&mut *(ptr as *mut T))
}
}

/// Sets the context pointer in the capsule.
///
/// Returns an error if this capsule is not valid.
Expand Down Expand Up @@ -398,10 +416,16 @@ mod tests {
let wrong_name = CString::new("builtins.non_existant").unwrap();
let result: PyResult<&Foo> = unsafe { PyCapsule::import(py, wrong_name.as_ref()) };
assert!(result.is_err());
let result: PyResult<&mut Foo> =
unsafe { PyCapsule::import_mut(py, wrong_name.as_ref()) };
assert!(result.is_err());

// corret name is okay.
// correct name is okay.
let cap: &Foo = unsafe { PyCapsule::import(py, name.as_ref())? };
assert_eq!(cap.val, 123);
let cap: &mut Foo = unsafe { PyCapsule::import_mut(py, name.as_ref())? };
assert_eq!(cap.val, 123);

Ok(())
})
}
Expand Down