2017-06-08 22:44:49 +00:00
|
|
|
|
/*
|
|
|
|
|
tests/test_builtin_casters.cpp -- Casters available without any additional headers
|
|
|
|
|
|
|
|
|
|
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/complex.h>
|
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
|
Adjusting `type_caster<std::reference_wrapper<T>>` to support const/non-const propagation in `cast_op`. (#2705)
* Allow type_caster of std::reference_wrapper<T> to be the same as a native reference.
Before, both std::reference_wrapper<T> and std::reference_wrapper<const T> would
invoke cast_op<type>. This doesn't allow the type_caster<> specialization for T
to distinguish reference_wrapper types from value types.
After, the type_caster<> specialization invokes cast_op<type&>, which allows
reference_wrapper to behave in the same way as a native reference type.
* Add tests/examples for std::reference_wrapper<const T>
* Add tests which use mutable/immutable variants
This test is a chimera; it blends the pybind11 casters with a custom
pytype implementation that supports immutable and mutable calls.
In order to detect the immutable/mutable state, the cast_op needs
to propagate it, even through e.g. std::reference<const T>
Note: This is still a work in progress; some things are crashing,
which likely means that I have a refcounting bug or something else
missing.
* Add/finish tests that distinguish const& from &
Fixes the bugs in my custom python type implementation,
demonstrate test that requires const& and reference_wrapper<const T>
being treated differently from Non-const.
* Add passing a const to non-const method.
* Demonstrate non-const conversion of reference_wrapper in tests.
Apply formatting presubmit check.
* Fix build errors from presubmit checks.
* Try and fix a few more CI errors
* More CI fixes.
* More CI fixups.
* Try and get PyPy to work.
* Additional minor fixups. Getting close to CI green.
* More ci fixes?
* fix clang-tidy warnings from presubmit
* fix more clang-tidy warnings
* minor comment and consistency cleanups
* PyDECREF -> Py_DECREF
* copy/move constructors
* Resolve codereview comments
* more review comment fixes
* review comments: remove spurious &
* Make the test fail even when the static_assert is commented out.
This expands the test_freezable_type_caster a bit by:
1/ adding accessors .is_immutable and .addr to compare identity
from python.
2/ Changing the default cast_op of the type_caster<> specialization
to return a non-const value. In normal codepaths this is a reasonable
default.
3/ adding roundtrip variants to exercise the by reference, by pointer
and by reference_wrapper in all call paths. In conjunction with 2/, this
demonstrates the failure case of the existing std::reference_wrpper conversion,
which now loses const in a similar way that happens when using the default cast_op_type<>.
* apply presubmit formatting
* Revert inclusion of test_freezable_type_caster
There's some concern that this test is a bit unwieldly because of the use
of the raw <Python.h> functions. Removing for now.
* Add a test that validates const references propagation.
This test verifies that cast_op may be used to correctly detect
const reference types when used with std::reference_wrapper.
* mend
* Review comments based changes.
1. std::add_lvalue_reference<type> -> type&
2. Simplify the test a little more; we're never returning the ConstRefCaster
type so the class_ definition can be removed.
* formatted files again.
* Move const_ref_caster test to builtin_casters
* Review comments: use cast_op and adjust some comments.
* Simplify ConstRefCasted test
I like this version better as it moves the assertion that matters
back into python.
2020-12-16 00:53:55 +00:00
|
|
|
|
struct ConstRefCasted {
|
2022-02-10 20:17:07 +00:00
|
|
|
|
int tag;
|
Adjusting `type_caster<std::reference_wrapper<T>>` to support const/non-const propagation in `cast_op`. (#2705)
* Allow type_caster of std::reference_wrapper<T> to be the same as a native reference.
Before, both std::reference_wrapper<T> and std::reference_wrapper<const T> would
invoke cast_op<type>. This doesn't allow the type_caster<> specialization for T
to distinguish reference_wrapper types from value types.
After, the type_caster<> specialization invokes cast_op<type&>, which allows
reference_wrapper to behave in the same way as a native reference type.
* Add tests/examples for std::reference_wrapper<const T>
* Add tests which use mutable/immutable variants
This test is a chimera; it blends the pybind11 casters with a custom
pytype implementation that supports immutable and mutable calls.
In order to detect the immutable/mutable state, the cast_op needs
to propagate it, even through e.g. std::reference<const T>
Note: This is still a work in progress; some things are crashing,
which likely means that I have a refcounting bug or something else
missing.
* Add/finish tests that distinguish const& from &
Fixes the bugs in my custom python type implementation,
demonstrate test that requires const& and reference_wrapper<const T>
being treated differently from Non-const.
* Add passing a const to non-const method.
* Demonstrate non-const conversion of reference_wrapper in tests.
Apply formatting presubmit check.
* Fix build errors from presubmit checks.
* Try and fix a few more CI errors
* More CI fixes.
* More CI fixups.
* Try and get PyPy to work.
* Additional minor fixups. Getting close to CI green.
* More ci fixes?
* fix clang-tidy warnings from presubmit
* fix more clang-tidy warnings
* minor comment and consistency cleanups
* PyDECREF -> Py_DECREF
* copy/move constructors
* Resolve codereview comments
* more review comment fixes
* review comments: remove spurious &
* Make the test fail even when the static_assert is commented out.
This expands the test_freezable_type_caster a bit by:
1/ adding accessors .is_immutable and .addr to compare identity
from python.
2/ Changing the default cast_op of the type_caster<> specialization
to return a non-const value. In normal codepaths this is a reasonable
default.
3/ adding roundtrip variants to exercise the by reference, by pointer
and by reference_wrapper in all call paths. In conjunction with 2/, this
demonstrates the failure case of the existing std::reference_wrpper conversion,
which now loses const in a similar way that happens when using the default cast_op_type<>.
* apply presubmit formatting
* Revert inclusion of test_freezable_type_caster
There's some concern that this test is a bit unwieldly because of the use
of the raw <Python.h> functions. Removing for now.
* Add a test that validates const references propagation.
This test verifies that cast_op may be used to correctly detect
const reference types when used with std::reference_wrapper.
* mend
* Review comments based changes.
1. std::add_lvalue_reference<type> -> type&
2. Simplify the test a little more; we're never returning the ConstRefCaster
type so the class_ definition can be removed.
* formatted files again.
* Move const_ref_caster test to builtin_casters
* Review comments: use cast_op and adjust some comments.
* Simplify ConstRefCasted test
I like this version better as it moves the assertion that matters
back into python.
2020-12-16 00:53:55 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PYBIND11_NAMESPACE_BEGIN(pybind11)
|
|
|
|
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
|
|
|
|
template <>
|
|
|
|
|
class type_caster<ConstRefCasted> {
|
2022-02-10 20:17:07 +00:00
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = const_name<ConstRefCasted>();
|
|
|
|
|
|
|
|
|
|
// Input is unimportant, a new value will always be constructed based on the
|
|
|
|
|
// cast operator.
|
|
|
|
|
bool load(handle, bool) { return true; }
|
|
|
|
|
|
|
|
|
|
explicit operator ConstRefCasted &&() {
|
|
|
|
|
value = {1};
|
|
|
|
|
// NOLINTNEXTLINE(performance-move-const-arg)
|
|
|
|
|
return std::move(value);
|
|
|
|
|
}
|
|
|
|
|
explicit operator ConstRefCasted &() {
|
|
|
|
|
value = {2};
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
explicit operator ConstRefCasted *() {
|
|
|
|
|
value = {3};
|
|
|
|
|
return &value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
explicit operator const ConstRefCasted &() {
|
|
|
|
|
value = {4};
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
explicit operator const ConstRefCasted *() {
|
|
|
|
|
value = {5};
|
|
|
|
|
return &value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// custom cast_op to explicitly propagate types to the conversion operators.
|
|
|
|
|
template <typename T_>
|
|
|
|
|
using cast_op_type =
|
|
|
|
|
/// const
|
|
|
|
|
conditional_t<
|
|
|
|
|
std::is_same<remove_reference_t<T_>, const ConstRefCasted *>::value,
|
|
|
|
|
const ConstRefCasted *,
|
|
|
|
|
conditional_t<
|
|
|
|
|
std::is_same<T_, const ConstRefCasted &>::value,
|
|
|
|
|
const ConstRefCasted &,
|
|
|
|
|
/// non-const
|
|
|
|
|
conditional_t<std::is_same<remove_reference_t<T_>, ConstRefCasted *>::value,
|
|
|
|
|
ConstRefCasted *,
|
|
|
|
|
conditional_t<std::is_same<T_, ConstRefCasted &>::value,
|
|
|
|
|
ConstRefCasted &,
|
|
|
|
|
/* else */ ConstRefCasted &&>>>>;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ConstRefCasted value = {0};
|
Adjusting `type_caster<std::reference_wrapper<T>>` to support const/non-const propagation in `cast_op`. (#2705)
* Allow type_caster of std::reference_wrapper<T> to be the same as a native reference.
Before, both std::reference_wrapper<T> and std::reference_wrapper<const T> would
invoke cast_op<type>. This doesn't allow the type_caster<> specialization for T
to distinguish reference_wrapper types from value types.
After, the type_caster<> specialization invokes cast_op<type&>, which allows
reference_wrapper to behave in the same way as a native reference type.
* Add tests/examples for std::reference_wrapper<const T>
* Add tests which use mutable/immutable variants
This test is a chimera; it blends the pybind11 casters with a custom
pytype implementation that supports immutable and mutable calls.
In order to detect the immutable/mutable state, the cast_op needs
to propagate it, even through e.g. std::reference<const T>
Note: This is still a work in progress; some things are crashing,
which likely means that I have a refcounting bug or something else
missing.
* Add/finish tests that distinguish const& from &
Fixes the bugs in my custom python type implementation,
demonstrate test that requires const& and reference_wrapper<const T>
being treated differently from Non-const.
* Add passing a const to non-const method.
* Demonstrate non-const conversion of reference_wrapper in tests.
Apply formatting presubmit check.
* Fix build errors from presubmit checks.
* Try and fix a few more CI errors
* More CI fixes.
* More CI fixups.
* Try and get PyPy to work.
* Additional minor fixups. Getting close to CI green.
* More ci fixes?
* fix clang-tidy warnings from presubmit
* fix more clang-tidy warnings
* minor comment and consistency cleanups
* PyDECREF -> Py_DECREF
* copy/move constructors
* Resolve codereview comments
* more review comment fixes
* review comments: remove spurious &
* Make the test fail even when the static_assert is commented out.
This expands the test_freezable_type_caster a bit by:
1/ adding accessors .is_immutable and .addr to compare identity
from python.
2/ Changing the default cast_op of the type_caster<> specialization
to return a non-const value. In normal codepaths this is a reasonable
default.
3/ adding roundtrip variants to exercise the by reference, by pointer
and by reference_wrapper in all call paths. In conjunction with 2/, this
demonstrates the failure case of the existing std::reference_wrpper conversion,
which now loses const in a similar way that happens when using the default cast_op_type<>.
* apply presubmit formatting
* Revert inclusion of test_freezable_type_caster
There's some concern that this test is a bit unwieldly because of the use
of the raw <Python.h> functions. Removing for now.
* Add a test that validates const references propagation.
This test verifies that cast_op may be used to correctly detect
const reference types when used with std::reference_wrapper.
* mend
* Review comments based changes.
1. std::add_lvalue_reference<type> -> type&
2. Simplify the test a little more; we're never returning the ConstRefCaster
type so the class_ definition can be removed.
* formatted files again.
* Move const_ref_caster test to builtin_casters
* Review comments: use cast_op and adjust some comments.
* Simplify ConstRefCasted test
I like this version better as it moves the assertion that matters
back into python.
2020-12-16 00:53:55 +00:00
|
|
|
|
};
|
|
|
|
|
PYBIND11_NAMESPACE_END(detail)
|
|
|
|
|
PYBIND11_NAMESPACE_END(pybind11)
|
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
|
TEST_SUBMODULE(builtin_casters, m) {
|
|
|
|
|
// test_simple_string
|
|
|
|
|
m.def("string_roundtrip", [](const char *s) { return s; });
|
|
|
|
|
|
|
|
|
|
// test_unicode_conversion
|
2022-02-10 20:17:07 +00:00
|
|
|
|
// Some test characters in utf16 and utf32 encodings. The last one (the 𝐀) contains a null
|
|
|
|
|
// byte
|
|
|
|
|
char32_t a32 = 0x61 /*a*/, z32 = 0x7a /*z*/, ib32 = 0x203d /*‽*/, cake32 = 0x1f382 /*🎂*/,
|
|
|
|
|
mathbfA32 = 0x1d400 /*𝐀*/;
|
|
|
|
|
char16_t b16 = 0x62 /*b*/, z16 = 0x7a, ib16 = 0x203d, cake16_1 = 0xd83c, cake16_2 = 0xdf82,
|
|
|
|
|
mathbfA16_1 = 0xd835, mathbfA16_2 = 0xdc00;
|
2017-06-08 22:44:49 +00:00
|
|
|
|
std::wstring wstr;
|
2022-02-10 20:17:07 +00:00
|
|
|
|
wstr.push_back(0x61); // a
|
2017-06-08 22:44:49 +00:00
|
|
|
|
wstr.push_back(0x2e18); // ⸘
|
2022-02-10 20:17:07 +00:00
|
|
|
|
if (PYBIND11_SILENCE_MSVC_C4127(sizeof(wchar_t) == 2)) {
|
|
|
|
|
wstr.push_back(mathbfA16_1);
|
|
|
|
|
wstr.push_back(mathbfA16_2);
|
|
|
|
|
} // 𝐀, utf16
|
|
|
|
|
else {
|
|
|
|
|
wstr.push_back((wchar_t) mathbfA32);
|
|
|
|
|
} // 𝐀, utf32
|
2017-06-08 22:44:49 +00:00
|
|
|
|
wstr.push_back(0x7a); // z
|
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("good_utf8_string", []() {
|
|
|
|
|
return std::string((const char *) u8"Say utf8\u203d \U0001f382 \U0001d400");
|
|
|
|
|
}); // Say utf8‽ 🎂 𝐀
|
|
|
|
|
m.def("good_utf16_string", [=]() {
|
|
|
|
|
return std::u16string({b16, ib16, cake16_1, cake16_2, mathbfA16_1, mathbfA16_2, z16});
|
|
|
|
|
}); // b‽🎂𝐀z
|
|
|
|
|
m.def("good_utf32_string", [=]() {
|
|
|
|
|
return std::u32string({a32, mathbfA32, cake32, ib32, z32});
|
|
|
|
|
}); // a𝐀🎂‽z
|
2017-06-08 22:44:49 +00:00
|
|
|
|
m.def("good_wchar_string", [=]() { return wstr; }); // a‽𝐀z
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("bad_utf8_string", []() {
|
|
|
|
|
return std::string("abc\xd0"
|
|
|
|
|
"def");
|
|
|
|
|
});
|
|
|
|
|
m.def("bad_utf16_string", [=]() { return std::u16string({b16, char16_t(0xd800), z16}); });
|
2022-02-11 02:28:08 +00:00
|
|
|
|
// Under Python 2.7, invalid unicode UTF-32 characters didn't appear to trigger
|
2022-02-10 20:17:07 +00:00
|
|
|
|
// UnicodeDecodeError
|
|
|
|
|
m.def("bad_utf32_string", [=]() { return std::u32string({a32, char32_t(0xd800), z32}); });
|
2022-02-08 00:23:20 +00:00
|
|
|
|
if (PYBIND11_SILENCE_MSVC_C4127(sizeof(wchar_t) == 2)) {
|
|
|
|
|
m.def("bad_wchar_string", [=]() {
|
|
|
|
|
return std::wstring({wchar_t(0x61), wchar_t(0xd800)});
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-06-08 22:44:49 +00:00
|
|
|
|
m.def("u8_Z", []() -> char { return 'Z'; });
|
|
|
|
|
m.def("u8_eacute", []() -> char { return '\xe9'; });
|
|
|
|
|
m.def("u16_ibang", [=]() -> char16_t { return ib16; });
|
|
|
|
|
m.def("u32_mathbfA", [=]() -> char32_t { return mathbfA32; });
|
|
|
|
|
m.def("wchar_heart", []() -> wchar_t { return 0x2665; });
|
|
|
|
|
|
|
|
|
|
// test_single_char_arguments
|
|
|
|
|
m.attr("wchar_size") = py::cast(sizeof(wchar_t));
|
|
|
|
|
m.def("ord_char", [](char c) -> int { return static_cast<unsigned char>(c); });
|
2017-10-06 14:50:10 +00:00
|
|
|
|
m.def("ord_char_lv", [](char &c) -> int { return static_cast<unsigned char>(c); });
|
2017-06-08 22:44:49 +00:00
|
|
|
|
m.def("ord_char16", [](char16_t c) -> uint16_t { return c; });
|
2017-10-06 14:50:10 +00:00
|
|
|
|
m.def("ord_char16_lv", [](char16_t &c) -> uint16_t { return c; });
|
2017-06-08 22:44:49 +00:00
|
|
|
|
m.def("ord_char32", [](char32_t c) -> uint32_t { return c; });
|
|
|
|
|
m.def("ord_wchar", [](wchar_t c) -> int { return c; });
|
|
|
|
|
|
|
|
|
|
// test_bytes_to_string
|
|
|
|
|
m.def("strlen", [](char *s) { return strlen(s); });
|
2021-07-12 20:10:28 +00:00
|
|
|
|
m.def("string_length", [](const std::string &s) { return s.length(); });
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
2019-12-19 11:16:24 +00:00
|
|
|
|
#ifdef PYBIND11_HAS_U8STRING
|
|
|
|
|
m.attr("has_u8string") = true;
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("good_utf8_u8string", []() {
|
|
|
|
|
return std::u8string(u8"Say utf8\u203d \U0001f382 \U0001d400");
|
|
|
|
|
}); // Say utf8‽ 🎂 𝐀
|
|
|
|
|
m.def("bad_utf8_u8string", []() {
|
|
|
|
|
return std::u8string((const char8_t *) "abc\xd0"
|
|
|
|
|
"def");
|
|
|
|
|
});
|
2019-12-19 11:16:24 +00:00
|
|
|
|
|
|
|
|
|
m.def("u8_char8_Z", []() -> char8_t { return u8'Z'; });
|
|
|
|
|
|
|
|
|
|
// test_single_char_arguments
|
|
|
|
|
m.def("ord_char8", [](char8_t c) -> int { return static_cast<unsigned char>(c); });
|
|
|
|
|
m.def("ord_char8_lv", [](char8_t &c) -> int { return static_cast<unsigned char>(c); });
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
|
// test_string_view
|
|
|
|
|
#ifdef PYBIND11_HAS_STRING_VIEW
|
|
|
|
|
m.attr("has_string_view") = true;
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("string_view_print", [](std::string_view s) { py::print(s, s.size()); });
|
2017-06-08 22:44:49 +00:00
|
|
|
|
m.def("string_view16_print", [](std::u16string_view s) { py::print(s, s.size()); });
|
|
|
|
|
m.def("string_view32_print", [](std::u32string_view s) { py::print(s, s.size()); });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("string_view_chars", [](std::string_view s) {
|
|
|
|
|
py::list l;
|
2022-02-08 00:23:20 +00:00
|
|
|
|
for (auto c : s) {
|
|
|
|
|
l.append((std::uint8_t) c);
|
|
|
|
|
}
|
2022-02-10 20:17:07 +00:00
|
|
|
|
return l;
|
|
|
|
|
});
|
|
|
|
|
m.def("string_view16_chars", [](std::u16string_view s) {
|
|
|
|
|
py::list l;
|
2022-02-08 00:23:20 +00:00
|
|
|
|
for (auto c : s) {
|
|
|
|
|
l.append((int) c);
|
|
|
|
|
}
|
2022-02-10 20:17:07 +00:00
|
|
|
|
return l;
|
|
|
|
|
});
|
|
|
|
|
m.def("string_view32_chars", [](std::u32string_view s) {
|
|
|
|
|
py::list l;
|
2022-02-08 00:23:20 +00:00
|
|
|
|
for (auto c : s) {
|
|
|
|
|
l.append((int) c);
|
|
|
|
|
}
|
2022-02-10 20:17:07 +00:00
|
|
|
|
return l;
|
|
|
|
|
});
|
|
|
|
|
m.def("string_view_return",
|
|
|
|
|
[]() { return std::string_view((const char *) u8"utf8 secret \U0001f382"); });
|
|
|
|
|
m.def("string_view16_return",
|
|
|
|
|
[]() { return std::u16string_view(u"utf16 secret \U0001f382"); });
|
|
|
|
|
m.def("string_view32_return",
|
|
|
|
|
[]() { return std::u32string_view(U"utf32 secret \U0001f382"); });
|
2019-12-19 11:16:24 +00:00
|
|
|
|
|
2021-12-03 17:20:32 +00:00
|
|
|
|
// The inner lambdas here are to also test implicit conversion
|
|
|
|
|
using namespace std::literals;
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("string_view_bytes",
|
|
|
|
|
[]() { return [](py::bytes b) { return b; }("abc \x80\x80 def"sv); });
|
|
|
|
|
m.def("string_view_str",
|
|
|
|
|
[]() { return [](py::str s) { return s; }("abc \342\200\275 def"sv); });
|
|
|
|
|
m.def("string_view_from_bytes",
|
|
|
|
|
[](const py::bytes &b) { return [](std::string_view s) { return s; }(b); });
|
2021-12-03 17:20:32 +00:00
|
|
|
|
m.def("string_view_memoryview", []() {
|
|
|
|
|
static constexpr auto val = "Have some \360\237\216\202"sv;
|
|
|
|
|
return py::memoryview::from_memory(val);
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
|
# ifdef PYBIND11_HAS_U8STRING
|
|
|
|
|
m.def("string_view8_print", [](std::u8string_view s) { py::print(s, s.size()); });
|
|
|
|
|
m.def("string_view8_chars", [](std::u8string_view s) {
|
|
|
|
|
py::list l;
|
|
|
|
|
for (auto c : s)
|
|
|
|
|
l.append((std::uint8_t) c);
|
|
|
|
|
return l;
|
|
|
|
|
});
|
2019-12-19 11:16:24 +00:00
|
|
|
|
m.def("string_view8_return", []() { return std::u8string_view(u8"utf8 secret \U0001f382"); });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("string_view8_str", []() { return py::str{std::u8string_view{u8"abc ‽ def"}}; });
|
|
|
|
|
# endif
|
2021-12-03 17:20:32 +00:00
|
|
|
|
|
|
|
|
|
struct TypeWithBothOperatorStringAndStringView {
|
|
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
|
|
|
operator std::string() const { return "success"; }
|
|
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
|
|
|
operator std::string_view() const { return "failure"; }
|
|
|
|
|
};
|
|
|
|
|
m.def("bytes_from_type_with_both_operator_string_and_string_view",
|
|
|
|
|
[]() { return py::bytes(TypeWithBothOperatorStringAndStringView()); });
|
|
|
|
|
m.def("str_from_type_with_both_operator_string_and_string_view",
|
|
|
|
|
[]() { return py::str(TypeWithBothOperatorStringAndStringView()); });
|
2017-06-08 22:44:49 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
Fix unsigned error value casting
When casting to an unsigned type from a python 2 `int`, we currently
cast using `(unsigned long long) PyLong_AsUnsignedLong(src.ptr())`.
If the Python cast fails, it returns (unsigned long) -1, but then we
cast this to `unsigned long long`, which means we get 4294967295, but
because that isn't equal to `(unsigned long long) -1`, we don't detect
the failure.
This commit moves the unsigned casting into a `detail::as_unsigned`
function which, upon error, casts -1 to the final type, and otherwise
casts the return value to the final type to avoid the problematic double
cast when an error occurs.
The error most commonly shows up wherever `long` is 32-bits (e.g. under
both 32- and 64-bit Windows, and under 32-bit linux) when passing a
negative value to a bound function taking an `unsigned long`.
Fixes #929.
The added tests also trigger a latent segfault under PyPy: when casting
to an integer smaller than `long` (e.g. casting to a `uint32_t` on a
64-bit `long` architecture) we check both for a Python error and also
that the resulting intermediate value will fit in the final type. If
there is no conversion error, but we get a value that would overflow, we
end up calling `PyErr_ExceptionMatches()` illegally: that call is only
allowed when there is a current exception. Under PyPy, this segfaults
the test suite. It doesn't appear to segfault under CPython, but the
documentation suggests that it *could* do so. The fix is to only check
for the exception match if we actually got an error.
2017-07-01 20:31:49 +00:00
|
|
|
|
// test_integer_casting
|
|
|
|
|
m.def("i32_str", [](std::int32_t v) { return std::to_string(v); });
|
|
|
|
|
m.def("u32_str", [](std::uint32_t v) { return std::to_string(v); });
|
|
|
|
|
m.def("i64_str", [](std::int64_t v) { return std::to_string(v); });
|
|
|
|
|
m.def("u64_str", [](std::uint64_t v) { return std::to_string(v); });
|
|
|
|
|
|
2021-01-17 01:52:14 +00:00
|
|
|
|
// test_int_convert
|
|
|
|
|
m.def("int_passthrough", [](int arg) { return arg; });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def(
|
|
|
|
|
"int_passthrough_noconvert", [](int arg) { return arg; }, py::arg{}.noconvert());
|
2021-01-17 01:52:14 +00:00
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
|
// test_tuple
|
2021-07-09 21:09:56 +00:00
|
|
|
|
m.def(
|
|
|
|
|
"pair_passthrough",
|
|
|
|
|
[](const std::pair<bool, std::string> &input) {
|
|
|
|
|
return std::make_pair(input.second, input.first);
|
|
|
|
|
},
|
|
|
|
|
"Return a pair in reversed order");
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def(
|
|
|
|
|
"tuple_passthrough",
|
|
|
|
|
[](std::tuple<bool, std::string, int> input) {
|
|
|
|
|
return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
|
|
|
|
|
},
|
|
|
|
|
"Return a triple in reversed order");
|
2017-07-04 18:57:41 +00:00
|
|
|
|
m.def("empty_tuple", []() { return std::tuple<>(); });
|
2017-07-03 23:12:09 +00:00
|
|
|
|
static std::pair<RValueCaster, RValueCaster> lvpair;
|
|
|
|
|
static std::tuple<RValueCaster, RValueCaster, RValueCaster> lvtuple;
|
2022-02-10 20:17:07 +00:00
|
|
|
|
static std::pair<RValueCaster, std::tuple<RValueCaster, std::pair<RValueCaster, RValueCaster>>>
|
|
|
|
|
lvnested;
|
2017-07-03 23:12:09 +00:00
|
|
|
|
m.def("rvalue_pair", []() { return std::make_pair(RValueCaster{}, RValueCaster{}); });
|
|
|
|
|
m.def("lvalue_pair", []() -> const decltype(lvpair) & { return lvpair; });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("rvalue_tuple",
|
|
|
|
|
[]() { return std::make_tuple(RValueCaster{}, RValueCaster{}, RValueCaster{}); });
|
2017-07-03 23:12:09 +00:00
|
|
|
|
m.def("lvalue_tuple", []() -> const decltype(lvtuple) & { return lvtuple; });
|
|
|
|
|
m.def("rvalue_nested", []() {
|
2022-02-10 20:17:07 +00:00
|
|
|
|
return std::make_pair(
|
|
|
|
|
RValueCaster{},
|
|
|
|
|
std::make_tuple(RValueCaster{}, std::make_pair(RValueCaster{}, RValueCaster{})));
|
|
|
|
|
});
|
2017-07-03 23:12:09 +00:00
|
|
|
|
m.def("lvalue_nested", []() -> const decltype(lvnested) & { return lvnested; });
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
2020-07-28 19:44:19 +00:00
|
|
|
|
static std::pair<int, std::string> int_string_pair{2, "items"};
|
|
|
|
|
m.def("int_string_pair", []() { return &int_string_pair; });
|
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
|
// test_builtins_cast_return_none
|
|
|
|
|
m.def("return_none_string", []() -> std::string * { return nullptr; });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("return_none_char", []() -> const char * { return nullptr; });
|
|
|
|
|
m.def("return_none_bool", []() -> bool * { return nullptr; });
|
|
|
|
|
m.def("return_none_int", []() -> int * { return nullptr; });
|
|
|
|
|
m.def("return_none_float", []() -> float * { return nullptr; });
|
|
|
|
|
m.def("return_none_pair", []() -> std::pair<int, int> * { return nullptr; });
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
|
|
|
|
// test_none_deferred
|
|
|
|
|
m.def("defer_none_cstring", [](char *) { return false; });
|
2021-07-12 20:10:28 +00:00
|
|
|
|
m.def("defer_none_cstring", [](const py::none &) { return true; });
|
2017-06-08 22:44:49 +00:00
|
|
|
|
m.def("defer_none_custom", [](UserType *) { return false; });
|
2021-07-12 20:10:28 +00:00
|
|
|
|
m.def("defer_none_custom", [](const py::none &) { return true; });
|
2017-06-08 22:44:49 +00:00
|
|
|
|
m.def("nodefer_none_void", [](void *) { return true; });
|
2021-07-12 20:10:28 +00:00
|
|
|
|
m.def("nodefer_none_void", [](const py::none &) { return false; });
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
|
|
|
|
// test_void_caster
|
|
|
|
|
m.def("load_nullptr_t", [](std::nullptr_t) {}); // not useful, but it should still compile
|
|
|
|
|
m.def("cast_nullptr_t", []() { return std::nullptr_t{}; });
|
|
|
|
|
|
2021-01-15 20:59:47 +00:00
|
|
|
|
// [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works.
|
|
|
|
|
|
2017-07-23 15:02:43 +00:00
|
|
|
|
// test_bool_caster
|
|
|
|
|
m.def("bool_passthrough", [](bool arg) { return arg; });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def(
|
|
|
|
|
"bool_passthrough_noconvert", [](bool arg) { return arg; }, py::arg{}.noconvert());
|
2017-07-23 15:02:43 +00:00
|
|
|
|
|
fix: Intel ICC C++17 compatibility (#2729)
* CI: Intel icc/icpc via oneAPI
Add testing for Intel icc/icpc via the oneAPI images.
Intel oneAPI is in a late beta stage, currently shipping
oneAPI beta09 with ICC 20.2.
CI: Skip Interpreter Tests for Intel
Cannot find how to add this, neiter the package `libc6-dev` nor
`intel-oneapi-mkl-devel` help when installed to solve this:
```
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - not found
CMake Error at /__t/cmake/3.18.4/x64/cmake-3.18.4-Linux-x86_64/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:165 (message):
Could NOT find Threads (missing: Threads_FOUND)
Call Stack (most recent call first):
/__t/cmake/3.18.4/x64/cmake-3.18.4-Linux-x86_64/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:458 (_FPHSA_FAILURE_MESSAGE)
/__t/cmake/3.18.4/x64/cmake-3.18.4-Linux-x86_64/share/cmake-3.18/Modules/FindThreads.cmake:234 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
tests/test_embed/CMakeLists.txt:17 (find_package)
```
CI: libc6-dev from GCC for ICC
CI: Run bare metal for oneAPI
CI: Ubuntu 18.04 for oneAPI
CI: Intel +Catch -Eigen
CI: CMake from Apt (ICC tests)
CI: Replace Intel Py with GCC Py
CI: Intel w/o GCC's Eigen
CI: ICC with verbose make
[Debug] Find core dump
tests: use arg{} instead of arg() for Intel
tests: adding a few more missing {}
fix: sync with @tobiasleibner's branch
fix: try ubuntu 20-04
fix: drop exit 1
docs: Apply suggestions from code review
Co-authored-by: Tobias Leibner <tobias.leibner@googlemail.com>
Workaround for ICC enable_if issues
Another workaround for ICC's enable_if issues
fix error in previous commit
Disable one test for the Intel compiler in C++17 mode
Add back one instance of py::arg().noconvert()
Add NOLINT to fix clang-tidy check
Work around for ICC internal error in PYBIND11_EXPAND_SIDE_EFFECTS in C++17 mode
CI: Intel ICC with C++17
docs: pybind11/numpy.h does not require numpy at build time. (#2720)
This is nice enough to be mentioned explicitly in the docs.
docs: Update warning about Python 3.9.0 UB, now that 3.9.1 has been released (#2719)
Adjusting `type_caster<std::reference_wrapper<T>>` to support const/non-const propagation in `cast_op`. (#2705)
* Allow type_caster of std::reference_wrapper<T> to be the same as a native reference.
Before, both std::reference_wrapper<T> and std::reference_wrapper<const T> would
invoke cast_op<type>. This doesn't allow the type_caster<> specialization for T
to distinguish reference_wrapper types from value types.
After, the type_caster<> specialization invokes cast_op<type&>, which allows
reference_wrapper to behave in the same way as a native reference type.
* Add tests/examples for std::reference_wrapper<const T>
* Add tests which use mutable/immutable variants
This test is a chimera; it blends the pybind11 casters with a custom
pytype implementation that supports immutable and mutable calls.
In order to detect the immutable/mutable state, the cast_op needs
to propagate it, even through e.g. std::reference<const T>
Note: This is still a work in progress; some things are crashing,
which likely means that I have a refcounting bug or something else
missing.
* Add/finish tests that distinguish const& from &
Fixes the bugs in my custom python type implementation,
demonstrate test that requires const& and reference_wrapper<const T>
being treated differently from Non-const.
* Add passing a const to non-const method.
* Demonstrate non-const conversion of reference_wrapper in tests.
Apply formatting presubmit check.
* Fix build errors from presubmit checks.
* Try and fix a few more CI errors
* More CI fixes.
* More CI fixups.
* Try and get PyPy to work.
* Additional minor fixups. Getting close to CI green.
* More ci fixes?
* fix clang-tidy warnings from presubmit
* fix more clang-tidy warnings
* minor comment and consistency cleanups
* PyDECREF -> Py_DECREF
* copy/move constructors
* Resolve codereview comments
* more review comment fixes
* review comments: remove spurious &
* Make the test fail even when the static_assert is commented out.
This expands the test_freezable_type_caster a bit by:
1/ adding accessors .is_immutable and .addr to compare identity
from python.
2/ Changing the default cast_op of the type_caster<> specialization
to return a non-const value. In normal codepaths this is a reasonable
default.
3/ adding roundtrip variants to exercise the by reference, by pointer
and by reference_wrapper in all call paths. In conjunction with 2/, this
demonstrates the failure case of the existing std::reference_wrpper conversion,
which now loses const in a similar way that happens when using the default cast_op_type<>.
* apply presubmit formatting
* Revert inclusion of test_freezable_type_caster
There's some concern that this test is a bit unwieldly because of the use
of the raw <Python.h> functions. Removing for now.
* Add a test that validates const references propagation.
This test verifies that cast_op may be used to correctly detect
const reference types when used with std::reference_wrapper.
* mend
* Review comments based changes.
1. std::add_lvalue_reference<type> -> type&
2. Simplify the test a little more; we're never returning the ConstRefCaster
type so the class_ definition can be removed.
* formatted files again.
* Move const_ref_caster test to builtin_casters
* Review comments: use cast_op and adjust some comments.
* Simplify ConstRefCasted test
I like this version better as it moves the assertion that matters
back into python.
ci: drop pypy2 linux, PGI 20.7, add Python 10 dev (#2724)
* ci: drop pypy2 linux, add Python 10 dev
* ci: fix mistake
* ci: commented-out PGI 20.11, drop 20.7
fix: regression with installed pybind11 overriding local one (#2716)
* fix: regression with installed pybind11 overriding discovered one
Closes #2709
* docs: wording incorrect
style: remove redundant instance->owned = true (#2723)
which was just before set to True in instance->allocate_layout()
fix: also throw in the move-constructor added by the PYBIND11_OBJECT macro, after the argument has been moved-out (if necessary) (#2701)
Make args_are_all_* ICC workarounds unconditional
Disable test_aligned on Intel ICC
Fix test_aligned on Intel ICC
Skip test_python_alreadyset_in_destructor on Intel ICC
Fix test_aligned again
ICC CI: Downgrade pytest
pytest 6 does not capture the `discard_as_unraisable` stderr and
just writes a warning with its content instead.
* refactor: simpler Intel workaround, suggested by @laramiel
* fix: try version with impl to see if it is easier to compile
* docs: update README for ICC
Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
2021-01-18 00:53:07 +00:00
|
|
|
|
// TODO: This should be disabled and fixed in future Intel compilers
|
|
|
|
|
#if !defined(__INTEL_COMPILER)
|
|
|
|
|
// Test "bool_passthrough_noconvert" again, but using () instead of {} to construct py::arg
|
|
|
|
|
// When compiled with the Intel compiler, this results in segmentation faults when importing
|
|
|
|
|
// the module. Tested with icc (ICC) 2021.1 Beta 20200827, this should be tested again when
|
|
|
|
|
// a newer version of icc is available.
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def(
|
|
|
|
|
"bool_passthrough_noconvert2", [](bool arg) { return arg; }, py::arg().noconvert());
|
fix: Intel ICC C++17 compatibility (#2729)
* CI: Intel icc/icpc via oneAPI
Add testing for Intel icc/icpc via the oneAPI images.
Intel oneAPI is in a late beta stage, currently shipping
oneAPI beta09 with ICC 20.2.
CI: Skip Interpreter Tests for Intel
Cannot find how to add this, neiter the package `libc6-dev` nor
`intel-oneapi-mkl-devel` help when installed to solve this:
```
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - not found
CMake Error at /__t/cmake/3.18.4/x64/cmake-3.18.4-Linux-x86_64/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:165 (message):
Could NOT find Threads (missing: Threads_FOUND)
Call Stack (most recent call first):
/__t/cmake/3.18.4/x64/cmake-3.18.4-Linux-x86_64/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:458 (_FPHSA_FAILURE_MESSAGE)
/__t/cmake/3.18.4/x64/cmake-3.18.4-Linux-x86_64/share/cmake-3.18/Modules/FindThreads.cmake:234 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
tests/test_embed/CMakeLists.txt:17 (find_package)
```
CI: libc6-dev from GCC for ICC
CI: Run bare metal for oneAPI
CI: Ubuntu 18.04 for oneAPI
CI: Intel +Catch -Eigen
CI: CMake from Apt (ICC tests)
CI: Replace Intel Py with GCC Py
CI: Intel w/o GCC's Eigen
CI: ICC with verbose make
[Debug] Find core dump
tests: use arg{} instead of arg() for Intel
tests: adding a few more missing {}
fix: sync with @tobiasleibner's branch
fix: try ubuntu 20-04
fix: drop exit 1
docs: Apply suggestions from code review
Co-authored-by: Tobias Leibner <tobias.leibner@googlemail.com>
Workaround for ICC enable_if issues
Another workaround for ICC's enable_if issues
fix error in previous commit
Disable one test for the Intel compiler in C++17 mode
Add back one instance of py::arg().noconvert()
Add NOLINT to fix clang-tidy check
Work around for ICC internal error in PYBIND11_EXPAND_SIDE_EFFECTS in C++17 mode
CI: Intel ICC with C++17
docs: pybind11/numpy.h does not require numpy at build time. (#2720)
This is nice enough to be mentioned explicitly in the docs.
docs: Update warning about Python 3.9.0 UB, now that 3.9.1 has been released (#2719)
Adjusting `type_caster<std::reference_wrapper<T>>` to support const/non-const propagation in `cast_op`. (#2705)
* Allow type_caster of std::reference_wrapper<T> to be the same as a native reference.
Before, both std::reference_wrapper<T> and std::reference_wrapper<const T> would
invoke cast_op<type>. This doesn't allow the type_caster<> specialization for T
to distinguish reference_wrapper types from value types.
After, the type_caster<> specialization invokes cast_op<type&>, which allows
reference_wrapper to behave in the same way as a native reference type.
* Add tests/examples for std::reference_wrapper<const T>
* Add tests which use mutable/immutable variants
This test is a chimera; it blends the pybind11 casters with a custom
pytype implementation that supports immutable and mutable calls.
In order to detect the immutable/mutable state, the cast_op needs
to propagate it, even through e.g. std::reference<const T>
Note: This is still a work in progress; some things are crashing,
which likely means that I have a refcounting bug or something else
missing.
* Add/finish tests that distinguish const& from &
Fixes the bugs in my custom python type implementation,
demonstrate test that requires const& and reference_wrapper<const T>
being treated differently from Non-const.
* Add passing a const to non-const method.
* Demonstrate non-const conversion of reference_wrapper in tests.
Apply formatting presubmit check.
* Fix build errors from presubmit checks.
* Try and fix a few more CI errors
* More CI fixes.
* More CI fixups.
* Try and get PyPy to work.
* Additional minor fixups. Getting close to CI green.
* More ci fixes?
* fix clang-tidy warnings from presubmit
* fix more clang-tidy warnings
* minor comment and consistency cleanups
* PyDECREF -> Py_DECREF
* copy/move constructors
* Resolve codereview comments
* more review comment fixes
* review comments: remove spurious &
* Make the test fail even when the static_assert is commented out.
This expands the test_freezable_type_caster a bit by:
1/ adding accessors .is_immutable and .addr to compare identity
from python.
2/ Changing the default cast_op of the type_caster<> specialization
to return a non-const value. In normal codepaths this is a reasonable
default.
3/ adding roundtrip variants to exercise the by reference, by pointer
and by reference_wrapper in all call paths. In conjunction with 2/, this
demonstrates the failure case of the existing std::reference_wrpper conversion,
which now loses const in a similar way that happens when using the default cast_op_type<>.
* apply presubmit formatting
* Revert inclusion of test_freezable_type_caster
There's some concern that this test is a bit unwieldly because of the use
of the raw <Python.h> functions. Removing for now.
* Add a test that validates const references propagation.
This test verifies that cast_op may be used to correctly detect
const reference types when used with std::reference_wrapper.
* mend
* Review comments based changes.
1. std::add_lvalue_reference<type> -> type&
2. Simplify the test a little more; we're never returning the ConstRefCaster
type so the class_ definition can be removed.
* formatted files again.
* Move const_ref_caster test to builtin_casters
* Review comments: use cast_op and adjust some comments.
* Simplify ConstRefCasted test
I like this version better as it moves the assertion that matters
back into python.
ci: drop pypy2 linux, PGI 20.7, add Python 10 dev (#2724)
* ci: drop pypy2 linux, add Python 10 dev
* ci: fix mistake
* ci: commented-out PGI 20.11, drop 20.7
fix: regression with installed pybind11 overriding local one (#2716)
* fix: regression with installed pybind11 overriding discovered one
Closes #2709
* docs: wording incorrect
style: remove redundant instance->owned = true (#2723)
which was just before set to True in instance->allocate_layout()
fix: also throw in the move-constructor added by the PYBIND11_OBJECT macro, after the argument has been moved-out (if necessary) (#2701)
Make args_are_all_* ICC workarounds unconditional
Disable test_aligned on Intel ICC
Fix test_aligned on Intel ICC
Skip test_python_alreadyset_in_destructor on Intel ICC
Fix test_aligned again
ICC CI: Downgrade pytest
pytest 6 does not capture the `discard_as_unraisable` stderr and
just writes a warning with its content instead.
* refactor: simpler Intel workaround, suggested by @laramiel
* fix: try version with impl to see if it is easier to compile
* docs: update README for ICC
Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
2021-01-18 00:53:07 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
|
// test_reference_wrapper
|
|
|
|
|
m.def("refwrap_builtin", [](std::reference_wrapper<int> p) { return 10 * p.get(); });
|
|
|
|
|
m.def("refwrap_usertype", [](std::reference_wrapper<UserType> p) { return p.get().value(); });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("refwrap_usertype_const",
|
|
|
|
|
[](std::reference_wrapper<const UserType> p) { return p.get().value(); });
|
Adjusting `type_caster<std::reference_wrapper<T>>` to support const/non-const propagation in `cast_op`. (#2705)
* Allow type_caster of std::reference_wrapper<T> to be the same as a native reference.
Before, both std::reference_wrapper<T> and std::reference_wrapper<const T> would
invoke cast_op<type>. This doesn't allow the type_caster<> specialization for T
to distinguish reference_wrapper types from value types.
After, the type_caster<> specialization invokes cast_op<type&>, which allows
reference_wrapper to behave in the same way as a native reference type.
* Add tests/examples for std::reference_wrapper<const T>
* Add tests which use mutable/immutable variants
This test is a chimera; it blends the pybind11 casters with a custom
pytype implementation that supports immutable and mutable calls.
In order to detect the immutable/mutable state, the cast_op needs
to propagate it, even through e.g. std::reference<const T>
Note: This is still a work in progress; some things are crashing,
which likely means that I have a refcounting bug or something else
missing.
* Add/finish tests that distinguish const& from &
Fixes the bugs in my custom python type implementation,
demonstrate test that requires const& and reference_wrapper<const T>
being treated differently from Non-const.
* Add passing a const to non-const method.
* Demonstrate non-const conversion of reference_wrapper in tests.
Apply formatting presubmit check.
* Fix build errors from presubmit checks.
* Try and fix a few more CI errors
* More CI fixes.
* More CI fixups.
* Try and get PyPy to work.
* Additional minor fixups. Getting close to CI green.
* More ci fixes?
* fix clang-tidy warnings from presubmit
* fix more clang-tidy warnings
* minor comment and consistency cleanups
* PyDECREF -> Py_DECREF
* copy/move constructors
* Resolve codereview comments
* more review comment fixes
* review comments: remove spurious &
* Make the test fail even when the static_assert is commented out.
This expands the test_freezable_type_caster a bit by:
1/ adding accessors .is_immutable and .addr to compare identity
from python.
2/ Changing the default cast_op of the type_caster<> specialization
to return a non-const value. In normal codepaths this is a reasonable
default.
3/ adding roundtrip variants to exercise the by reference, by pointer
and by reference_wrapper in all call paths. In conjunction with 2/, this
demonstrates the failure case of the existing std::reference_wrpper conversion,
which now loses const in a similar way that happens when using the default cast_op_type<>.
* apply presubmit formatting
* Revert inclusion of test_freezable_type_caster
There's some concern that this test is a bit unwieldly because of the use
of the raw <Python.h> functions. Removing for now.
* Add a test that validates const references propagation.
This test verifies that cast_op may be used to correctly detect
const reference types when used with std::reference_wrapper.
* mend
* Review comments based changes.
1. std::add_lvalue_reference<type> -> type&
2. Simplify the test a little more; we're never returning the ConstRefCaster
type so the class_ definition can be removed.
* formatted files again.
* Move const_ref_caster test to builtin_casters
* Review comments: use cast_op and adjust some comments.
* Simplify ConstRefCasted test
I like this version better as it moves the assertion that matters
back into python.
2020-12-16 00:53:55 +00:00
|
|
|
|
|
|
|
|
|
m.def("refwrap_lvalue", []() -> std::reference_wrapper<UserType> {
|
|
|
|
|
static UserType x(1);
|
|
|
|
|
return std::ref(x);
|
|
|
|
|
});
|
|
|
|
|
m.def("refwrap_lvalue_const", []() -> std::reference_wrapper<const UserType> {
|
|
|
|
|
static UserType x(1);
|
|
|
|
|
return std::cref(x);
|
|
|
|
|
});
|
|
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
|
// Not currently supported (std::pair caster has return-by-value cast operator);
|
|
|
|
|
// triggers static_assert failure.
|
2022-02-10 20:17:07 +00:00
|
|
|
|
// m.def("refwrap_pair", [](std::reference_wrapper<std::pair<int, int>>) { });
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def(
|
|
|
|
|
"refwrap_list",
|
|
|
|
|
[](bool copy) {
|
|
|
|
|
static IncType x1(1), x2(2);
|
|
|
|
|
py::list l;
|
|
|
|
|
for (const auto &f : {std::ref(x1), std::ref(x2)}) {
|
|
|
|
|
l.append(py::cast(
|
|
|
|
|
f, copy ? py::return_value_policy::copy : py::return_value_policy::reference));
|
|
|
|
|
}
|
|
|
|
|
return l;
|
|
|
|
|
},
|
|
|
|
|
"copy"_a);
|
2017-06-08 22:44:49 +00:00
|
|
|
|
|
|
|
|
|
m.def("refwrap_iiw", [](const IncType &w) { return w.value(); });
|
2021-07-12 20:10:28 +00:00
|
|
|
|
m.def("refwrap_call_iiw", [](IncType &w, const py::function &f) {
|
2017-06-08 22:44:49 +00:00
|
|
|
|
py::list l;
|
|
|
|
|
l.append(f(std::ref(w)));
|
|
|
|
|
l.append(f(std::cref(w)));
|
|
|
|
|
IncType x(w.value());
|
|
|
|
|
l.append(f(std::ref(x)));
|
|
|
|
|
IncType y(w.value());
|
|
|
|
|
auto r3 = std::ref(y);
|
|
|
|
|
l.append(f(r3));
|
|
|
|
|
return l;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// test_complex
|
|
|
|
|
m.def("complex_cast", [](float x) { return "{}"_s.format(x); });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("complex_cast",
|
|
|
|
|
[](std::complex<float> x) { return "({}, {})"_s.format(x.real(), x.imag()); });
|
2017-11-30 17:33:24 +00:00
|
|
|
|
|
|
|
|
|
// test int vs. long (Python 2)
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("int_cast", []() { return (int) 42; });
|
|
|
|
|
m.def("long_cast", []() { return (long) 42; });
|
|
|
|
|
m.def("longlong_cast", []() { return ULLONG_MAX; });
|
2018-11-11 18:32:09 +00:00
|
|
|
|
|
|
|
|
|
/// test void* cast operator
|
|
|
|
|
m.def("test_void_caster", []() -> bool {
|
|
|
|
|
void *v = (void *) 0xabcd;
|
|
|
|
|
py::object o = py::cast(v);
|
|
|
|
|
return py::cast<void *>(o) == v;
|
|
|
|
|
});
|
Adjusting `type_caster<std::reference_wrapper<T>>` to support const/non-const propagation in `cast_op`. (#2705)
* Allow type_caster of std::reference_wrapper<T> to be the same as a native reference.
Before, both std::reference_wrapper<T> and std::reference_wrapper<const T> would
invoke cast_op<type>. This doesn't allow the type_caster<> specialization for T
to distinguish reference_wrapper types from value types.
After, the type_caster<> specialization invokes cast_op<type&>, which allows
reference_wrapper to behave in the same way as a native reference type.
* Add tests/examples for std::reference_wrapper<const T>
* Add tests which use mutable/immutable variants
This test is a chimera; it blends the pybind11 casters with a custom
pytype implementation that supports immutable and mutable calls.
In order to detect the immutable/mutable state, the cast_op needs
to propagate it, even through e.g. std::reference<const T>
Note: This is still a work in progress; some things are crashing,
which likely means that I have a refcounting bug or something else
missing.
* Add/finish tests that distinguish const& from &
Fixes the bugs in my custom python type implementation,
demonstrate test that requires const& and reference_wrapper<const T>
being treated differently from Non-const.
* Add passing a const to non-const method.
* Demonstrate non-const conversion of reference_wrapper in tests.
Apply formatting presubmit check.
* Fix build errors from presubmit checks.
* Try and fix a few more CI errors
* More CI fixes.
* More CI fixups.
* Try and get PyPy to work.
* Additional minor fixups. Getting close to CI green.
* More ci fixes?
* fix clang-tidy warnings from presubmit
* fix more clang-tidy warnings
* minor comment and consistency cleanups
* PyDECREF -> Py_DECREF
* copy/move constructors
* Resolve codereview comments
* more review comment fixes
* review comments: remove spurious &
* Make the test fail even when the static_assert is commented out.
This expands the test_freezable_type_caster a bit by:
1/ adding accessors .is_immutable and .addr to compare identity
from python.
2/ Changing the default cast_op of the type_caster<> specialization
to return a non-const value. In normal codepaths this is a reasonable
default.
3/ adding roundtrip variants to exercise the by reference, by pointer
and by reference_wrapper in all call paths. In conjunction with 2/, this
demonstrates the failure case of the existing std::reference_wrpper conversion,
which now loses const in a similar way that happens when using the default cast_op_type<>.
* apply presubmit formatting
* Revert inclusion of test_freezable_type_caster
There's some concern that this test is a bit unwieldly because of the use
of the raw <Python.h> functions. Removing for now.
* Add a test that validates const references propagation.
This test verifies that cast_op may be used to correctly detect
const reference types when used with std::reference_wrapper.
* mend
* Review comments based changes.
1. std::add_lvalue_reference<type> -> type&
2. Simplify the test a little more; we're never returning the ConstRefCaster
type so the class_ definition can be removed.
* formatted files again.
* Move const_ref_caster test to builtin_casters
* Review comments: use cast_op and adjust some comments.
* Simplify ConstRefCasted test
I like this version better as it moves the assertion that matters
back into python.
2020-12-16 00:53:55 +00:00
|
|
|
|
|
|
|
|
|
// Tests const/non-const propagation in cast_op.
|
|
|
|
|
m.def("takes", [](ConstRefCasted x) { return x.tag; });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("takes_move", [](ConstRefCasted &&x) { return x.tag; });
|
|
|
|
|
m.def("takes_ptr", [](ConstRefCasted *x) { return x->tag; });
|
|
|
|
|
m.def("takes_ref", [](ConstRefCasted &x) { return x.tag; });
|
Adjusting `type_caster<std::reference_wrapper<T>>` to support const/non-const propagation in `cast_op`. (#2705)
* Allow type_caster of std::reference_wrapper<T> to be the same as a native reference.
Before, both std::reference_wrapper<T> and std::reference_wrapper<const T> would
invoke cast_op<type>. This doesn't allow the type_caster<> specialization for T
to distinguish reference_wrapper types from value types.
After, the type_caster<> specialization invokes cast_op<type&>, which allows
reference_wrapper to behave in the same way as a native reference type.
* Add tests/examples for std::reference_wrapper<const T>
* Add tests which use mutable/immutable variants
This test is a chimera; it blends the pybind11 casters with a custom
pytype implementation that supports immutable and mutable calls.
In order to detect the immutable/mutable state, the cast_op needs
to propagate it, even through e.g. std::reference<const T>
Note: This is still a work in progress; some things are crashing,
which likely means that I have a refcounting bug or something else
missing.
* Add/finish tests that distinguish const& from &
Fixes the bugs in my custom python type implementation,
demonstrate test that requires const& and reference_wrapper<const T>
being treated differently from Non-const.
* Add passing a const to non-const method.
* Demonstrate non-const conversion of reference_wrapper in tests.
Apply formatting presubmit check.
* Fix build errors from presubmit checks.
* Try and fix a few more CI errors
* More CI fixes.
* More CI fixups.
* Try and get PyPy to work.
* Additional minor fixups. Getting close to CI green.
* More ci fixes?
* fix clang-tidy warnings from presubmit
* fix more clang-tidy warnings
* minor comment and consistency cleanups
* PyDECREF -> Py_DECREF
* copy/move constructors
* Resolve codereview comments
* more review comment fixes
* review comments: remove spurious &
* Make the test fail even when the static_assert is commented out.
This expands the test_freezable_type_caster a bit by:
1/ adding accessors .is_immutable and .addr to compare identity
from python.
2/ Changing the default cast_op of the type_caster<> specialization
to return a non-const value. In normal codepaths this is a reasonable
default.
3/ adding roundtrip variants to exercise the by reference, by pointer
and by reference_wrapper in all call paths. In conjunction with 2/, this
demonstrates the failure case of the existing std::reference_wrpper conversion,
which now loses const in a similar way that happens when using the default cast_op_type<>.
* apply presubmit formatting
* Revert inclusion of test_freezable_type_caster
There's some concern that this test is a bit unwieldly because of the use
of the raw <Python.h> functions. Removing for now.
* Add a test that validates const references propagation.
This test verifies that cast_op may be used to correctly detect
const reference types when used with std::reference_wrapper.
* mend
* Review comments based changes.
1. std::add_lvalue_reference<type> -> type&
2. Simplify the test a little more; we're never returning the ConstRefCaster
type so the class_ definition can be removed.
* formatted files again.
* Move const_ref_caster test to builtin_casters
* Review comments: use cast_op and adjust some comments.
* Simplify ConstRefCasted test
I like this version better as it moves the assertion that matters
back into python.
2020-12-16 00:53:55 +00:00
|
|
|
|
m.def("takes_ref_wrap", [](std::reference_wrapper<ConstRefCasted> x) { return x.get().tag; });
|
2022-02-10 20:17:07 +00:00
|
|
|
|
m.def("takes_const_ptr", [](const ConstRefCasted *x) { return x->tag; });
|
|
|
|
|
m.def("takes_const_ref", [](const ConstRefCasted &x) { return x.tag; });
|
|
|
|
|
m.def("takes_const_ref_wrap",
|
|
|
|
|
[](std::reference_wrapper<const ConstRefCasted> x) { return x.get().tag; });
|
2017-06-08 22:44:49 +00:00
|
|
|
|
}
|