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

Safely access data in PyUntypedArray #477

Open
flying-sheep opened this issue Jan 16, 2025 · 0 comments
Open

Safely access data in PyUntypedArray #477

flying-sheep opened this issue Jan 16, 2025 · 0 comments

Comments

@flying-sheep
Copy link

flying-sheep commented Jan 16, 2025

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.

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

No branches or pull requests

1 participant