Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand std::string_view support to str, bytes, memoryview#3521
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
91e2cb9
Expand string_view support to str, bytes, memoryview
jagerman f56dc86
Move string_view include to pytypes.h
jagerman add6628
CI-testing a fix for the "ambiguous conversion" issue.
rwgk 7f7ddaa
Make clang-tidy happy (hopefully).
rwgk 16e178b
Adding minimal reproducer for the `tensorflow::tstring` issue.
rwgk 3428195
Adding missing NOLINTNEXTLINE
rwgk 5709ccf
Also apply ambiguous conversion workaround to str()
jagerman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -18,6 +18,10 @@ | ||
| # include <optional> | ||
| #endif | ||
| #ifdef PYBIND11_HAS_STRING_VIEW | ||
| # include <string_view> | ||
| #endif | ||
| PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) | ||
| /* A few forward declarations */ | ||
| @@ -1085,6 +1089,20 @@ class str : public object { | ||
| // NOLINTNEXTLINE(google-explicit-constructor) | ||
| str(const std::string &s) : str(s.data(), s.size()) { } | ||
| #ifdef PYBIND11_HAS_STRING_VIEW | ||
| // enable_if is needed to avoid "ambiguous conversion" errors (see PR #3521). | ||
| template <typename T, detail::enable_if_t<std::is_same<T, std::string_view>::value, int> = 0> | ||
| // NOLINTNEXTLINE(google-explicit-constructor) | ||
| str(T s) : str(s.data(), s.size()) { } | ||
| # ifdef PYBIND11_HAS_U8STRING | ||
| // reinterpret_cast here is safe (C++20 guarantees char8_t has the same size/alignment as char) | ||
| // NOLINTNEXTLINE(google-explicit-constructor) | ||
| str(std::u8string_view s) : str(reinterpret_cast<const char*>(s.data()), s.size()) { } | ||
| # endif | ||
| #endif | ||
| explicit str(const bytes &b); | ||
| /** \rst | ||
| @@ -1167,6 +1185,26 @@ class bytes : public object { | ||
| pybind11_fail("Unable to extract bytes contents!"); | ||
| return std::string(buffer, (size_t) length); | ||
| } | ||
| #ifdef PYBIND11_HAS_STRING_VIEW | ||
| // enable_if is needed to avoid "ambiguous conversion" errors (see PR #3521). | ||
| template <typename T, detail::enable_if_t<std::is_same<T, std::string_view>::value, int> = 0> | ||
| // NOLINTNEXTLINE(google-explicit-constructor) | ||
| bytes(T s) : bytes(s.data(), s.size()) { } | ||
| // Obtain a string view that views the current `bytes` buffer value. Note that this is only | ||
| // valid so long as the `bytes` instance remains alive and so generally should not outlive the | ||
| // lifetime of the `bytes` instance. | ||
| // NOLINTNEXTLINE(google-explicit-constructor) | ||
| operator std::string_view() const { | ||
jagerman marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| char *buffer = nullptr; | ||
| ssize_t length = 0; | ||
| if (PYBIND11_BYTES_AS_STRING_AND_SIZE(m_ptr, &buffer, &length)) | ||
| pybind11_fail("Unable to extract bytes contents!"); | ||
jagerman marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return {buffer, static_cast<size_t>(length)}; | ||
| } | ||
| #endif | ||
| }; | ||
| // Note: breathe >= 4.17.0 will fail to build docs if the below two constructors | ||
| // are included in the doxygen group; close here and reopen after as a workaround | ||
| @@ -1714,6 +1752,13 @@ class memoryview : public object { | ||
| static memoryview from_memory(const void *mem, ssize_t size) { | ||
| return memoryview::from_memory(const_cast<void*>(mem), size, true); | ||
| } | ||
| #ifdef PYBIND11_HAS_STRING_VIEW | ||
| static memoryview from_memory(std::string_view mem) { | ||
| return from_memory(const_cast<char*>(mem.data()), static_cast<ssize_t>(mem.size()), true); | ||
| } | ||
| #endif | ||
| #endif | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.