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

Add method to check PyCapsule name while getting pointer #3584

Open
wjones127 opened this issue Nov 18, 2023 · 1 comment
Open

Add method to check PyCapsule name while getting pointer #3584

wjones127 opened this issue Nov 18, 2023 · 1 comment

Comments

@wjones127
Copy link
Contributor

wjones127 commented Nov 18, 2023

PyCapsule::pointer() passes the name of the PyCapsule itself, instead of passing a user-provided name. This means users must perform the validation of the PyCapsule name themselves.

let capsule: PyCapsule = object.extract(py)?;
if capsule.name() != expected_name {
    return PyValueError::new_err("Capsule does not have expected name");
}
let pointer = capsule.pointer() as *mut MyDataType;
if pointer.is_null() {
    return PyValueError::new_err("Capsule pointer was null");
}
// do something with pointer.

It might be nice to instead have an API that is more strongly typed and convenient:

let capsule = unsafe { TypedPyCapsule::<MyDataType>::from_object(obj, expected_name)? }; // does name check
let pointer: NonNull<MyDataType> = capsule.get_pointer()?; // does null check
@wjones127 wjones127 changed the title Add PyCapsule::import_mut as mutable parallel to PyCapsule::import Add method to check PyCapsule name while getting pointer Nov 18, 2023
@wjones127
Copy link
Contributor Author

wjones127 commented Nov 18, 2023

Substantial discussion was had in this PR: #3585

I'll implement this as a utility wrapper in upstream projects and meanwhile I'll let the project maintainers here think about whether this is something that would make sense in PyO3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant