-
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
Fix casters of STL containers with char*
#2303
Draft
YannickJadoul
wants to merge
1
commit into
pybind:master
Choose a base branch
from
YannickJadoul:stl-container-with-char-ptr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -73,8 +73,11 @@ template <typename Type, typename Key> struct set_caster { | |
return false; | ||
auto s = reinterpret_borrow<pybind11::set>(src); | ||
value.clear(); | ||
subcasters.clear(); | ||
subcasters.reserve(s.size()); | ||
for (auto entry : s) { | ||
key_conv conv; | ||
subcasters.emplace_back(); | ||
auto &conv = subcasters.back(); | ||
if (!conv.load(entry, convert)) | ||
return false; | ||
value.insert(cast_op<Key &&>(std::move(conv))); | ||
|
@@ -96,6 +99,9 @@ template <typename Type, typename Key> struct set_caster { | |
} | ||
|
||
PYBIND11_TYPE_CASTER(type, _("Set[") + key_conv::name + _("]")); | ||
|
||
private: | ||
std::vector<key_conv> subcasters; | ||
}; | ||
|
||
template <typename Type, typename Key, typename Value> struct map_caster { | ||
|
@@ -107,9 +113,13 @@ template <typename Type, typename Key, typename Value> struct map_caster { | |
return false; | ||
auto d = reinterpret_borrow<dict>(src); | ||
value.clear(); | ||
subcasters.clear(); | ||
subcasters.reserve(d.size()); | ||
for (auto it : d) { | ||
key_conv kconv; | ||
value_conv vconv; | ||
subcasters.emplace_back(); | ||
auto &conv_pair = subcasters.back(); | ||
auto &kconv = conv_pair.first; | ||
auto &vconv = conv_pair.second; | ||
if (!kconv.load(it.first.ptr(), convert) || | ||
!vconv.load(it.second.ptr(), convert)) | ||
return false; | ||
|
@@ -138,6 +148,9 @@ template <typename Type, typename Key, typename Value> struct map_caster { | |
} | ||
|
||
PYBIND11_TYPE_CASTER(Type, _("Dict[") + key_conv::name + _(", ") + value_conv::name + _("]")); | ||
|
||
private: | ||
std::vector<std::pair<key_conv, value_conv>> subcasters; | ||
}; | ||
|
||
template <typename Type, typename Value> struct list_caster { | ||
|
@@ -149,8 +162,11 @@ template <typename Type, typename Value> struct list_caster { | |
auto s = reinterpret_borrow<sequence>(src); | ||
value.clear(); | ||
reserve_maybe(s, &value); | ||
subcasters.clear(); | ||
subcasters.reserve(s.size()); | ||
for (auto it : s) { | ||
value_conv conv; | ||
subcasters.emplace_back(); | ||
auto &conv = subcasters.back(); | ||
if (!conv.load(it, convert)) | ||
return false; | ||
value.push_back(cast_op<Value &&>(std::move(conv))); | ||
|
@@ -181,6 +197,9 @@ template <typename Type, typename Value> struct list_caster { | |
} | ||
|
||
PYBIND11_TYPE_CASTER(Type, _("List[") + value_conv::name + _("]")); | ||
|
||
private: | ||
std::vector<value_conv> subcasters; | ||
}; | ||
|
||
template <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>> | ||
|
@@ -214,9 +233,12 @@ template <typename ArrayType, typename Value, bool Resizable, size_t Size = 0> s | |
auto l = reinterpret_borrow<sequence>(src); | ||
if (!require_size(l.size())) | ||
return false; | ||
subcasters.clear(); | ||
subcasters.reserve(l.size()); | ||
size_t ctr = 0; | ||
for (auto it : l) { | ||
value_conv conv; | ||
subcasters.emplace_back(); | ||
auto &conv = subcasters.back(); | ||
if (!conv.load(it, convert)) | ||
return false; | ||
value[ctr++] = cast_op<Value &&>(std::move(conv)); | ||
|
@@ -238,6 +260,9 @@ template <typename ArrayType, typename Value, bool Resizable, size_t Size = 0> s | |
} | ||
|
||
PYBIND11_TYPE_CASTER(ArrayType, _("List[") + value_conv::name + _<Resizable>(_(""), _("[") + _<Size>() + _("]")) + _("]")); | ||
|
||
private: | ||
std::vector<value_conv> subcasters; | ||
}; | ||
|
||
template <typename Type, size_t Size> struct type_caster<std::array<Type, Size>> | ||
|
@@ -278,7 +303,6 @@ template<typename T> struct optional_caster { | |
} else if (src.is_none()) { | ||
return true; // default-constructed value is already empty | ||
} | ||
value_conv inner_caster; | ||
if (!inner_caster.load(src, convert)) | ||
return false; | ||
|
||
|
@@ -287,6 +311,9 @@ template<typename T> struct optional_caster { | |
} | ||
|
||
PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name + _("]")); | ||
|
||
private: | ||
value_conv inner_caster; | ||
}; | ||
|
||
#if PYBIND11_HAS_OPTIONAL | ||
|
@@ -342,6 +369,7 @@ struct variant_caster<V<Ts...>> { | |
auto caster = make_caster<U>(); | ||
if (caster.load(src, convert)) { | ||
value = cast_op<U>(caster); | ||
subcaster = std::move(caster); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, are casters movable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are. |
||
return true; | ||
} | ||
return load_alternative(src, convert, type_list<Us...>{}); | ||
|
@@ -367,6 +395,9 @@ struct variant_caster<V<Ts...>> { | |
|
||
using Type = V<Ts...>; | ||
PYBIND11_TYPE_CASTER(Type, _("Union[") + detail::concat(make_caster<Ts>::name...) + _("]")); | ||
|
||
private: | ||
V<make_caster<Ts>...> subcaster; | ||
}; | ||
|
||
#if PYBIND11_HAS_VARIANT | ||
|
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.
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.
Call it
subcaster
, for consistency's sake.