-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Expand std::string_view support to str, bytes, memoryview #3521
Conversation
fd737d5
to
d7c5d27
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
The 2.7 ubuntu failure probably just needs a leading I've never seen the ITERATOR_DEBUG_LEVEL failure before. |
0bf48b2
to
0c7d446
Compare
Squashed it. |
@rwgk Does this pass the Google Test suite? |
I don't understand why the 3.6 - windows-latest build here started failing. Is that a known flakey build or something? |
Yes, that's our most common known flake, safe to ignore. It's a lot better than it used to be before PR #2995, but it's still not stable. There are other common flakes: 1. print from destructor; 2. various transient issues installing or downloading dependencies. |
I'll initiate this now (will take several hours). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy when Google's happy. :)
I missed the 12:00 global testing opportunity, but got this PR into the 16:00 batch. Results are expected about 4-5 hours later. I did comprehensive manual testing before sending this PR for global testing, including testing with ASAN. No issues. |
This PR didn't make it through the basic "smoke check" :-( EDIT: What I had in mind was a PyCLIF issue, not pybind11: google/clif@ef5fa11 EDIT: This is the Error message:
While compiling:
|
1. Allows constructing a str or bytes implicitly from a string_view; this is essentially a small shortcut allowing a caller to write `py::bytes{sv}` rather than `py::bytes{sv.data(), sv.size()}`. 2. Allows implicit conversion *to* string_view from py::bytes -- this saves a fair bit more as currently there is no simple way to get such a view of the bytes without copying it (or resorting to Python API calls). (This is not done for `str` because when the str contains unicode we have to allocate to a temporary and so there might not be some string data we can properly view without owning.) 3. Allows `memoryview::from_memory` to accept a string_view. As with the other from_memory calls, it's entirely your responsibility to keep it alive. This also required moving the string_view availability detection into detail/common.h because this PR needs it in pytypes.h, which is higher up the include chain than cast.h where it was being detected currently.
This change is known to fix the `tensorflow::tstring` issue reported under pybind#3521 (comment) TODO: Minimal reproducer for the `tensorflow::tstring` issue.
c207063
to
add6628
Compare
Trying a fix. I don't know if this is the best approach, but at least it fixes the failures in the wild (at least some; I still have to try the global testing again). The force push was needed because I also rebased on master. |
Error without the enable_if trick: ``` /usr/local/google/home/rwgk/forked/pybind11/tests/test_builtin_casters.cpp:169:16: error: ambiguous conversion for functional-style cast from 'TypeWithBothOperatorStringAndStringView' to 'py::bytes' return py::bytes(TypeWithBothOperatorStringAndStringView()); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/detail/../pytypes.h:1174:5: note: candidate constructor bytes(const std::string &s) : bytes(s.data(), s.size()) { } ^ /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/detail/../pytypes.h:1191:5: note: candidate constructor bytes(std::string_view s) : bytes(s.data(), s.size()) { } ^ ```
I think this will work now, pending full global testing; just initiated. Results when I wake up (hopefully). I already verified that all failing targets from the previous sample run pass with the latest version of this PR. |
|
I'd kind of prefer that, in the case of ambiguous conversions, we were preferring string_view to string (because it likely avoids an extra allocation), but I don't really see a nice way to do that without introducing potential breakage for existing code. |
Initially I tried |
The one CI failure is a flake (Run jwlawson/actions-setup-cmake@v1.11 Error: connect ETIMEDOUT 140.82.114.6:443). Google-global testing was successful (it did not run into the Definitely good to merge Jason! |
Description
Allows constructing a str or bytes implicitly from a string_view; this is essentially a small shortcut allowing a caller to write
py::bytes{sv}
rather thanpy::bytes{sv.data(), sv.size()}
.For
py::str
this also allowsstd::u8string_view
, but not forpy::bytes
because that didn't seem entirely appropriate to me.Allows implicit conversion to
std::string_view
frompy::bytes
—this plugs a current hole where there's no simple way to get such a view of the bytes without copying it (or resorting to Python API calls).(This is not done for
str
because when the str contains unicode we have to allocate to a temporary and so there might not be some string data we can properly view without owning.)Allows
memoryview::from_memory
to accept a string_view. As with the other from_memory calls, it's entirely your responsibility to keep it alive.This also required moving the string_view availability detection into detail/common.h because this PR needs it in pytypes.h, which is higher up the include chain than cast.h where it was being detected currently.
Suggested changelog entry:
* Make str/bytes/memoryview more interoperable with ``std::string_view``.