pybind / pybind/pybind11

[QUESTION] subscripting pybind11::array_t without bounds check and without using proxy

Open
#2,600 20 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

hello,
I am trying to subscript a pybind11::array_t<scalar_t, pybind11::array::c_style> or pybind11::array_t<scalar_t, pybind11::array::f_style>. I know I can use at() or mutable_at but those imply a check on the bounds.
I also know I can use the proxy but for my use case I have a wrapped class of pybind11::array_t and would like to not rely on the proxy since I am seeing some issues if I try to keep the proxy as member of my wrapper class.
So basically i would like to do something like:

template<typename scalar_t>
class MyWrapper
{
  MyWrapper(std::size_t extent) 
  : data_(extent), mutProxy(data_), uMutProxy(...)
public:
  scalar_t & operator[](std:size_t i){ data_.mP_(i); }
  scalar_t const & operator[](std:size_t i) const{ data_.uP_(i); }
  pbv_t * data() { return &data_; }
  pbv_t const * data() const { return &data_; }

private:
  using pbv_t = pybind11::array_t<scalar_t, pybind11::array::f_style>;
  using mp_t = decltype( std::declval<pbv_t &>().mutable_unchecked() );
  using up_t = decltype( std::declval<const pbv_t &>().unchecked() );

  pbv_t data_;
  mp_t mP_;
  up_t uP_;
}

Are there corresponding methods for at mutable_at or other ways I can subscript the native array that do not check bounds but that avoid the proxy? I looked at the source code and did not find anything.
Thanks

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 pybind11::array_t entry points named in the issue: at(), mutable_at(), unchecked(), and mutable_unchecked(). Determine whether an existing native-array access path can avoid bounds checks without retaining a proxy, and document the supported approach or the API change required. No file or test is named in the issue, so the relevant implementation and coverage would need to be located first.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
api
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.