PyO3 / PyO3/pyo3

Add method to check PyCapsule name while getting pointer

Open
#3,584 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing PyCapsule::pointer() and name() APIs in the Rust bindings, then compare them with the proposed TypedPyCapsule and get_pointer() usage. Done means the API can validate a user-provided capsule name and reject null pointers while providing the proposed typed access pattern.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.