PyO3 / PyO3/rust-numpy

Safely access data in PyUntypedArray

Open
#477 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
1.4k
Forks
141
Avg merge
16m
Merged PRs (30d)
3

Description

PyUntypedArrayMethods::as_array_ptr doesn’t describe under which circumstances the returned pointer is convertible into a reference.

Since the requirements are rather hairy, it would be great to have a way to access the data safely without going through unsafe. Would it e.g. be possible to have this?

trait PyUntypedArrayMethods<'py> {
    ...
    fn bytes<'a>(&'a self) -> &'a [u8] {
        let ptr = self.as_array_ptr();
        let n_bytes = self.len() * self.dtype().itemsize();
        unsafe {
            let p = (*ptr).data.cast();
            slice::from_raw_parts(p, n_bytes)
        }
    }
}

Because we currently implement this ourselves, but without any of the aforementioned safety information, we can’t really be sure if it’s safe.

Contributor guide

No contributing guide indexed for this repository

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 at PyUntypedArrayMethods::as_array_ptr and read the Rust pointer-to-reference conversion documentation cited in the issue. Determine the existing pointer’s safety guarantees and evaluate the proposed bytes accessor, including what documentation or tests are needed to show that callers can access the data safely without their own unsafe code.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, rust
Domain
api, backend-api-design, data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.