Skip to content

Feature idea: allow exposing read-only buffers #863

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

Closed
bmerry opened this issue May 18, 2017 · 8 comments
Closed

Feature idea: allow exposing read-only buffers #863

bmerry opened this issue May 18, 2017 · 8 comments

Comments

@bmerry
Copy link
Contributor

bmerry commented May 18, 2017

Issue description

This is something that occurred to me when reading the Python docs on memoryview, but not something I have any immediate need for - which is why I've labelled it a "feature idea" rather than a "feature request".

At present, buffer_info doesn't have any indication of whether the memory should be treated as read-only or read-write. There may be a use case for a class to expose a buffer while not allowing writes to it. It looks like it shouldn't be too hard to add a flag to buffer_info and then add check in pybind11_buffer to raise a BufferError if the request has PyBUF_WRITABLE is set but the exposed buffer is read-only.

@bmerry
Copy link
Contributor Author

bmerry commented May 18, 2017

Just spotted that this might be duplicated by #481, although it might not be quite the same thing.

@aldanor
Copy link
Member

aldanor commented May 18, 2017

Yes this makes sense, should be a simple flag addition; in fact I had it implemented in one of the branches at some point but it wasn't merged in.

I believe numpy should respect this flag as well (and set the writeable flag accordingly), but this better be double checked.

@lramati
Copy link

lramati commented Aug 13, 2017

Hi, I am trying to bind a project to Python using this library, and have just run in to this issue. I have an object with the function const void* get_data() that points to a couple megabytes of data. Right now I have to copy this into a std::vector<uint8_t> and send that to python, but this is very expensive. I was hoping to use py::buffer to get around this expensive copy, but can't because my data is read-only.

Can I have an explanation of how to modify pybind11 locally to expose this functionality, or an estimate of when this code might make it into the project?

Thanks

@complyue
Copy link

My +1 for I'm using pybind to expose mmap-ed array data to arm massive data on shared storage to multiple compute nodes, certainly majority of the mmaps are readonly.

If I don't touch pybind internal/detail like this:

  vector<array> bas; // bundle arrays
// ...
      // construct bundle array inplace
      bas.emplace_back(
          dt, // dtype
          // the explicit initializer_list construction is a necessary hint to compile,
          // plain {} won't work, even with explicit, element-wise type cast
          std::initializer_list<ssize_t>{scnt, slen}, // shape
          static_cast<void *>(mp + offset), // ptr
          mm // base
      );
      if (readonly_a) {
        // no public pybind api to manipulate array flags so far, have to use internal details until
        // https://github.com/aldanor/pybind11/pull/2 finds its way into release
        py::detail::array_proxy(bas.back().ptr())->flags &= ~py::detail::npy_api::NPY_ARRAY_WRITEABLE_;
      }

i.e. leave the array flags containing writeable, then accident write to such arrays will crash the process right away, rather than leave any hint for the problem like:

Traceback (most recent call last):
  File ".../interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-6-5b2103cfff02>", line 1, in <module>
    a[2]=1
ValueError: assignment destination is read-only

code-of-kpp pushed a commit to code-of-kpp/pybind11 that referenced this issue Jan 30, 2018
skoslowski added a commit to skoslowski/pybind11 that referenced this issue Jul 23, 2018
skoslowski added a commit to skoslowski/pybind11 that referenced this issue Jul 24, 2018
skoslowski added a commit to skoslowski/pybind11 that referenced this issue Sep 11, 2018
skoslowski added a commit to skoslowski/pybind11 that referenced this issue Sep 11, 2018
skoslowski added a commit to skoslowski/pybind11 that referenced this issue Nov 16, 2018
skoslowski added a commit to skoslowski/pybind11 that referenced this issue Nov 23, 2018
@SimonRodrigue43
Copy link

Sebastian, what is the state of your modifications? Is it completed, tested and working? Could it be put in PR for merging?
Thanks.

@skoslowski
Copy link
Contributor

There is a PR already: #1466
Concerning the state: This was an attempt to upstream some of the patches I have on a private fork. I never got any feedback on whether this would be considered for a merge. So, I eventually stopped updating the PR. If there is interest now, I'd be happy to continue.

@SimonRodrigue43
Copy link

This feature is very important in our situation because in some cases, buffer memory must stay immutable and a write could result in a crash. I also notice that we're not alone having that need by reading other comments.
@wjakob, is there some concerns with PR #1466 that could prevent it to be merged at some point?

@wjakob
Copy link
Member

wjakob commented Nov 24, 2019

This PR is now merged, closing the ticket.

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

7 participants