2017-06-08 22:44:49 +00:00
|
|
|
/*
|
|
|
|
tests/test_stl.cpp -- STL type casters
|
|
|
|
|
|
|
|
Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <pybind11/stl.h>
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
#include "constructor_stats.h"
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
2021-07-08 16:02:48 +00:00
|
|
|
#ifndef PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL
|
2022-02-10 20:17:07 +00:00
|
|
|
# define PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL
|
2021-07-08 16:02:48 +00:00
|
|
|
#endif
|
|
|
|
#include <pybind11/stl/filesystem.h>
|
|
|
|
|
2018-10-11 08:28:12 +00:00
|
|
|
#include <string>
|
2022-02-10 20:17:07 +00:00
|
|
|
#include <vector>
|
2018-10-11 08:28:12 +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
|
|
|
#if defined(PYBIND11_TEST_BOOST)
|
2022-02-10 20:17:07 +00:00
|
|
|
# include <boost/optional.hpp>
|
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
|
|
|
|
2022-08-01 18:31:31 +00:00
|
|
|
namespace PYBIND11_NAMESPACE {
|
2022-02-10 20:17:07 +00:00
|
|
|
namespace detail {
|
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>
|
|
|
|
struct type_caster<boost::optional<T>> : optional_caster<boost::optional<T>> {};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct type_caster<boost::none_t> : void_caster<boost::none_t> {};
|
2022-02-10 20:17:07 +00:00
|
|
|
} // namespace detail
|
2022-08-01 18:31:31 +00:00
|
|
|
} // namespace PYBIND11_NAMESPACE
|
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
|
|
|
#endif
|
|
|
|
|
2017-08-08 14:02:31 +00:00
|
|
|
// Test with `std::variant` in C++17 mode, or with `boost::variant` in C++11/14
|
2020-09-10 17:58:26 +00:00
|
|
|
#if defined(PYBIND11_HAS_VARIANT)
|
2017-08-08 14:02:31 +00:00
|
|
|
using std::variant;
|
2022-03-22 05:58:04 +00:00
|
|
|
# define PYBIND11_TEST_VARIANT 1
|
Dropping MSVC 2015 (#3722)
* Changing `_MSC_VER` guard to `< 1910` (dropping MSVC 2015).
* Removing MSVC 2015 from ci.yml, and .appveyor.yml entirely.
* Bringing back .appveyor.yml from master.
* appveyor Visual Studio 2017
* 1st manual pass, builds & tests with unix_clang, before pre-commit.
* After clang-format (via pre-commit).
* Manual pass looking for "2015", builds & tests with unix_clang, before pre-commit.
* Backtracking for include/pybind11 changes in previous commit.
git checkout d07865846c7d31dd61111e6df801864327b65070 include/pybind11/attr.h include/pybind11/detail/common.h include/pybind11/functional.h
--------------------
CI #4160 errors observed:
https://github.com/pybind/pybind11/pull/3722/commits/2a26873727214c5f1e159cba98f5c625b908381a
https://github.com/pybind/pybind11/runs/5168332130?check_suite_focus=true
$ grep ' error C' *.txt | sed 's/2022-02-12[^ ]*//' | sed 's/^[0-9][0-9]*//' | sed 's/^.*\.txt: //' | sort | uniqD:\a\pybind11\pybind11\include\pybind11\cast.h(1364,1): error C2752: 'pybind11::detail::type_caster<Eigen::Ref<Eigen::Vector3f,0,pybind11::EigenDStride>,void>': more than one partial specialization matches the template argument list [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
d:\a\pybind11\pybind11\include\pybind11\detail/common.h(1023): error C2737: 'pybind11::overload_cast': 'constexpr' object must be initialized [D:\a\pybind11\pybind11\build\tests\cross_module_gil_utils.vcxproj]
d:\a\pybind11\pybind11\include\pybind11\detail/common.h(1023): error C2737: 'pybind11::overload_cast': 'constexpr' object must be initialized [D:\a\pybind11\pybind11\build\tests\pybind11_cross_module_tests.vcxproj]
d:\a\pybind11\pybind11\include\pybind11\detail/common.h(1023): error C2737: 'pybind11::overload_cast': 'constexpr' object must be initialized [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
d:\a\pybind11\pybind11\include\pybind11\detail/common.h(1023): error C2737: 'pybind11::overload_cast': 'constexpr' object must be initialized [D:\a\pybind11\pybind11\build\tests\test_embed\external_module.vcxproj]
D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj]
$ grep ': error C2737' *.txt | sed 's/^.*MSVC//' | sed 's/___.*//' | sort | uniq
_2017
$ grep ': error C2752' *.txt
3______3.8_____MSVC_2019_____x86_-DCMAKE_CXX_STANDARD=17.txt:2022-02-12T16:12:45.9921122Z D:\a\pybind11\pybind11\include\pybind11\cast.h(1364,1): error C2752: 'pybind11::detail::type_caster<Eigen::Ref<Eigen::Vector3f,0,pybind11::EigenDStride>,void>': more than one partial specialization matches the template argument list [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
$ grep ': fatal error C1001:' *.txt
10______pypy-3.8-v7.3.7_____windows-2022_____x64.txt:2022-02-12T16:12:56.3163683Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj]
1______3.6_____MSVC_2019_____x86.txt:2022-02-12T16:12:47.6774625Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
16______3.6_____windows-latest_____x64_-DPYBIND11_FINDPYTHON=ON.txt:2022-02-12T16:12:27.0556151Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj]
17______3.9_____windows-2019_____x64.txt:2022-02-12T16:12:30.3822566Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj]
2______3.7_____MSVC_2019_____x86.txt:2022-02-12T16:12:38.7018911Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
6______3.6_____windows-2022_____x64.txt:2022-02-12T16:12:00.4513642Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj]
7______3.9_____windows-2022_____x64.txt:2022-02-12T16:11:43.6306160Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj]
8______3.10_____windows-2022_____x64.txt:2022-02-12T16:11:49.9589644Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj]
9______pypy-3.7-v7.3.7_____windows-2022_____x64.txt:2022-02-12T16:11:53.7912112Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj]
* common.h: is_template_base_of
* Re-applying 4 changes from 2a26873727214c5f1e159cba98f5c625b908381a that work universally.
* `overload_cast = {};` only for MSVC 2017 and Clang 5
* Refining condition for using is_template_base_of workaround.
* Undoing MSVC 2015 workaround in test_constants_and_functions.cpp
* CentOS7: silence_unused_warnings
* Tweaks in response to reviews.
* Adding windows-2022 C++20
* Trying another way of adding windows-2022 C++20
2022-02-14 19:36:22 +00:00
|
|
|
#elif defined(PYBIND11_TEST_BOOST)
|
2022-02-10 20:17:07 +00:00
|
|
|
# include <boost/variant.hpp>
|
2022-03-22 05:58:04 +00:00
|
|
|
# define PYBIND11_TEST_VARIANT 1
|
2017-08-08 14:02:31 +00:00
|
|
|
using boost::variant;
|
|
|
|
|
2022-08-01 18:31:31 +00:00
|
|
|
namespace PYBIND11_NAMESPACE {
|
2022-02-10 20:17:07 +00:00
|
|
|
namespace detail {
|
2017-08-08 14:02:31 +00:00
|
|
|
template <typename... Ts>
|
|
|
|
struct type_caster<boost::variant<Ts...>> : variant_caster<boost::variant<Ts...>> {};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct visit_helper<boost::variant> {
|
|
|
|
template <typename... Args>
|
|
|
|
static auto call(Args &&...args) -> decltype(boost::apply_visitor(args...)) {
|
|
|
|
return boost::apply_visitor(args...);
|
|
|
|
}
|
|
|
|
};
|
2022-02-10 20:17:07 +00:00
|
|
|
} // namespace detail
|
2022-08-01 18:31:31 +00:00
|
|
|
} // namespace PYBIND11_NAMESPACE
|
2017-08-08 14:02:31 +00:00
|
|
|
#endif
|
2017-06-08 22:44:49 +00:00
|
|
|
|
2018-10-11 08:28:12 +00:00
|
|
|
PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>);
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
/// Issue #528: templated constructor
|
|
|
|
struct TplCtorClass {
|
CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250)
* Adding google-explicit-constructor to .clang-tidy
* clang-tidy explicit attr.h (all automatic)
* clang-tidy explicit cast.h (all automatic)
* clang-tidy detail/init.h (1 NOLINT)
* clang-tidy detail/type_caster_base.h (2 NOLINT)
* clang-tidy pybind11.h (7 NOLINT)
* clang-tidy detail/common.h (3 NOLINT)
* clang-tidy detail/descr.h (2 NOLINT)
* clang-tidy pytypes.h (23 NOLINT, only 1 explicit)
* clang-tidy eigen.h (7 NOLINT, 0 explicit)
* Adding 2 explicit in functional.h
* Adding 4 explicit in iostream.h
* clang-tidy numpy.h (1 NOLINT, 1 explicit)
* clang-tidy embed.h (0 NOLINT, 1 explicit)
* clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit)
* clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit)
* clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit)
* clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit)
* clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit)
* clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit)
* clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit)
* clang-tidy tests/object.h (0 NOLINT, 2 explicit)
* clang-tidy batch of fully automatic fixes.
* Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
2021-09-09 01:53:38 +00:00
|
|
|
template <typename T>
|
|
|
|
explicit TplCtorClass(const T &) {}
|
2017-06-08 22:44:49 +00:00
|
|
|
bool operator==(const TplCtorClass &) const { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace std {
|
2022-02-10 20:17:07 +00:00
|
|
|
template <>
|
|
|
|
struct hash<TplCtorClass> {
|
|
|
|
size_t operator()(const TplCtorClass &) const { return 0; }
|
|
|
|
};
|
2020-09-11 01:16:40 +00:00
|
|
|
} // namespace std
|
2017-06-08 22:44:49 +00:00
|
|
|
|
2020-03-14 14:10:14 +00:00
|
|
|
template <template <typename> class OptionalImpl, typename T>
|
2022-02-10 20:17:07 +00:00
|
|
|
struct OptionalHolder {
|
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
|
|
|
// NOLINTNEXTLINE(modernize-use-equals-default): breaks GCC 4.8
|
2022-02-10 20:17:07 +00:00
|
|
|
OptionalHolder(){};
|
|
|
|
bool member_initialized() const { return member && member->initialized; }
|
2020-03-14 13:15:12 +00:00
|
|
|
OptionalImpl<T> member = T{};
|
|
|
|
};
|
|
|
|
|
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
|
|
|
enum class EnumType {
|
2022-02-10 20:17:07 +00:00
|
|
|
kSet = 42,
|
|
|
|
kUnset = 85,
|
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
|
|
|
};
|
|
|
|
|
|
|
|
// This is used to test that return-by-ref and return-by-copy policies are
|
|
|
|
// handled properly for optional types. This is a regression test for a dangling
|
|
|
|
// reference issue. The issue seemed to require the enum value type to
|
|
|
|
// reproduce - it didn't seem to happen if the value type is just an integer.
|
|
|
|
template <template <typename> class OptionalImpl>
|
|
|
|
class OptionalProperties {
|
|
|
|
public:
|
|
|
|
using OptionalEnumValue = OptionalImpl<EnumType>;
|
|
|
|
|
|
|
|
OptionalProperties() : value(EnumType::kSet) {}
|
|
|
|
~OptionalProperties() {
|
|
|
|
// Reset value to detect use-after-destruction.
|
|
|
|
// This is set to a specific value rather than nullopt to ensure that
|
|
|
|
// the memory that contains the value gets re-written.
|
|
|
|
value = EnumType::kUnset;
|
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
OptionalEnumValue &access_by_ref() { return 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
|
|
|
OptionalEnumValue access_by_copy() { return value; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
OptionalEnumValue value;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This type mimics aspects of boost::optional from old versions of Boost,
|
|
|
|
// which exposed a dangling reference bug in Pybind11. Recent versions of
|
|
|
|
// boost::optional, as well as libstdc++'s std::optional, don't seem to be
|
|
|
|
// affected by the same issue. This is meant to be a minimal implementation
|
|
|
|
// required to reproduce the issue, not fully standard-compliant.
|
|
|
|
// See issue #3330 for more details.
|
|
|
|
template <typename T>
|
|
|
|
class ReferenceSensitiveOptional {
|
|
|
|
public:
|
|
|
|
using value_type = T;
|
|
|
|
|
|
|
|
ReferenceSensitiveOptional() = default;
|
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2022-02-10 20:17:07 +00:00
|
|
|
ReferenceSensitiveOptional(const T &value) : storage{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
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2022-02-10 20:17:07 +00:00
|
|
|
ReferenceSensitiveOptional(T &&value) : storage{std::move(value)} {}
|
|
|
|
ReferenceSensitiveOptional &operator=(const 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
|
|
|
storage = {value};
|
|
|
|
return *this;
|
|
|
|
}
|
2022-02-10 20:17:07 +00:00
|
|
|
ReferenceSensitiveOptional &operator=(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
|
|
|
storage = {std::move(value)};
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
2022-02-10 20:17:07 +00:00
|
|
|
T &emplace(Args &&...args) {
|
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
|
|
|
storage.clear();
|
|
|
|
storage.emplace_back(std::forward<Args>(args)...);
|
|
|
|
return storage.back();
|
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
const T &value() const noexcept {
|
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
|
|
|
assert(!storage.empty());
|
|
|
|
return storage[0];
|
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
const T &operator*() const noexcept { return 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
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
const T *operator->() const noexcept { return &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
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
explicit operator bool() const noexcept { return !storage.empty(); }
|
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
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<T> storage;
|
|
|
|
};
|
|
|
|
|
2022-08-01 18:31:31 +00:00
|
|
|
namespace PYBIND11_NAMESPACE {
|
2022-02-10 20:17:07 +00:00
|
|
|
namespace detail {
|
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>
|
2022-02-10 20:17:07 +00:00
|
|
|
struct type_caster<ReferenceSensitiveOptional<T>>
|
|
|
|
: optional_caster<ReferenceSensitiveOptional<T>> {};
|
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
|
|
|
} // namespace detail
|
2022-08-01 18:31:31 +00:00
|
|
|
} // namespace PYBIND11_NAMESPACE
|
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
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
TEST_SUBMODULE(stl, m) {
|
|
|
|
// test_vector
|
|
|
|
m.def("cast_vector", []() { return std::vector<int>{1}; });
|
|
|
|
m.def("load_vector", [](const std::vector<int> &v) { return v.at(0) == 1 && v.at(1) == 2; });
|
2017-09-01 19:42:20 +00:00
|
|
|
// `std::vector<bool>` is special because it returns proxy objects instead of references
|
|
|
|
m.def("cast_bool_vector", []() { return std::vector<bool>{true, false}; });
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("load_bool_vector",
|
|
|
|
[](const std::vector<bool> &v) { return v.at(0) == true && v.at(1) == false; });
|
2017-07-07 00:41:52 +00:00
|
|
|
// Unnumbered regression (caused by #936): pointers to stl containers aren't castable
|
2022-05-24 17:46:31 +00:00
|
|
|
m.def(
|
2022-08-01 13:18:48 +00:00
|
|
|
"cast_ptr_vector",
|
|
|
|
[]() {
|
|
|
|
// Using no-destructor idiom to side-step warnings from overzealous compilers.
|
|
|
|
static auto *v = new std::vector<RValueCaster>{2};
|
|
|
|
return v;
|
|
|
|
},
|
|
|
|
py::return_value_policy::reference);
|
2017-06-08 22:44:49 +00:00
|
|
|
|
2018-11-16 05:45:19 +00:00
|
|
|
// test_deque
|
|
|
|
m.def("cast_deque", []() { return std::deque<int>{1}; });
|
|
|
|
m.def("load_deque", [](const std::deque<int> &v) { return v.at(0) == 1 && v.at(1) == 2; });
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
// test_array
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("cast_array", []() { return std::array<int, 2>{{1, 2}}; });
|
2017-06-08 22:44:49 +00:00
|
|
|
m.def("load_array", [](const std::array<int, 2> &a) { return a[0] == 1 && a[1] == 2; });
|
|
|
|
|
|
|
|
// test_valarray
|
|
|
|
m.def("cast_valarray", []() { return std::valarray<int>{1, 4, 9}; });
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("load_valarray", [](const std::valarray<int> &v) {
|
2017-06-08 22:44:49 +00:00
|
|
|
return v.size() == 3 && v[0] == 1 && v[1] == 4 && v[2] == 9;
|
|
|
|
});
|
|
|
|
|
|
|
|
// test_map
|
|
|
|
m.def("cast_map", []() { return std::map<std::string, std::string>{{"key", "value"}}; });
|
|
|
|
m.def("load_map", [](const std::map<std::string, std::string> &map) {
|
|
|
|
return map.at("key") == "value" && map.at("key2") == "value2";
|
|
|
|
});
|
|
|
|
|
|
|
|
// test_set
|
|
|
|
m.def("cast_set", []() { return std::set<std::string>{"key1", "key2"}; });
|
|
|
|
m.def("load_set", [](const std::set<std::string> &set) {
|
2021-07-27 22:32:26 +00:00
|
|
|
return (set.count("key1") != 0u) && (set.count("key2") != 0u) && (set.count("key3") != 0u);
|
2017-06-08 22:44:49 +00:00
|
|
|
});
|
|
|
|
|
2017-07-03 23:12:09 +00:00
|
|
|
// test_recursive_casting
|
|
|
|
m.def("cast_rv_vector", []() { return std::vector<RValueCaster>{2}; });
|
|
|
|
m.def("cast_rv_array", []() { return std::array<RValueCaster, 3>(); });
|
|
|
|
// NB: map and set keys are `const`, so while we technically do move them (as `const Type &&`),
|
|
|
|
// casters don't typically do anything with that, which means they fall to the `const Type &`
|
|
|
|
// caster.
|
2024-04-02 15:14:08 +00:00
|
|
|
m.def("cast_rv_map",
|
|
|
|
[]() { return std::unordered_map<std::string, RValueCaster>{{"a", RValueCaster{}}}; });
|
2017-07-03 23:12:09 +00:00
|
|
|
m.def("cast_rv_nested", []() {
|
|
|
|
std::vector<std::array<std::list<std::unordered_map<std::string, RValueCaster>>, 2>> v;
|
2022-02-10 20:17:07 +00:00
|
|
|
v.emplace_back(); // add an array
|
2017-07-03 23:12:09 +00:00
|
|
|
v.back()[0].emplace_back(); // add a map to the array
|
|
|
|
v.back()[0].back().emplace("b", RValueCaster{});
|
|
|
|
v.back()[0].back().emplace("c", RValueCaster{});
|
|
|
|
v.back()[1].emplace_back(); // add a map to the array
|
|
|
|
v.back()[1].back().emplace("a", RValueCaster{});
|
|
|
|
return v;
|
|
|
|
});
|
|
|
|
static std::array<RValueCaster, 2> lva;
|
2022-02-10 20:17:07 +00:00
|
|
|
static std::unordered_map<std::string, RValueCaster> lvm{{"a", RValueCaster{}},
|
|
|
|
{"b", RValueCaster{}}};
|
|
|
|
static std::unordered_map<std::string, std::vector<std::list<std::array<RValueCaster, 2>>>>
|
|
|
|
lvn;
|
|
|
|
lvn["a"].emplace_back(); // add a list
|
2017-07-03 23:12:09 +00:00
|
|
|
lvn["a"].back().emplace_back(); // add an array
|
2022-02-10 20:17:07 +00:00
|
|
|
lvn["a"].emplace_back(); // another list
|
2017-07-03 23:12:09 +00:00
|
|
|
lvn["a"].back().emplace_back(); // add an array
|
2022-02-10 20:17:07 +00:00
|
|
|
lvn["b"].emplace_back(); // add a list
|
2017-07-03 23:12:09 +00:00
|
|
|
lvn["b"].back().emplace_back(); // add an array
|
|
|
|
lvn["b"].back().emplace_back(); // add another array
|
2022-08-01 13:18:48 +00:00
|
|
|
static std::vector<RValueCaster> lvv{2};
|
2017-07-03 23:12:09 +00:00
|
|
|
m.def("cast_lv_vector", []() -> const decltype(lvv) & { return lvv; });
|
|
|
|
m.def("cast_lv_array", []() -> const decltype(lva) & { return lva; });
|
|
|
|
m.def("cast_lv_map", []() -> const decltype(lvm) & { return lvm; });
|
|
|
|
m.def("cast_lv_nested", []() -> const decltype(lvn) & { return lvn; });
|
|
|
|
// #853:
|
|
|
|
m.def("cast_unique_ptr_vector", []() {
|
|
|
|
std::vector<std::unique_ptr<UserType>> v;
|
|
|
|
v.emplace_back(new UserType{7});
|
|
|
|
v.emplace_back(new UserType{42});
|
|
|
|
return v;
|
|
|
|
});
|
|
|
|
|
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
|
|
|
pybind11::enum_<EnumType>(m, "EnumType")
|
|
|
|
.value("kSet", EnumType::kSet)
|
|
|
|
.value("kUnset", EnumType::kUnset);
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_move_out_container
|
2017-06-08 22:44:49 +00:00
|
|
|
struct MoveOutContainer {
|
2022-02-10 20:17:07 +00:00
|
|
|
struct Value {
|
|
|
|
int value;
|
|
|
|
};
|
2017-06-08 22:44:49 +00:00
|
|
|
std::list<Value> move_list() const { return {{0}, {1}, {2}}; }
|
|
|
|
};
|
|
|
|
py::class_<MoveOutContainer::Value>(m, "MoveOutContainerValue")
|
|
|
|
.def_readonly("value", &MoveOutContainer::Value::value);
|
|
|
|
py::class_<MoveOutContainer>(m, "MoveOutContainer")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def_property_readonly("move_list", &MoveOutContainer::move_list);
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// Class that can be move- and copy-constructed, but not assigned
|
|
|
|
struct NoAssign {
|
|
|
|
int value;
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
explicit NoAssign(int value = 0) : value(value) {}
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
NoAssign(const NoAssign &) = default;
|
|
|
|
NoAssign(NoAssign &&) = default;
|
|
|
|
|
|
|
|
NoAssign &operator=(const NoAssign &) = delete;
|
|
|
|
NoAssign &operator=(NoAssign &&) = delete;
|
|
|
|
};
|
2017-06-08 22:44:49 +00:00
|
|
|
py::class_<NoAssign>(m, "NoAssign", "Class with no C++ assignment operators")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def(py::init<int>());
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
struct MoveOutDetector {
|
2020-03-14 13:15:12 +00:00
|
|
|
MoveOutDetector() = default;
|
2022-02-10 20:17:07 +00:00
|
|
|
MoveOutDetector(const MoveOutDetector &) = default;
|
|
|
|
MoveOutDetector(MoveOutDetector &&other) noexcept : initialized(other.initialized) {
|
2020-03-14 13:15:12 +00:00
|
|
|
// steal underlying resource
|
|
|
|
other.initialized = false;
|
|
|
|
}
|
|
|
|
bool initialized = true;
|
|
|
|
};
|
|
|
|
py::class_<MoveOutDetector>(m, "MoveOutDetector", "Class with move tracking")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def_readonly("initialized", &MoveOutDetector::initialized);
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
#ifdef PYBIND11_HAS_OPTIONAL
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_optional
|
2017-06-08 22:44:49 +00:00
|
|
|
m.attr("has_optional") = true;
|
|
|
|
|
|
|
|
using opt_int = std::optional<int>;
|
|
|
|
using opt_no_assign = std::optional<NoAssign>;
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("double_or_zero", [](const opt_int &x) -> int { return x.value_or(0) * 2; });
|
2021-07-27 22:32:26 +00:00
|
|
|
m.def("half_or_none", [](int x) -> opt_int { return x != 0 ? opt_int(x / 2) : opt_int(); });
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def(
|
|
|
|
"test_nullopt",
|
|
|
|
[](opt_int x) { return x.value_or(42); },
|
|
|
|
py::arg_v("x", std::nullopt, "None"));
|
|
|
|
m.def(
|
|
|
|
"test_no_assign",
|
|
|
|
[](const opt_no_assign &x) { return x ? x->value : 42; },
|
|
|
|
py::arg_v("x", std::nullopt, "None"));
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
|
|
m.def("nodefer_none_optional", [](std::optional<int>) { return true; });
|
2021-07-12 20:10:28 +00:00
|
|
|
m.def("nodefer_none_optional", [](const py::none &) { return false; });
|
2020-03-14 13:15:12 +00:00
|
|
|
|
|
|
|
using opt_holder = OptionalHolder<std::optional, MoveOutDetector>;
|
|
|
|
py::class_<opt_holder>(m, "OptionalHolder", "Class with optional member")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def_readonly("member", &opt_holder::member)
|
|
|
|
.def("member_initialized", &opt_holder::member_initialized);
|
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 opt_props = OptionalProperties<std::optional>;
|
|
|
|
pybind11::class_<opt_props>(m, "OptionalProperties")
|
|
|
|
.def(pybind11::init<>())
|
|
|
|
.def_property_readonly("access_by_ref", &opt_props::access_by_ref)
|
|
|
|
.def_property_readonly("access_by_copy", &opt_props::access_by_copy);
|
2017-06-08 22:44:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PYBIND11_HAS_EXP_OPTIONAL
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_exp_optional
|
2017-06-08 22:44:49 +00:00
|
|
|
m.attr("has_exp_optional") = true;
|
|
|
|
|
|
|
|
using exp_opt_int = std::experimental::optional<int>;
|
|
|
|
using exp_opt_no_assign = std::experimental::optional<NoAssign>;
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("double_or_zero_exp", [](const exp_opt_int &x) -> int { return x.value_or(0) * 2; });
|
|
|
|
m.def("half_or_none_exp",
|
|
|
|
[](int x) -> exp_opt_int { return x ? exp_opt_int(x / 2) : exp_opt_int(); });
|
|
|
|
m.def(
|
|
|
|
"test_nullopt_exp",
|
|
|
|
[](exp_opt_int x) { return x.value_or(42); },
|
|
|
|
py::arg_v("x", std::experimental::nullopt, "None"));
|
|
|
|
m.def(
|
|
|
|
"test_no_assign_exp",
|
|
|
|
[](const exp_opt_no_assign &x) { return x ? x->value : 42; },
|
|
|
|
py::arg_v("x", std::experimental::nullopt, "None"));
|
2020-03-14 13:15:12 +00:00
|
|
|
|
|
|
|
using opt_exp_holder = OptionalHolder<std::experimental::optional, MoveOutDetector>;
|
|
|
|
py::class_<opt_exp_holder>(m, "OptionalExpHolder", "Class with optional member")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def_readonly("member", &opt_exp_holder::member)
|
|
|
|
.def("member_initialized", &opt_exp_holder::member_initialized);
|
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 opt_exp_props = OptionalProperties<std::experimental::optional>;
|
|
|
|
pybind11::class_<opt_exp_props>(m, "OptionalExpProperties")
|
|
|
|
.def(pybind11::init<>())
|
|
|
|
.def_property_readonly("access_by_ref", &opt_exp_props::access_by_ref)
|
|
|
|
.def_property_readonly("access_by_copy", &opt_exp_props::access_by_copy);
|
2017-06-08 22:44:49 +00:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
#if defined(PYBIND11_TEST_BOOST)
|
|
|
|
// test_boost_optional
|
|
|
|
m.attr("has_boost_optional") = true;
|
|
|
|
|
|
|
|
using boost_opt_int = boost::optional<int>;
|
|
|
|
using boost_opt_no_assign = boost::optional<NoAssign>;
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("double_or_zero_boost", [](const boost_opt_int &x) -> int { return x.value_or(0) * 2; });
|
|
|
|
m.def("half_or_none_boost",
|
|
|
|
[](int x) -> boost_opt_int { return x != 0 ? boost_opt_int(x / 2) : boost_opt_int(); });
|
|
|
|
m.def(
|
|
|
|
"test_nullopt_boost",
|
|
|
|
[](boost_opt_int x) { return x.value_or(42); },
|
|
|
|
py::arg_v("x", boost::none, "None"));
|
|
|
|
m.def(
|
|
|
|
"test_no_assign_boost",
|
|
|
|
[](const boost_opt_no_assign &x) { return x ? x->value : 42; },
|
|
|
|
py::arg_v("x", boost::none, "None"));
|
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 opt_boost_holder = OptionalHolder<boost::optional, MoveOutDetector>;
|
|
|
|
py::class_<opt_boost_holder>(m, "OptionalBoostHolder", "Class with optional member")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def_readonly("member", &opt_boost_holder::member)
|
|
|
|
.def("member_initialized", &opt_boost_holder::member_initialized);
|
|
|
|
|
|
|
|
using opt_boost_props = OptionalProperties<boost::optional>;
|
|
|
|
pybind11::class_<opt_boost_props>(m, "OptionalBoostProperties")
|
|
|
|
.def(pybind11::init<>())
|
|
|
|
.def_property_readonly("access_by_ref", &opt_boost_props::access_by_ref)
|
|
|
|
.def_property_readonly("access_by_copy", &opt_boost_props::access_by_copy);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// test_refsensitive_optional
|
|
|
|
using refsensitive_opt_int = ReferenceSensitiveOptional<int>;
|
|
|
|
using refsensitive_opt_no_assign = ReferenceSensitiveOptional<NoAssign>;
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("double_or_zero_refsensitive",
|
|
|
|
[](const refsensitive_opt_int &x) -> int { return (x ? x.value() : 0) * 2; });
|
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
|
|
|
m.def("half_or_none_refsensitive", [](int x) -> refsensitive_opt_int {
|
|
|
|
return x != 0 ? refsensitive_opt_int(x / 2) : refsensitive_opt_int();
|
|
|
|
});
|
2022-02-09 14:32:41 +00:00
|
|
|
m.def(
|
|
|
|
"test_nullopt_refsensitive",
|
|
|
|
// NOLINTNEXTLINE(performance-unnecessary-value-param)
|
|
|
|
[](refsensitive_opt_int x) { return x ? x.value() : 42; },
|
|
|
|
py::arg_v("x", refsensitive_opt_int(), "None"));
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def(
|
|
|
|
"test_no_assign_refsensitive",
|
|
|
|
[](const refsensitive_opt_no_assign &x) { return x ? x->value : 42; },
|
|
|
|
py::arg_v("x", refsensitive_opt_no_assign(), "None"));
|
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 opt_refsensitive_holder = OptionalHolder<ReferenceSensitiveOptional, MoveOutDetector>;
|
2022-02-10 20:17:07 +00:00
|
|
|
py::class_<opt_refsensitive_holder>(
|
|
|
|
m, "OptionalRefSensitiveHolder", "Class with optional member")
|
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
|
|
|
.def(py::init<>())
|
|
|
|
.def_readonly("member", &opt_refsensitive_holder::member)
|
|
|
|
.def("member_initialized", &opt_refsensitive_holder::member_initialized);
|
|
|
|
|
|
|
|
using opt_refsensitive_props = OptionalProperties<ReferenceSensitiveOptional>;
|
|
|
|
pybind11::class_<opt_refsensitive_props>(m, "OptionalRefSensitiveProperties")
|
|
|
|
.def(pybind11::init<>())
|
|
|
|
.def_property_readonly("access_by_ref", &opt_refsensitive_props::access_by_ref)
|
|
|
|
.def_property_readonly("access_by_copy", &opt_refsensitive_props::access_by_copy);
|
|
|
|
|
2021-07-02 14:00:50 +00:00
|
|
|
#ifdef PYBIND11_HAS_FILESYSTEM
|
|
|
|
// test_fs_path
|
|
|
|
m.attr("has_filesystem") = true;
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("parent_path", [](const std::filesystem::path &p) { return p.parent_path(); });
|
2021-07-02 14:00:50 +00:00
|
|
|
#endif
|
|
|
|
|
2022-03-22 05:58:04 +00:00
|
|
|
#ifdef PYBIND11_TEST_VARIANT
|
2017-08-08 14:02:31 +00:00
|
|
|
static_assert(std::is_same<py::detail::variant_caster_visitor::result_type, py::handle>::value,
|
|
|
|
"visitor::result_type is required by boost::variant in C++11 mode");
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
struct visitor {
|
2017-08-08 14:02:31 +00:00
|
|
|
using result_type = const char *;
|
|
|
|
|
|
|
|
result_type operator()(int) { return "int"; }
|
2021-06-22 16:11:54 +00:00
|
|
|
result_type operator()(const std::string &) { return "std::string"; }
|
2017-08-08 14:02:31 +00:00
|
|
|
result_type operator()(double) { return "double"; }
|
|
|
|
result_type operator()(std::nullptr_t) { return "std::nullptr_t"; }
|
2022-03-22 05:58:04 +00:00
|
|
|
# if defined(PYBIND11_HAS_VARIANT)
|
|
|
|
result_type operator()(std::monostate) { return "std::monostate"; }
|
|
|
|
# endif
|
2017-06-08 22:44:49 +00:00
|
|
|
};
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_variant
|
2021-06-22 16:11:54 +00:00
|
|
|
m.def("load_variant", [](const variant<int, std::string, double, std::nullptr_t> &v) {
|
2017-08-08 14:02:31 +00:00
|
|
|
return py::detail::visit_helper<variant>::call(visitor(), v);
|
2017-06-08 22:44:49 +00:00
|
|
|
});
|
2017-08-08 14:02:31 +00:00
|
|
|
m.def("load_variant_2pass", [](variant<double, int> v) {
|
|
|
|
return py::detail::visit_helper<variant>::call(visitor(), v);
|
2017-06-08 22:44:49 +00:00
|
|
|
});
|
|
|
|
m.def("cast_variant", []() {
|
2017-08-08 14:02:31 +00:00
|
|
|
using V = variant<int, std::string>;
|
2017-06-08 22:44:49 +00:00
|
|
|
return py::make_tuple(V(5), V("Hello"));
|
|
|
|
});
|
2022-03-22 05:58:04 +00:00
|
|
|
|
|
|
|
# if defined(PYBIND11_HAS_VARIANT)
|
|
|
|
// std::monostate tests.
|
|
|
|
m.def("load_monostate_variant",
|
|
|
|
[](const variant<std::monostate, int, std::string> &v) -> const char * {
|
|
|
|
return py::detail::visit_helper<variant>::call(visitor(), v);
|
|
|
|
});
|
|
|
|
m.def("cast_monostate_variant", []() {
|
|
|
|
using V = variant<std::monostate, int, std::string>;
|
|
|
|
return py::make_tuple(V{}, V(5), V("Hello"));
|
|
|
|
});
|
|
|
|
# endif
|
2017-06-08 22:44:49 +00:00
|
|
|
#endif
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// #528: templated constructor
|
|
|
|
// (no python tests: the test here is that this compiles)
|
2017-06-08 22:44:49 +00:00
|
|
|
m.def("tpl_ctor_vector", [](std::vector<TplCtorClass> &) {});
|
|
|
|
m.def("tpl_ctor_map", [](std::unordered_map<TplCtorClass, TplCtorClass> &) {});
|
|
|
|
m.def("tpl_ctor_set", [](std::unordered_set<TplCtorClass> &) {});
|
|
|
|
#if defined(PYBIND11_HAS_OPTIONAL)
|
|
|
|
m.def("tpl_constr_optional", [](std::optional<TplCtorClass> &) {});
|
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
|
|
|
#endif
|
|
|
|
#if defined(PYBIND11_HAS_EXP_OPTIONAL)
|
|
|
|
m.def("tpl_constr_optional_exp", [](std::experimental::optional<TplCtorClass> &) {});
|
|
|
|
#endif
|
|
|
|
#if defined(PYBIND11_TEST_BOOST)
|
|
|
|
m.def("tpl_constr_optional_boost", [](boost::optional<TplCtorClass> &) {});
|
2017-06-08 22:44:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// test_vec_of_reference_wrapper
|
|
|
|
// #171: Can't return STL structures containing reference wrapper
|
|
|
|
m.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<UserType> p4) {
|
|
|
|
static UserType p1{1}, p2{2}, p3{3};
|
2022-02-10 20:17:07 +00:00
|
|
|
return std::vector<std::reference_wrapper<UserType>>{
|
|
|
|
std::ref(p1), std::ref(p2), std::ref(p3), p4};
|
2017-06-08 22:44:49 +00:00
|
|
|
});
|
|
|
|
|
2017-05-09 13:01:22 +00:00
|
|
|
// test_stl_pass_by_pointer
|
2024-04-02 15:14:08 +00:00
|
|
|
m.def("stl_pass_by_pointer", [](std::vector<int> *v) { return *v; }, "v"_a = nullptr);
|
2018-07-17 14:56:26 +00:00
|
|
|
|
2018-10-11 08:28:12 +00:00
|
|
|
// #1258: pybind11/stl.h converts string to vector<string>
|
2021-07-12 20:10:28 +00:00
|
|
|
m.def("func_with_string_or_vector_string_arg_overload",
|
|
|
|
[](const std::vector<std::string> &) { return 1; });
|
|
|
|
m.def("func_with_string_or_vector_string_arg_overload",
|
|
|
|
[](const std::list<std::string> &) { return 2; });
|
|
|
|
m.def("func_with_string_or_vector_string_arg_overload", [](const std::string &) { return 3; });
|
2018-10-11 08:28:12 +00:00
|
|
|
|
2018-07-17 14:56:26 +00:00
|
|
|
class Placeholder {
|
|
|
|
public:
|
|
|
|
Placeholder() { print_created(this); }
|
|
|
|
Placeholder(const Placeholder &) = delete;
|
|
|
|
~Placeholder() { print_destroyed(this); }
|
|
|
|
};
|
|
|
|
py::class_<Placeholder>(m, "Placeholder");
|
|
|
|
|
|
|
|
/// test_stl_vector_ownership
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def(
|
|
|
|
"test_stl_ownership",
|
|
|
|
[]() {
|
|
|
|
std::vector<Placeholder *> result;
|
|
|
|
result.push_back(new Placeholder());
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
py::return_value_policy::take_ownership);
|
2018-11-09 11:32:48 +00:00
|
|
|
|
|
|
|
m.def("array_cast_sequence", [](std::array<int, 3> x) { return x; });
|
2018-11-09 19:12:46 +00:00
|
|
|
|
|
|
|
/// test_issue_1561
|
2022-02-10 20:17:07 +00:00
|
|
|
struct Issue1561Inner {
|
|
|
|
std::string data;
|
|
|
|
};
|
|
|
|
struct Issue1561Outer {
|
|
|
|
std::vector<Issue1561Inner> list;
|
|
|
|
};
|
2018-11-09 19:12:46 +00:00
|
|
|
|
|
|
|
py::class_<Issue1561Inner>(m, "Issue1561Inner")
|
|
|
|
.def(py::init<std::string>())
|
|
|
|
.def_readwrite("data", &Issue1561Inner::data);
|
|
|
|
|
|
|
|
py::class_<Issue1561Outer>(m, "Issue1561Outer")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def_readwrite("list", &Issue1561Outer::list);
|
2021-07-12 23:56:10 +00:00
|
|
|
|
|
|
|
m.def(
|
|
|
|
"return_vector_bool_raw_ptr",
|
|
|
|
[]() { return new std::vector<bool>(4513); },
|
|
|
|
// Without explicitly specifying `take_ownership`, this function leaks.
|
|
|
|
py::return_value_policy::take_ownership);
|
2017-06-08 22:44:49 +00:00
|
|
|
}
|