2015-07-30 13:29:00 +00:00
|
|
|
/*
|
2016-05-05 18:33:54 +00:00
|
|
|
pybind11/stl.h: Transparent conversion for STL data types
|
2015-07-30 13:29:00 +00:00
|
|
|
|
2016-04-17 18:21:41 +00:00
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
2015-07-30 13:29:00 +00:00
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-10-15 16:13:33 +00:00
|
|
|
#include "pybind11.h"
|
2022-02-10 20:17:07 +00:00
|
|
|
#include "detail/common.h"
|
|
|
|
|
|
|
|
#include <deque>
|
2015-07-30 13:29:00 +00:00
|
|
|
#include <iostream>
|
2016-02-23 16:28:45 +00:00
|
|
|
#include <list>
|
2022-02-10 20:17:07 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2016-12-07 23:43:29 +00:00
|
|
|
#include <valarray>
|
2015-07-30 13:29:00 +00:00
|
|
|
|
2021-09-22 21:41:56 +00:00
|
|
|
// See `detail/common.h` for implementation of these guards.
|
|
|
|
#if defined(PYBIND11_HAS_OPTIONAL)
|
2022-02-10 20:17:07 +00:00
|
|
|
# include <optional>
|
2021-09-22 21:41:56 +00:00
|
|
|
#elif defined(PYBIND11_HAS_EXP_OPTIONAL)
|
2022-02-10 20:17:07 +00:00
|
|
|
# include <experimental/optional>
|
2021-09-22 21:41:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PYBIND11_HAS_VARIANT)
|
2022-02-10 20:17:07 +00:00
|
|
|
# include <variant>
|
2016-11-03 12:42:46 +00:00
|
|
|
#endif
|
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
|
|
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
2015-07-30 13:29:00 +00:00
|
|
|
|
2017-07-03 23:12:09 +00:00
|
|
|
/// Extracts an const lvalue reference or rvalue reference for U based on the type of T (e.g. for
|
|
|
|
/// forwarding a container element). Typically used indirect via forwarded_type(), below.
|
|
|
|
template <typename T, typename U>
|
2022-02-10 20:17:07 +00:00
|
|
|
using forwarded_type = conditional_t<std::is_lvalue_reference<T>::value,
|
|
|
|
remove_reference_t<U> &,
|
|
|
|
remove_reference_t<U> &&>;
|
2017-07-03 23:12:09 +00:00
|
|
|
|
|
|
|
/// Forwards a value U as rvalue or lvalue according to whether T is rvalue or lvalue; typically
|
|
|
|
/// used for forwarding a container's elements.
|
|
|
|
template <typename T, typename U>
|
|
|
|
forwarded_type<T, U> forward_like(U &&u) {
|
|
|
|
return std::forward<detail::forwarded_type<T, U>>(std::forward<U>(u));
|
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type, typename Key>
|
|
|
|
struct set_caster {
|
2016-09-05 12:30:56 +00:00
|
|
|
using type = Type;
|
|
|
|
using key_conv = make_caster<Key>;
|
2016-02-12 23:20:21 +00:00
|
|
|
|
|
|
|
bool load(handle src, bool convert) {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!isinstance<pybind11::set>(src)) {
|
2016-02-12 23:20:21 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-10-28 01:08:15 +00:00
|
|
|
auto s = reinterpret_borrow<pybind11::set>(src);
|
2016-02-12 23:20:21 +00:00
|
|
|
value.clear();
|
|
|
|
for (auto entry : s) {
|
2017-05-14 19:57:26 +00:00
|
|
|
key_conv conv;
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!conv.load(entry, convert)) {
|
2016-02-12 23:20:21 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-05-14 19:57:26 +00:00
|
|
|
value.insert(cast_op<Key &&>(std::move(conv)));
|
2016-02-12 23:20:21 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-03 23:12:09 +00:00
|
|
|
template <typename T>
|
|
|
|
static handle cast(T &&src, return_value_policy policy, handle parent) {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!std::is_lvalue_reference<T>::value) {
|
2018-11-09 19:12:46 +00:00
|
|
|
policy = return_value_policy_override<Key>::policy(policy);
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-02-12 23:20:21 +00:00
|
|
|
pybind11::set s;
|
2017-09-01 19:42:20 +00:00
|
|
|
for (auto &&value : src) {
|
2022-02-10 20:17:07 +00:00
|
|
|
auto value_ = reinterpret_steal<object>(
|
|
|
|
key_conv::cast(forward_like<T>(value), policy, parent));
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!value_ || !s.add(value_)) {
|
2016-02-12 23:20:21 +00:00
|
|
|
return handle();
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-02-12 23:20:21 +00:00
|
|
|
}
|
|
|
|
return s.release();
|
|
|
|
}
|
|
|
|
|
2021-12-21 19:24:21 +00:00
|
|
|
PYBIND11_TYPE_CASTER(type, const_name("Set[") + key_conv::name + const_name("]"));
|
2016-02-12 23:20:21 +00:00
|
|
|
};
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type, typename Key, typename Value>
|
|
|
|
struct map_caster {
|
|
|
|
using key_conv = make_caster<Key>;
|
2016-09-05 12:30:56 +00:00
|
|
|
using value_conv = make_caster<Value>;
|
2016-02-12 23:20:21 +00:00
|
|
|
|
|
|
|
bool load(handle src, bool convert) {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!isinstance<dict>(src)) {
|
2016-02-12 23:20:21 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-10-28 01:08:15 +00:00
|
|
|
auto d = reinterpret_borrow<dict>(src);
|
2016-02-12 23:20:21 +00:00
|
|
|
value.clear();
|
|
|
|
for (auto it : d) {
|
2017-05-14 19:57:26 +00:00
|
|
|
key_conv kconv;
|
|
|
|
value_conv vconv;
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!kconv.load(it.first.ptr(), convert) || !vconv.load(it.second.ptr(), convert)) {
|
2016-02-12 23:20:21 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-05-14 19:57:26 +00:00
|
|
|
value.emplace(cast_op<Key &&>(std::move(kconv)), cast_op<Value &&>(std::move(vconv)));
|
2016-02-12 23:20:21 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-03 23:12:09 +00:00
|
|
|
template <typename T>
|
|
|
|
static handle cast(T &&src, return_value_policy policy, handle parent) {
|
2016-02-12 23:20:21 +00:00
|
|
|
dict d;
|
2018-11-09 19:12:46 +00:00
|
|
|
return_value_policy policy_key = policy;
|
|
|
|
return_value_policy policy_value = policy;
|
|
|
|
if (!std::is_lvalue_reference<T>::value) {
|
|
|
|
policy_key = return_value_policy_override<Key>::policy(policy_key);
|
|
|
|
policy_value = return_value_policy_override<Value>::policy(policy_value);
|
|
|
|
}
|
2017-09-01 19:42:20 +00:00
|
|
|
for (auto &&kv : src) {
|
2022-02-10 20:17:07 +00:00
|
|
|
auto key = reinterpret_steal<object>(
|
|
|
|
key_conv::cast(forward_like<T>(kv.first), policy_key, parent));
|
|
|
|
auto value = reinterpret_steal<object>(
|
|
|
|
value_conv::cast(forward_like<T>(kv.second), policy_value, parent));
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!key || !value) {
|
2016-02-12 23:20:21 +00:00
|
|
|
return handle();
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-02-12 23:20:21 +00:00
|
|
|
d[key] = value;
|
|
|
|
}
|
|
|
|
return d.release();
|
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
PYBIND11_TYPE_CASTER(Type,
|
|
|
|
const_name("Dict[") + key_conv::name + const_name(", ") + value_conv::name
|
|
|
|
+ const_name("]"));
|
2016-02-12 23:20:21 +00:00
|
|
|
};
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type, typename Value>
|
|
|
|
struct list_caster {
|
2016-09-05 12:30:56 +00:00
|
|
|
using value_conv = make_caster<Value>;
|
2016-02-12 23:20:21 +00:00
|
|
|
|
2016-01-17 21:36:44 +00:00
|
|
|
bool load(handle src, bool convert) {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!isinstance<sequence>(src) || isinstance<bytes>(src) || isinstance<str>(src)) {
|
2015-07-30 13:29:00 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-10-28 01:08:15 +00:00
|
|
|
auto s = reinterpret_borrow<sequence>(src);
|
2016-02-23 16:28:45 +00:00
|
|
|
value.clear();
|
2016-10-12 16:00:53 +00:00
|
|
|
reserve_maybe(s, &value);
|
|
|
|
for (auto it : s) {
|
2017-05-14 19:57:26 +00:00
|
|
|
value_conv conv;
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!conv.load(it, convert)) {
|
2015-07-30 13:29:00 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-05-14 19:57:26 +00:00
|
|
|
value.push_back(cast_op<Value &&>(std::move(conv)));
|
2015-07-30 13:29:00 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-07 23:43:29 +00:00
|
|
|
private:
|
2021-06-19 17:53:27 +00:00
|
|
|
template <
|
2022-02-10 20:17:07 +00:00
|
|
|
typename T = Type,
|
2021-06-19 17:53:27 +00:00
|
|
|
enable_if_t<std::is_same<decltype(std::declval<T>().reserve(0)), void>::value, int> = 0>
|
|
|
|
void reserve_maybe(const sequence &s, Type *) {
|
|
|
|
value.reserve(s.size());
|
|
|
|
}
|
|
|
|
void reserve_maybe(const sequence &, void *) {}
|
2016-02-23 16:28:45 +00:00
|
|
|
|
2016-12-07 23:43:29 +00:00
|
|
|
public:
|
2017-07-03 23:12:09 +00:00
|
|
|
template <typename T>
|
|
|
|
static handle cast(T &&src, return_value_policy policy, handle parent) {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!std::is_lvalue_reference<T>::value) {
|
2018-11-09 19:12:46 +00:00
|
|
|
policy = return_value_policy_override<Value>::policy(policy);
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-01-17 21:36:41 +00:00
|
|
|
list l(src.size());
|
2021-08-28 23:40:46 +00:00
|
|
|
ssize_t index = 0;
|
2017-09-01 19:42:20 +00:00
|
|
|
for (auto &&value : src) {
|
2022-02-10 20:17:07 +00:00
|
|
|
auto value_ = reinterpret_steal<object>(
|
|
|
|
value_conv::cast(forward_like<T>(value), policy, parent));
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!value_) {
|
2016-01-17 21:36:44 +00:00
|
|
|
return handle();
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2021-08-28 23:40:46 +00:00
|
|
|
PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference
|
2015-07-30 13:29:00 +00:00
|
|
|
}
|
2016-01-17 21:36:41 +00:00
|
|
|
return l.release();
|
2015-07-30 13:29:00 +00:00
|
|
|
}
|
2016-02-23 16:28:45 +00:00
|
|
|
|
2021-12-21 19:24:21 +00:00
|
|
|
PYBIND11_TYPE_CASTER(Type, const_name("List[") + value_conv::name + const_name("]"));
|
2016-02-07 15:36:26 +00:00
|
|
|
};
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type, typename Alloc>
|
|
|
|
struct type_caster<std::vector<Type, Alloc>> : list_caster<std::vector<Type, Alloc>, Type> {};
|
2016-02-23 16:28:45 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type, typename Alloc>
|
|
|
|
struct type_caster<std::deque<Type, Alloc>> : list_caster<std::deque<Type, Alloc>, Type> {};
|
2018-11-16 05:45:19 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type, typename Alloc>
|
|
|
|
struct type_caster<std::list<Type, Alloc>> : list_caster<std::list<Type, Alloc>, Type> {};
|
2016-02-23 16:28:45 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename ArrayType, typename Value, bool Resizable, size_t Size = 0>
|
|
|
|
struct array_caster {
|
2016-12-07 23:43:29 +00:00
|
|
|
using value_conv = make_caster<Value>;
|
2016-02-12 23:20:21 +00:00
|
|
|
|
2016-12-07 23:43:29 +00:00
|
|
|
private:
|
|
|
|
template <bool R = Resizable>
|
|
|
|
bool require_size(enable_if_t<R, size_t> size) {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (value.size() != size) {
|
2016-12-07 23:43:29 +00:00
|
|
|
value.resize(size);
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-12-07 23:43:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template <bool R = Resizable>
|
|
|
|
bool require_size(enable_if_t<!R, size_t> size) {
|
|
|
|
return size == Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2016-02-07 15:36:26 +00:00
|
|
|
bool load(handle src, bool convert) {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!isinstance<sequence>(src)) {
|
2016-02-07 15:36:26 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2018-11-09 11:32:48 +00:00
|
|
|
auto l = reinterpret_borrow<sequence>(src);
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!require_size(l.size())) {
|
2016-02-07 15:36:26 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-02-07 15:36:26 +00:00
|
|
|
size_t ctr = 0;
|
|
|
|
for (auto it : l) {
|
2017-05-14 19:57:26 +00:00
|
|
|
value_conv conv;
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!conv.load(it, convert)) {
|
2016-02-07 15:36:26 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-05-14 19:57:26 +00:00
|
|
|
value[ctr++] = cast_op<Value &&>(std::move(conv));
|
2016-02-07 15:36:26 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-03 23:12:09 +00:00
|
|
|
template <typename T>
|
|
|
|
static handle cast(T &&src, return_value_policy policy, handle parent) {
|
2016-12-07 23:43:29 +00:00
|
|
|
list l(src.size());
|
2021-08-28 23:40:46 +00:00
|
|
|
ssize_t index = 0;
|
2017-09-01 19:42:20 +00:00
|
|
|
for (auto &&value : src) {
|
2022-02-10 20:17:07 +00:00
|
|
|
auto value_ = reinterpret_steal<object>(
|
|
|
|
value_conv::cast(forward_like<T>(value), policy, parent));
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!value_) {
|
2016-02-07 15:36:26 +00:00
|
|
|
return handle();
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2021-08-28 23:40:46 +00:00
|
|
|
PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference
|
2016-02-07 15:36:26 +00:00
|
|
|
}
|
|
|
|
return l.release();
|
|
|
|
}
|
2016-12-07 23:43:29 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
PYBIND11_TYPE_CASTER(ArrayType,
|
|
|
|
const_name("List[") + value_conv::name
|
|
|
|
+ const_name<Resizable>(const_name(""),
|
|
|
|
const_name("[") + const_name<Size>()
|
|
|
|
+ const_name("]"))
|
|
|
|
+ const_name("]"));
|
2015-07-30 13:29:00 +00:00
|
|
|
};
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type, size_t Size>
|
|
|
|
struct type_caster<std::array<Type, Size>>
|
|
|
|
: array_caster<std::array<Type, Size>, Type, false, Size> {};
|
2016-12-07 23:43:29 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type>
|
|
|
|
struct type_caster<std::valarray<Type>> : array_caster<std::valarray<Type>, Type, true> {};
|
2016-12-07 23:43:29 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Key, typename Compare, typename Alloc>
|
|
|
|
struct type_caster<std::set<Key, Compare, Alloc>>
|
|
|
|
: set_caster<std::set<Key, Compare, Alloc>, Key> {};
|
2015-07-30 13:29:00 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Key, typename Hash, typename Equal, typename Alloc>
|
|
|
|
struct type_caster<std::unordered_set<Key, Hash, Equal, Alloc>>
|
|
|
|
: set_caster<std::unordered_set<Key, Hash, Equal, Alloc>, Key> {};
|
2015-07-30 13:29:00 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Key, typename Value, typename Compare, typename Alloc>
|
|
|
|
struct type_caster<std::map<Key, Value, Compare, Alloc>>
|
|
|
|
: map_caster<std::map<Key, Value, Compare, Alloc>, Key, Value> {};
|
2015-08-24 13:31:24 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Key, typename Value, typename Hash, typename Equal, typename Alloc>
|
|
|
|
struct type_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>>
|
|
|
|
: map_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>, Key, Value> {};
|
2015-07-30 13:29:00 +00:00
|
|
|
|
2016-11-03 12:42:46 +00:00
|
|
|
// This type caster is intended to be used for std::optional and std::experimental::optional
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Type, typename Value = typename Type::value_type>
|
|
|
|
struct optional_caster {
|
fix: the types for return_value_policy_override in optional_caster (#3376)
* fix: the types for return_value_policy_override in optional_caster
`return_value_policy_override` was not being applied correctly in
`optional_caster` in two ways:
- The `is_lvalue_reference` condition referenced `T`, which was the
`optional<T>` type parameter from the class, when it should have used `T_`,
which was the parameter to the `cast` function. `T_` can potentially be a
reference type, but `T` will never be.
- The type parameter passed to `return_value_policy_override` should be
`T::value_type`, not `T`. This matches the way that the other STL container
type casters work.
The result of these issues was that a method/property definition which used a
`reference` or `reference_internal` return value policy would create a Python
value that's bound by reference to a temporary C++ object, resulting in
undefined behavior. For reasons that I was not able to figure out fully, it
seems like this causes problems when using old versions of `boost::optional`,
but not with recent versions of `boost::optional` or the `libstdc++`
implementation of `std::optional`. The issue (that the override to
`return_value_policy::move` is never being applied) is present for all
implementations, it just seems like that somehow doesn't result in problems for
the some implementation of `optional`. This change includes a regression type
with a custom optional-like type which was able to reproduce the issue.
Part of the issue with using the wrong types may have stemmed from the type
variables `T` and `T_` having very similar names. This also changes the type
variables in `optional_caster` to use slightly more descriptive names, which
also more closely follow the naming convention used by the other STL casters.
Fixes #3330
* Fix clang-tidy complaints
* Add missing NOLINT
* Apply a couple more fixes
* fix: support GCC 4.8
* tests: avoid warning about unknown compiler for compilers missing C++17
* Remove unneeded test module attribute
* Change test enum to have more unique int values
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-26 02:04:45 +00:00
|
|
|
using value_conv = make_caster<Value>;
|
2016-11-03 12:42:46 +00:00
|
|
|
|
fix: the types for return_value_policy_override in optional_caster (#3376)
* fix: the types for return_value_policy_override in optional_caster
`return_value_policy_override` was not being applied correctly in
`optional_caster` in two ways:
- The `is_lvalue_reference` condition referenced `T`, which was the
`optional<T>` type parameter from the class, when it should have used `T_`,
which was the parameter to the `cast` function. `T_` can potentially be a
reference type, but `T` will never be.
- The type parameter passed to `return_value_policy_override` should be
`T::value_type`, not `T`. This matches the way that the other STL container
type casters work.
The result of these issues was that a method/property definition which used a
`reference` or `reference_internal` return value policy would create a Python
value that's bound by reference to a temporary C++ object, resulting in
undefined behavior. For reasons that I was not able to figure out fully, it
seems like this causes problems when using old versions of `boost::optional`,
but not with recent versions of `boost::optional` or the `libstdc++`
implementation of `std::optional`. The issue (that the override to
`return_value_policy::move` is never being applied) is present for all
implementations, it just seems like that somehow doesn't result in problems for
the some implementation of `optional`. This change includes a regression type
with a custom optional-like type which was able to reproduce the issue.
Part of the issue with using the wrong types may have stemmed from the type
variables `T` and `T_` having very similar names. This also changes the type
variables in `optional_caster` to use slightly more descriptive names, which
also more closely follow the naming convention used by the other STL casters.
Fixes #3330
* Fix clang-tidy complaints
* Add missing NOLINT
* Apply a couple more fixes
* fix: support GCC 4.8
* tests: avoid warning about unknown compiler for compilers missing C++17
* Remove unneeded test module attribute
* Change test enum to have more unique int values
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-26 02:04:45 +00:00
|
|
|
template <typename T>
|
|
|
|
static handle cast(T &&src, return_value_policy policy, handle parent) {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!src) {
|
2016-11-15 12:00:38 +00:00
|
|
|
return none().inc_ref();
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2020-06-29 17:52:35 +00:00
|
|
|
if (!std::is_lvalue_reference<T>::value) {
|
fix: the types for return_value_policy_override in optional_caster (#3376)
* fix: the types for return_value_policy_override in optional_caster
`return_value_policy_override` was not being applied correctly in
`optional_caster` in two ways:
- The `is_lvalue_reference` condition referenced `T`, which was the
`optional<T>` type parameter from the class, when it should have used `T_`,
which was the parameter to the `cast` function. `T_` can potentially be a
reference type, but `T` will never be.
- The type parameter passed to `return_value_policy_override` should be
`T::value_type`, not `T`. This matches the way that the other STL container
type casters work.
The result of these issues was that a method/property definition which used a
`reference` or `reference_internal` return value policy would create a Python
value that's bound by reference to a temporary C++ object, resulting in
undefined behavior. For reasons that I was not able to figure out fully, it
seems like this causes problems when using old versions of `boost::optional`,
but not with recent versions of `boost::optional` or the `libstdc++`
implementation of `std::optional`. The issue (that the override to
`return_value_policy::move` is never being applied) is present for all
implementations, it just seems like that somehow doesn't result in problems for
the some implementation of `optional`. This change includes a regression type
with a custom optional-like type which was able to reproduce the issue.
Part of the issue with using the wrong types may have stemmed from the type
variables `T` and `T_` having very similar names. This also changes the type
variables in `optional_caster` to use slightly more descriptive names, which
also more closely follow the naming convention used by the other STL casters.
Fixes #3330
* Fix clang-tidy complaints
* Add missing NOLINT
* Apply a couple more fixes
* fix: support GCC 4.8
* tests: avoid warning about unknown compiler for compilers missing C++17
* Remove unneeded test module attribute
* Change test enum to have more unique int values
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-26 02:04:45 +00:00
|
|
|
policy = return_value_policy_override<Value>::policy(policy);
|
2020-06-29 17:52:35 +00:00
|
|
|
}
|
fix: the types for return_value_policy_override in optional_caster (#3376)
* fix: the types for return_value_policy_override in optional_caster
`return_value_policy_override` was not being applied correctly in
`optional_caster` in two ways:
- The `is_lvalue_reference` condition referenced `T`, which was the
`optional<T>` type parameter from the class, when it should have used `T_`,
which was the parameter to the `cast` function. `T_` can potentially be a
reference type, but `T` will never be.
- The type parameter passed to `return_value_policy_override` should be
`T::value_type`, not `T`. This matches the way that the other STL container
type casters work.
The result of these issues was that a method/property definition which used a
`reference` or `reference_internal` return value policy would create a Python
value that's bound by reference to a temporary C++ object, resulting in
undefined behavior. For reasons that I was not able to figure out fully, it
seems like this causes problems when using old versions of `boost::optional`,
but not with recent versions of `boost::optional` or the `libstdc++`
implementation of `std::optional`. The issue (that the override to
`return_value_policy::move` is never being applied) is present for all
implementations, it just seems like that somehow doesn't result in problems for
the some implementation of `optional`. This change includes a regression type
with a custom optional-like type which was able to reproduce the issue.
Part of the issue with using the wrong types may have stemmed from the type
variables `T` and `T_` having very similar names. This also changes the type
variables in `optional_caster` to use slightly more descriptive names, which
also more closely follow the naming convention used by the other STL casters.
Fixes #3330
* Fix clang-tidy complaints
* Add missing NOLINT
* Apply a couple more fixes
* fix: support GCC 4.8
* tests: avoid warning about unknown compiler for compilers missing C++17
* Remove unneeded test module attribute
* Change test enum to have more unique int values
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-26 02:04:45 +00:00
|
|
|
return value_conv::cast(*std::forward<T>(src), policy, parent);
|
2016-11-03 12:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool load(handle src, bool convert) {
|
|
|
|
if (!src) {
|
|
|
|
return false;
|
2021-07-09 13:45:53 +00:00
|
|
|
}
|
|
|
|
if (src.is_none()) {
|
2022-02-10 20:17:07 +00:00
|
|
|
return true; // default-constructed value is already empty
|
2016-11-03 12:42:46 +00:00
|
|
|
}
|
2016-11-25 12:06:18 +00:00
|
|
|
value_conv inner_caster;
|
2022-02-08 00:23:20 +00:00
|
|
|
if (!inner_caster.load(src, convert)) {
|
2016-11-25 12:06:18 +00:00
|
|
|
return false;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-11-03 12:42:46 +00:00
|
|
|
|
fix: the types for return_value_policy_override in optional_caster (#3376)
* fix: the types for return_value_policy_override in optional_caster
`return_value_policy_override` was not being applied correctly in
`optional_caster` in two ways:
- The `is_lvalue_reference` condition referenced `T`, which was the
`optional<T>` type parameter from the class, when it should have used `T_`,
which was the parameter to the `cast` function. `T_` can potentially be a
reference type, but `T` will never be.
- The type parameter passed to `return_value_policy_override` should be
`T::value_type`, not `T`. This matches the way that the other STL container
type casters work.
The result of these issues was that a method/property definition which used a
`reference` or `reference_internal` return value policy would create a Python
value that's bound by reference to a temporary C++ object, resulting in
undefined behavior. For reasons that I was not able to figure out fully, it
seems like this causes problems when using old versions of `boost::optional`,
but not with recent versions of `boost::optional` or the `libstdc++`
implementation of `std::optional`. The issue (that the override to
`return_value_policy::move` is never being applied) is present for all
implementations, it just seems like that somehow doesn't result in problems for
the some implementation of `optional`. This change includes a regression type
with a custom optional-like type which was able to reproduce the issue.
Part of the issue with using the wrong types may have stemmed from the type
variables `T` and `T_` having very similar names. This also changes the type
variables in `optional_caster` to use slightly more descriptive names, which
also more closely follow the naming convention used by the other STL casters.
Fixes #3330
* Fix clang-tidy complaints
* Add missing NOLINT
* Apply a couple more fixes
* fix: support GCC 4.8
* tests: avoid warning about unknown compiler for compilers missing C++17
* Remove unneeded test module attribute
* Change test enum to have more unique int values
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-26 02:04:45 +00:00
|
|
|
value.emplace(cast_op<Value &&>(std::move(inner_caster)));
|
2016-11-25 12:06:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-03 12:42:46 +00:00
|
|
|
|
2021-12-21 19:24:21 +00:00
|
|
|
PYBIND11_TYPE_CASTER(Type, const_name("Optional[") + value_conv::name + const_name("]"));
|
2016-11-03 12:42:46 +00:00
|
|
|
};
|
|
|
|
|
2020-09-10 17:58:26 +00:00
|
|
|
#if defined(PYBIND11_HAS_OPTIONAL)
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename T>
|
|
|
|
struct type_caster<std::optional<T>> : public optional_caster<std::optional<T>> {};
|
2016-11-15 12:00:38 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <>
|
|
|
|
struct type_caster<std::nullopt_t> : public void_caster<std::nullopt_t> {};
|
2016-11-03 12:42:46 +00:00
|
|
|
#endif
|
|
|
|
|
2020-09-10 17:58:26 +00:00
|
|
|
#if defined(PYBIND11_HAS_EXP_OPTIONAL)
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename T>
|
|
|
|
struct type_caster<std::experimental::optional<T>>
|
2016-11-03 12:42:46 +00:00
|
|
|
: public optional_caster<std::experimental::optional<T>> {};
|
2016-11-15 12:00:38 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <>
|
|
|
|
struct type_caster<std::experimental::nullopt_t>
|
2016-11-15 12:00:38 +00:00
|
|
|
: public void_caster<std::experimental::nullopt_t> {};
|
2016-11-03 12:42:46 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-21 21:54:41 +00:00
|
|
|
/// Visit a variant and cast any found type to Python
|
|
|
|
struct variant_caster_visitor {
|
|
|
|
return_value_policy policy;
|
|
|
|
handle parent;
|
|
|
|
|
2017-08-08 14:02:31 +00:00
|
|
|
using result_type = handle; // required by boost::variant in C++11
|
|
|
|
|
2017-04-21 21:54:41 +00:00
|
|
|
template <typename T>
|
2017-08-08 14:02:31 +00:00
|
|
|
result_type operator()(T &&src) const {
|
2017-04-21 21:54:41 +00:00
|
|
|
return make_caster<T>::cast(std::forward<T>(src), policy, parent);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Helper class which abstracts away variant's `visit` function. `std::variant` and similar
|
|
|
|
/// `namespace::variant` types which provide a `namespace::visit()` function are handled here
|
|
|
|
/// automatically using argument-dependent lookup. Users can provide specializations for other
|
|
|
|
/// variant-like classes, e.g. `boost::variant` and `boost::apply_visitor`.
|
2022-02-10 20:17:07 +00:00
|
|
|
template <template <typename...> class Variant>
|
2017-04-21 21:54:41 +00:00
|
|
|
struct visit_helper {
|
|
|
|
template <typename... Args>
|
|
|
|
static auto call(Args &&...args) -> decltype(visit(std::forward<Args>(args)...)) {
|
|
|
|
return visit(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Generic variant caster
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename Variant>
|
|
|
|
struct variant_caster;
|
2017-04-21 21:54:41 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <template <typename...> class V, typename... Ts>
|
2017-04-21 21:54:41 +00:00
|
|
|
struct variant_caster<V<Ts...>> {
|
|
|
|
static_assert(sizeof...(Ts) > 0, "Variant must consist of at least one alternative.");
|
|
|
|
|
|
|
|
template <typename U, typename... Us>
|
|
|
|
bool load_alternative(handle src, bool convert, type_list<U, Us...>) {
|
|
|
|
auto caster = make_caster<U>();
|
|
|
|
if (caster.load(src, convert)) {
|
2022-03-18 18:10:31 +00:00
|
|
|
value = cast_op<U>(std::move(caster));
|
2017-04-21 21:54:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return load_alternative(src, convert, type_list<Us...>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool load_alternative(handle, bool, type_list<>) { return false; }
|
|
|
|
|
|
|
|
bool load(handle src, bool convert) {
|
2017-05-10 09:15:17 +00:00
|
|
|
// Do a first pass without conversions to improve constructor resolution.
|
|
|
|
// E.g. `py::int_(1).cast<variant<double, int>>()` needs to fill the `int`
|
|
|
|
// slot of the variant. Without two-pass loading `double` would be filled
|
|
|
|
// because it appears first and a conversion is possible.
|
2022-02-08 00:23:20 +00:00
|
|
|
if (convert && load_alternative(src, false, type_list<Ts...>{})) {
|
2017-05-10 09:15:17 +00:00
|
|
|
return true;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-04-21 21:54:41 +00:00
|
|
|
return load_alternative(src, convert, type_list<Ts...>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Variant>
|
|
|
|
static handle cast(Variant &&src, return_value_policy policy, handle parent) {
|
|
|
|
return visit_helper<V>::call(variant_caster_visitor{policy, parent},
|
|
|
|
std::forward<Variant>(src));
|
|
|
|
}
|
|
|
|
|
|
|
|
using Type = V<Ts...>;
|
2022-02-10 20:17:07 +00:00
|
|
|
PYBIND11_TYPE_CASTER(Type,
|
|
|
|
const_name("Union[") + detail::concat(make_caster<Ts>::name...)
|
|
|
|
+ const_name("]"));
|
2017-04-21 21:54:41 +00:00
|
|
|
};
|
|
|
|
|
2020-09-10 17:58:26 +00:00
|
|
|
#if defined(PYBIND11_HAS_VARIANT)
|
2017-04-21 21:54:41 +00:00
|
|
|
template <typename... Ts>
|
2022-02-10 20:17:07 +00:00
|
|
|
struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> {};
|
2017-04-21 21:54:41 +00:00
|
|
|
#endif
|
2018-07-17 14:56:26 +00:00
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_END(detail)
|
2015-09-04 21:42:12 +00:00
|
|
|
|
2016-01-17 21:36:41 +00:00
|
|
|
inline std::ostream &operator<<(std::ostream &os, const handle &obj) {
|
2021-07-01 05:19:14 +00:00
|
|
|
#ifdef PYBIND11_HAS_STRING_VIEW
|
|
|
|
os << str(obj).cast<std::string_view>();
|
|
|
|
#else
|
2016-10-25 20:12:39 +00:00
|
|
|
os << (std::string) str(obj);
|
2021-07-01 05:19:14 +00:00
|
|
|
#endif
|
2016-01-17 21:36:41 +00:00
|
|
|
return os;
|
|
|
|
}
|
2015-07-30 13:29:00 +00:00
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|