Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:52

0001 /*
0002     tests/test_builtin_casters.cpp -- Casters available without any additional headers
0003 
0004     Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
0005 
0006     All rights reserved. Use of this source code is governed by a
0007     BSD-style license that can be found in the LICENSE file.
0008 */
0009 
0010 #include <pybind11/complex.h>
0011 
0012 #include "pybind11_tests.h"
0013 
0014 struct ConstRefCasted {
0015     int tag;
0016 };
0017 
0018 PYBIND11_NAMESPACE_BEGIN(pybind11)
0019 PYBIND11_NAMESPACE_BEGIN(detail)
0020 template <>
0021 class type_caster<ConstRefCasted> {
0022 public:
0023     static constexpr auto name = const_name<ConstRefCasted>();
0024 
0025     // Input is unimportant, a new value will always be constructed based on the
0026     // cast operator.
0027     bool load(handle, bool) { return true; }
0028 
0029     explicit operator ConstRefCasted &&() {
0030         value = {1};
0031         // NOLINTNEXTLINE(performance-move-const-arg)
0032         return std::move(value);
0033     }
0034     explicit operator ConstRefCasted &() {
0035         value = {2};
0036         return value;
0037     }
0038     explicit operator ConstRefCasted *() {
0039         value = {3};
0040         return &value;
0041     }
0042 
0043     explicit operator const ConstRefCasted &() {
0044         value = {4};
0045         return value;
0046     }
0047     explicit operator const ConstRefCasted *() {
0048         value = {5};
0049         return &value;
0050     }
0051 
0052     // custom cast_op to explicitly propagate types to the conversion operators.
0053     template <typename T_>
0054     using cast_op_type =
0055         /// const
0056         conditional_t<
0057             std::is_same<remove_reference_t<T_>, const ConstRefCasted *>::value,
0058             const ConstRefCasted *,
0059             conditional_t<
0060                 std::is_same<T_, const ConstRefCasted &>::value,
0061                 const ConstRefCasted &,
0062                 /// non-const
0063                 conditional_t<std::is_same<remove_reference_t<T_>, ConstRefCasted *>::value,
0064                               ConstRefCasted *,
0065                               conditional_t<std::is_same<T_, ConstRefCasted &>::value,
0066                                             ConstRefCasted &,
0067                                             /* else */ ConstRefCasted &&>>>>;
0068 
0069 private:
0070     ConstRefCasted value = {0};
0071 };
0072 PYBIND11_NAMESPACE_END(detail)
0073 PYBIND11_NAMESPACE_END(pybind11)
0074 
0075 TEST_SUBMODULE(builtin_casters, m) {
0076     PYBIND11_WARNING_PUSH
0077     PYBIND11_WARNING_DISABLE_MSVC(4127)
0078 
0079     // test_simple_string
0080     m.def("string_roundtrip", [](const char *s) { return s; });
0081 
0082     // test_unicode_conversion
0083     // Some test characters in utf16 and utf32 encodings.  The last one (the 𝐀) contains a null
0084     // byte
0085     char32_t a32 = 0x61 /*a*/, z32 = 0x7a /*z*/, ib32 = 0x203d /*β€½*/, cake32 = 0x1f382 /*πŸŽ‚*/,
0086              mathbfA32 = 0x1d400 /*𝐀*/;
0087     char16_t b16 = 0x62 /*b*/, z16 = 0x7a, ib16 = 0x203d, cake16_1 = 0xd83c, cake16_2 = 0xdf82,
0088              mathbfA16_1 = 0xd835, mathbfA16_2 = 0xdc00;
0089     std::wstring wstr;
0090     wstr.push_back(0x61);   // a
0091     wstr.push_back(0x2e18); // ⸘
0092     if (sizeof(wchar_t) == 2) {
0093         wstr.push_back(mathbfA16_1);
0094         wstr.push_back(mathbfA16_2);
0095     } // 𝐀, utf16
0096     else {
0097         wstr.push_back((wchar_t) mathbfA32);
0098     }                     // 𝐀, utf32
0099     wstr.push_back(0x7a); // z
0100 
0101     m.def("good_utf8_string", []() {
0102         return std::string((const char *) u8"Say utf8\u203d \U0001f382 \U0001d400");
0103     }); // Say utf8β€½ πŸŽ‚ 𝐀
0104     m.def("good_utf16_string", [=]() {
0105         return std::u16string({b16, ib16, cake16_1, cake16_2, mathbfA16_1, mathbfA16_2, z16});
0106     }); // bβ€½πŸŽ‚π€z
0107     m.def("good_utf32_string", [=]() {
0108         return std::u32string({a32, mathbfA32, cake32, ib32, z32});
0109     });                                                 // aπ€πŸŽ‚β€½z
0110     m.def("good_wchar_string", [=]() { return wstr; }); // a‽𝐀z
0111     m.def("bad_utf8_string", []() {
0112         return std::string("abc\xd0"
0113                            "def");
0114     });
0115     m.def("bad_utf16_string", [=]() { return std::u16string({b16, char16_t(0xd800), z16}); });
0116     // Under Python 2.7, invalid unicode UTF-32 characters didn't appear to trigger
0117     // UnicodeDecodeError
0118     m.def("bad_utf32_string", [=]() { return std::u32string({a32, char32_t(0xd800), z32}); });
0119     if (sizeof(wchar_t) == 2) {
0120         m.def("bad_wchar_string", [=]() {
0121             return std::wstring({wchar_t(0x61), wchar_t(0xd800)});
0122         });
0123     }
0124     m.def("u8_Z", []() -> char { return 'Z'; });
0125     m.def("u8_eacute", []() -> char { return '\xe9'; });
0126     m.def("u16_ibang", [=]() -> char16_t { return ib16; });
0127     m.def("u32_mathbfA", [=]() -> char32_t { return mathbfA32; });
0128     m.def("wchar_heart", []() -> wchar_t { return 0x2665; });
0129 
0130     // test_single_char_arguments
0131     m.attr("wchar_size") = py::cast(sizeof(wchar_t));
0132     m.def("ord_char", [](char c) -> int { return static_cast<unsigned char>(c); });
0133     m.def("ord_char_lv", [](char &c) -> int { return static_cast<unsigned char>(c); });
0134     m.def("ord_char16", [](char16_t c) -> uint16_t { return c; });
0135     m.def("ord_char16_lv", [](char16_t &c) -> uint16_t { return c; });
0136     m.def("ord_char32", [](char32_t c) -> uint32_t { return c; });
0137     m.def("ord_wchar", [](wchar_t c) -> int { return c; });
0138 
0139     // test_bytes_to_string
0140     m.def("strlen", [](char *s) { return strlen(s); });
0141     m.def("string_length", [](const std::string &s) { return s.length(); });
0142 
0143 #ifdef PYBIND11_HAS_U8STRING
0144     m.attr("has_u8string") = true;
0145     m.def("good_utf8_u8string", []() {
0146         return std::u8string(u8"Say utf8\u203d \U0001f382 \U0001d400");
0147     }); // Say utf8β€½ πŸŽ‚ 𝐀
0148     m.def("bad_utf8_u8string", []() {
0149         return std::u8string((const char8_t *) "abc\xd0"
0150                                                "def");
0151     });
0152 
0153     m.def("u8_char8_Z", []() -> char8_t { return u8'Z'; });
0154 
0155     // test_single_char_arguments
0156     m.def("ord_char8", [](char8_t c) -> int { return static_cast<unsigned char>(c); });
0157     m.def("ord_char8_lv", [](char8_t &c) -> int { return static_cast<unsigned char>(c); });
0158 #endif
0159 
0160     // test_string_view
0161 #ifdef PYBIND11_HAS_STRING_VIEW
0162     m.attr("has_string_view") = true;
0163     m.def("string_view_print", [](std::string_view s) { py::print(s, s.size()); });
0164     m.def("string_view16_print", [](std::u16string_view s) { py::print(s, s.size()); });
0165     m.def("string_view32_print", [](std::u32string_view s) { py::print(s, s.size()); });
0166     m.def("string_view_chars", [](std::string_view s) {
0167         py::list l;
0168         for (auto c : s) {
0169             l.append((std::uint8_t) c);
0170         }
0171         return l;
0172     });
0173     m.def("string_view16_chars", [](std::u16string_view s) {
0174         py::list l;
0175         for (auto c : s) {
0176             l.append((int) c);
0177         }
0178         return l;
0179     });
0180     m.def("string_view32_chars", [](std::u32string_view s) {
0181         py::list l;
0182         for (auto c : s) {
0183             l.append((int) c);
0184         }
0185         return l;
0186     });
0187     m.def("string_view_return",
0188           []() { return std::string_view((const char *) u8"utf8 secret \U0001f382"); });
0189     m.def("string_view16_return",
0190           []() { return std::u16string_view(u"utf16 secret \U0001f382"); });
0191     m.def("string_view32_return",
0192           []() { return std::u32string_view(U"utf32 secret \U0001f382"); });
0193 
0194     // The inner lambdas here are to also test implicit conversion
0195     using namespace std::literals;
0196     m.def("string_view_bytes",
0197           []() { return [](py::bytes b) { return b; }("abc \x80\x80 def"sv); });
0198     m.def("string_view_str",
0199           []() { return [](py::str s) { return s; }("abc \342\200\275 def"sv); });
0200     m.def("string_view_from_bytes",
0201           [](const py::bytes &b) { return [](std::string_view s) { return s; }(b); });
0202     m.def("string_view_memoryview", []() {
0203         static constexpr auto val = "Have some \360\237\216\202"sv;
0204         return py::memoryview::from_memory(val);
0205     });
0206 
0207 #    ifdef PYBIND11_HAS_U8STRING
0208     m.def("string_view8_print", [](std::u8string_view s) { py::print(s, s.size()); });
0209     m.def("string_view8_chars", [](std::u8string_view s) {
0210         py::list l;
0211         for (auto c : s)
0212             l.append((std::uint8_t) c);
0213         return l;
0214     });
0215     m.def("string_view8_return", []() { return std::u8string_view(u8"utf8 secret \U0001f382"); });
0216     m.def("string_view8_str", []() { return py::str{std::u8string_view{u8"abc β€½ def"}}; });
0217 #    endif
0218 
0219     struct TypeWithBothOperatorStringAndStringView {
0220         // NOLINTNEXTLINE(google-explicit-constructor)
0221         operator std::string() const { return "success"; }
0222         // NOLINTNEXTLINE(google-explicit-constructor)
0223         operator std::string_view() const { return "failure"; }
0224     };
0225     m.def("bytes_from_type_with_both_operator_string_and_string_view",
0226           []() { return py::bytes(TypeWithBothOperatorStringAndStringView()); });
0227     m.def("str_from_type_with_both_operator_string_and_string_view",
0228           []() { return py::str(TypeWithBothOperatorStringAndStringView()); });
0229 #endif
0230 
0231     // test_integer_casting
0232     m.def("i32_str", [](std::int32_t v) { return std::to_string(v); });
0233     m.def("u32_str", [](std::uint32_t v) { return std::to_string(v); });
0234     m.def("i64_str", [](std::int64_t v) { return std::to_string(v); });
0235     m.def("u64_str", [](std::uint64_t v) { return std::to_string(v); });
0236 
0237     // test_int_convert
0238     m.def("int_passthrough", [](int arg) { return arg; });
0239     m.def(
0240         "int_passthrough_noconvert", [](int arg) { return arg; }, py::arg{}.noconvert());
0241 
0242     // test_tuple
0243     m.def(
0244         "pair_passthrough",
0245         [](const std::pair<bool, std::string> &input) {
0246             return std::make_pair(input.second, input.first);
0247         },
0248         "Return a pair in reversed order");
0249     m.def(
0250         "tuple_passthrough",
0251         [](std::tuple<bool, std::string, int> input) {
0252             return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
0253         },
0254         "Return a triple in reversed order");
0255     m.def("empty_tuple", []() { return std::tuple<>(); });
0256     static std::pair<RValueCaster, RValueCaster> lvpair;
0257     static std::tuple<RValueCaster, RValueCaster, RValueCaster> lvtuple;
0258     static std::pair<RValueCaster, std::tuple<RValueCaster, std::pair<RValueCaster, RValueCaster>>>
0259         lvnested;
0260     m.def("rvalue_pair", []() { return std::make_pair(RValueCaster{}, RValueCaster{}); });
0261     m.def("lvalue_pair", []() -> const decltype(lvpair) & { return lvpair; });
0262     m.def("rvalue_tuple",
0263           []() { return std::make_tuple(RValueCaster{}, RValueCaster{}, RValueCaster{}); });
0264     m.def("lvalue_tuple", []() -> const decltype(lvtuple) & { return lvtuple; });
0265     m.def("rvalue_nested", []() {
0266         return std::make_pair(
0267             RValueCaster{},
0268             std::make_tuple(RValueCaster{}, std::make_pair(RValueCaster{}, RValueCaster{})));
0269     });
0270     m.def("lvalue_nested", []() -> const decltype(lvnested) & { return lvnested; });
0271 
0272     m.def(
0273         "int_string_pair",
0274         []() {
0275             // Using no-destructor idiom to side-step warnings from overzealous compilers.
0276             static auto *int_string_pair = new std::pair<int, std::string>{2, "items"};
0277             return int_string_pair;
0278         },
0279         py::return_value_policy::reference);
0280 
0281     // test_builtins_cast_return_none
0282     m.def("return_none_string", []() -> std::string * { return nullptr; });
0283     m.def("return_none_char", []() -> const char * { return nullptr; });
0284     m.def("return_none_bool", []() -> bool * { return nullptr; });
0285     m.def("return_none_int", []() -> int * { return nullptr; });
0286     m.def("return_none_float", []() -> float * { return nullptr; });
0287     m.def("return_none_pair", []() -> std::pair<int, int> * { return nullptr; });
0288 
0289     // test_none_deferred
0290     m.def("defer_none_cstring", [](char *) { return false; });
0291     m.def("defer_none_cstring", [](const py::none &) { return true; });
0292     m.def("defer_none_custom", [](UserType *) { return false; });
0293     m.def("defer_none_custom", [](const py::none &) { return true; });
0294     m.def("nodefer_none_void", [](void *) { return true; });
0295     m.def("nodefer_none_void", [](const py::none &) { return false; });
0296 
0297     // test_void_caster
0298     m.def("load_nullptr_t", [](std::nullptr_t) {}); // not useful, but it should still compile
0299     m.def("cast_nullptr_t", []() { return std::nullptr_t{}; });
0300 
0301     // [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works.
0302 
0303     // test_bool_caster
0304     m.def("bool_passthrough", [](bool arg) { return arg; });
0305     m.def(
0306         "bool_passthrough_noconvert", [](bool arg) { return arg; }, py::arg{}.noconvert());
0307 
0308     // TODO: This should be disabled and fixed in future Intel compilers
0309 #if !defined(__INTEL_COMPILER)
0310     // Test "bool_passthrough_noconvert" again, but using () instead of {} to construct py::arg
0311     // When compiled with the Intel compiler, this results in segmentation faults when importing
0312     // the module. Tested with icc (ICC) 2021.1 Beta 20200827, this should be tested again when
0313     // a newer version of icc is available.
0314     m.def(
0315         "bool_passthrough_noconvert2", [](bool arg) { return arg; }, py::arg().noconvert());
0316 #endif
0317 
0318     // test_reference_wrapper
0319     m.def("refwrap_builtin", [](std::reference_wrapper<int> p) { return 10 * p.get(); });
0320     m.def("refwrap_usertype", [](std::reference_wrapper<UserType> p) { return p.get().value(); });
0321     m.def("refwrap_usertype_const",
0322           [](std::reference_wrapper<const UserType> p) { return p.get().value(); });
0323 
0324     m.def("refwrap_lvalue", []() -> std::reference_wrapper<UserType> {
0325         static UserType x(1);
0326         return std::ref(x);
0327     });
0328     m.def("refwrap_lvalue_const", []() -> std::reference_wrapper<const UserType> {
0329         static UserType x(1);
0330         return std::cref(x);
0331     });
0332 
0333     // Not currently supported (std::pair caster has return-by-value cast operator);
0334     // triggers static_assert failure.
0335     // m.def("refwrap_pair", [](std::reference_wrapper<std::pair<int, int>>) { });
0336 
0337     m.def(
0338         "refwrap_list",
0339         [](bool copy) {
0340             static IncType x1(1), x2(2);
0341             py::list l;
0342             for (const auto &f : {std::ref(x1), std::ref(x2)}) {
0343                 l.append(py::cast(
0344                     f, copy ? py::return_value_policy::copy : py::return_value_policy::reference));
0345             }
0346             return l;
0347         },
0348         "copy"_a);
0349 
0350     m.def("refwrap_iiw", [](const IncType &w) { return w.value(); });
0351     m.def("refwrap_call_iiw", [](IncType &w, const py::function &f) {
0352         py::list l;
0353         l.append(f(std::ref(w)));
0354         l.append(f(std::cref(w)));
0355         IncType x(w.value());
0356         l.append(f(std::ref(x)));
0357         IncType y(w.value());
0358         auto r3 = std::ref(y);
0359         l.append(f(r3));
0360         return l;
0361     });
0362 
0363     // test_complex
0364     m.def("complex_cast", [](float x) { return "{}"_s.format(x); });
0365     m.def("complex_cast",
0366           [](std::complex<float> x) { return "({}, {})"_s.format(x.real(), x.imag()); });
0367 
0368     // test int vs. long (Python 2)
0369     m.def("int_cast", []() { return (int) 42; });
0370     m.def("long_cast", []() { return (long) 42; });
0371     m.def("longlong_cast", []() { return ULLONG_MAX; });
0372 
0373     /// test void* cast operator
0374     m.def("test_void_caster", []() -> bool {
0375         void *v = (void *) 0xabcd;
0376         py::object o = py::cast(v);
0377         return py::cast<void *>(o) == v;
0378     });
0379 
0380     // Tests const/non-const propagation in cast_op.
0381     m.def("takes", [](ConstRefCasted x) { return x.tag; });
0382     m.def("takes_move", [](ConstRefCasted &&x) { return x.tag; });
0383     m.def("takes_ptr", [](ConstRefCasted *x) { return x->tag; });
0384     m.def("takes_ref", [](ConstRefCasted &x) { return x.tag; });
0385     m.def("takes_ref_wrap", [](std::reference_wrapper<ConstRefCasted> x) { return x.get().tag; });
0386     m.def("takes_const_ptr", [](const ConstRefCasted *x) { return x->tag; });
0387     m.def("takes_const_ref", [](const ConstRefCasted &x) { return x.tag; });
0388     m.def("takes_const_ref_wrap",
0389           [](std::reference_wrapper<const ConstRefCasted> x) { return x.get().tag; });
0390 
0391     PYBIND11_WARNING_POP
0392 }