-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[FEAT] Get a read-only view of the data in py::bytes #2517
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
Comments
This may be an alternative solution to #1807 – instead of writing functions that take
As they say,
|
From the user's perspective, you can currently achieve this via inline std::basic_string_view<uint8_t> bytes_view(py::bytes bytes) {
py::buffer_info info(py::buffer(bytes).request());
return { as_unsigned(info.ptr), as_unsigned(info.len) };
} ... But there is some overhead in acquiring + releasing the It might be nice to have /* XXX: guard error return with pybind11_fail */
const char *data() const { return PYBIND11_BYTES_AS_STRING(m_ptr); }
size_t size() const { return static_cast<size_t>(PYBIND11_BYTES_SIZE(m_ptr)); } |
It would make sense, yes. It's still up for discussion whether this ought to be a conversion operator or some full method (cfr. your |
Uh oh!
There was an error while loading. Please reload this page.
Looking at the pybind11::bytes class (in pytypes.h), it is not obvious how to get the data out without first making an unnecessary std::string copy of it.
This function is not terribly useful:
More useful:
The text was updated successfully, but these errors were encountered: