test_builtin_casters.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. tests/test_builtin_casters.cpp -- Casters available without any additional headers
  3. Copyright (c) 2017 Wenzel Jakob <[email protected]>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. #include <pybind11/complex.h>
  9. #if defined(_MSC_VER)
  10. # pragma warning(push)
  11. # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
  12. #endif
  13. TEST_SUBMODULE(builtin_casters, m) {
  14. // test_simple_string
  15. m.def("string_roundtrip", [](const char *s) { return s; });
  16. // test_unicode_conversion
  17. // Some test characters in utf16 and utf32 encodings. The last one (the 𝐀) contains a null byte
  18. char32_t a32 = 0x61 /*a*/, z32 = 0x7a /*z*/, ib32 = 0x203d /*‽*/, cake32 = 0x1f382 /*🎂*/, mathbfA32 = 0x1d400 /*𝐀*/;
  19. char16_t b16 = 0x62 /*b*/, z16 = 0x7a, ib16 = 0x203d, cake16_1 = 0xd83c, cake16_2 = 0xdf82, mathbfA16_1 = 0xd835, mathbfA16_2 = 0xdc00;
  20. std::wstring wstr;
  21. wstr.push_back(0x61); // a
  22. wstr.push_back(0x2e18); // ⸘
  23. if (sizeof(wchar_t) == 2) { wstr.push_back(mathbfA16_1); wstr.push_back(mathbfA16_2); } // 𝐀, utf16
  24. else { wstr.push_back((wchar_t) mathbfA32); } // 𝐀, utf32
  25. wstr.push_back(0x7a); // z
  26. m.def("good_utf8_string", []() { return std::string(u8"Say utf8\u203d \U0001f382 \U0001d400"); }); // Say utf8‽ 🎂 𝐀
  27. m.def("good_utf16_string", [=]() { return std::u16string({ b16, ib16, cake16_1, cake16_2, mathbfA16_1, mathbfA16_2, z16 }); }); // b‽🎂𝐀z
  28. m.def("good_utf32_string", [=]() { return std::u32string({ a32, mathbfA32, cake32, ib32, z32 }); }); // a𝐀🎂‽z
  29. m.def("good_wchar_string", [=]() { return wstr; }); // a‽𝐀z
  30. m.def("bad_utf8_string", []() { return std::string("abc\xd0" "def"); });
  31. m.def("bad_utf16_string", [=]() { return std::u16string({ b16, char16_t(0xd800), z16 }); });
  32. // Under Python 2.7, invalid unicode UTF-32 characters don't appear to trigger UnicodeDecodeError
  33. if (PY_MAJOR_VERSION >= 3)
  34. m.def("bad_utf32_string", [=]() { return std::u32string({ a32, char32_t(0xd800), z32 }); });
  35. if (PY_MAJOR_VERSION >= 3 || sizeof(wchar_t) == 2)
  36. m.def("bad_wchar_string", [=]() { return std::wstring({ wchar_t(0x61), wchar_t(0xd800) }); });
  37. m.def("u8_Z", []() -> char { return 'Z'; });
  38. m.def("u8_eacute", []() -> char { return '\xe9'; });
  39. m.def("u16_ibang", [=]() -> char16_t { return ib16; });
  40. m.def("u32_mathbfA", [=]() -> char32_t { return mathbfA32; });
  41. m.def("wchar_heart", []() -> wchar_t { return 0x2665; });
  42. // test_single_char_arguments
  43. m.attr("wchar_size") = py::cast(sizeof(wchar_t));
  44. m.def("ord_char", [](char c) -> int { return static_cast<unsigned char>(c); });
  45. m.def("ord_char_lv", [](char &c) -> int { return static_cast<unsigned char>(c); });
  46. m.def("ord_char16", [](char16_t c) -> uint16_t { return c; });
  47. m.def("ord_char16_lv", [](char16_t &c) -> uint16_t { return c; });
  48. m.def("ord_char32", [](char32_t c) -> uint32_t { return c; });
  49. m.def("ord_wchar", [](wchar_t c) -> int { return c; });
  50. // test_bytes_to_string
  51. m.def("strlen", [](char *s) { return strlen(s); });
  52. m.def("string_length", [](std::string s) { return s.length(); });
  53. // test_string_view
  54. #ifdef PYBIND11_HAS_STRING_VIEW
  55. m.attr("has_string_view") = true;
  56. m.def("string_view_print", [](std::string_view s) { py::print(s, s.size()); });
  57. m.def("string_view16_print", [](std::u16string_view s) { py::print(s, s.size()); });
  58. m.def("string_view32_print", [](std::u32string_view s) { py::print(s, s.size()); });
  59. m.def("string_view_chars", [](std::string_view s) { py::list l; for (auto c : s) l.append((std::uint8_t) c); return l; });
  60. m.def("string_view16_chars", [](std::u16string_view s) { py::list l; for (auto c : s) l.append((int) c); return l; });
  61. m.def("string_view32_chars", [](std::u32string_view s) { py::list l; for (auto c : s) l.append((int) c); return l; });
  62. m.def("string_view_return", []() { return std::string_view(u8"utf8 secret \U0001f382"); });
  63. m.def("string_view16_return", []() { return std::u16string_view(u"utf16 secret \U0001f382"); });
  64. m.def("string_view32_return", []() { return std::u32string_view(U"utf32 secret \U0001f382"); });
  65. #endif
  66. // test_integer_casting
  67. m.def("i32_str", [](std::int32_t v) { return std::to_string(v); });
  68. m.def("u32_str", [](std::uint32_t v) { return std::to_string(v); });
  69. m.def("i64_str", [](std::int64_t v) { return std::to_string(v); });
  70. m.def("u64_str", [](std::uint64_t v) { return std::to_string(v); });
  71. // test_tuple
  72. m.def("pair_passthrough", [](std::pair<bool, std::string> input) {
  73. return std::make_pair(input.second, input.first);
  74. }, "Return a pair in reversed order");
  75. m.def("tuple_passthrough", [](std::tuple<bool, std::string, int> input) {
  76. return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
  77. }, "Return a triple in reversed order");
  78. m.def("empty_tuple", []() { return std::tuple<>(); });
  79. static std::pair<RValueCaster, RValueCaster> lvpair;
  80. static std::tuple<RValueCaster, RValueCaster, RValueCaster> lvtuple;
  81. static std::pair<RValueCaster, std::tuple<RValueCaster, std::pair<RValueCaster, RValueCaster>>> lvnested;
  82. m.def("rvalue_pair", []() { return std::make_pair(RValueCaster{}, RValueCaster{}); });
  83. m.def("lvalue_pair", []() -> const decltype(lvpair) & { return lvpair; });
  84. m.def("rvalue_tuple", []() { return std::make_tuple(RValueCaster{}, RValueCaster{}, RValueCaster{}); });
  85. m.def("lvalue_tuple", []() -> const decltype(lvtuple) & { return lvtuple; });
  86. m.def("rvalue_nested", []() {
  87. return std::make_pair(RValueCaster{}, std::make_tuple(RValueCaster{}, std::make_pair(RValueCaster{}, RValueCaster{}))); });
  88. m.def("lvalue_nested", []() -> const decltype(lvnested) & { return lvnested; });
  89. // test_builtins_cast_return_none
  90. m.def("return_none_string", []() -> std::string * { return nullptr; });
  91. m.def("return_none_char", []() -> const char * { return nullptr; });
  92. m.def("return_none_bool", []() -> bool * { return nullptr; });
  93. m.def("return_none_int", []() -> int * { return nullptr; });
  94. m.def("return_none_float", []() -> float * { return nullptr; });
  95. // test_none_deferred
  96. m.def("defer_none_cstring", [](char *) { return false; });
  97. m.def("defer_none_cstring", [](py::none) { return true; });
  98. m.def("defer_none_custom", [](UserType *) { return false; });
  99. m.def("defer_none_custom", [](py::none) { return true; });
  100. m.def("nodefer_none_void", [](void *) { return true; });
  101. m.def("nodefer_none_void", [](py::none) { return false; });
  102. // test_void_caster
  103. m.def("load_nullptr_t", [](std::nullptr_t) {}); // not useful, but it should still compile
  104. m.def("cast_nullptr_t", []() { return std::nullptr_t{}; });
  105. // test_bool_caster
  106. m.def("bool_passthrough", [](bool arg) { return arg; });
  107. m.def("bool_passthrough_noconvert", [](bool arg) { return arg; }, py::arg().noconvert());
  108. // test_reference_wrapper
  109. m.def("refwrap_builtin", [](std::reference_wrapper<int> p) { return 10 * p.get(); });
  110. m.def("refwrap_usertype", [](std::reference_wrapper<UserType> p) { return p.get().value(); });
  111. // Not currently supported (std::pair caster has return-by-value cast operator);
  112. // triggers static_assert failure.
  113. //m.def("refwrap_pair", [](std::reference_wrapper<std::pair<int, int>>) { });
  114. m.def("refwrap_list", [](bool copy) {
  115. static IncType x1(1), x2(2);
  116. py::list l;
  117. for (auto &f : {std::ref(x1), std::ref(x2)}) {
  118. l.append(py::cast(f, copy ? py::return_value_policy::copy
  119. : py::return_value_policy::reference));
  120. }
  121. return l;
  122. }, "copy"_a);
  123. m.def("refwrap_iiw", [](const IncType &w) { return w.value(); });
  124. m.def("refwrap_call_iiw", [](IncType &w, py::function f) {
  125. py::list l;
  126. l.append(f(std::ref(w)));
  127. l.append(f(std::cref(w)));
  128. IncType x(w.value());
  129. l.append(f(std::ref(x)));
  130. IncType y(w.value());
  131. auto r3 = std::ref(y);
  132. l.append(f(r3));
  133. return l;
  134. });
  135. // test_complex
  136. m.def("complex_cast", [](float x) { return "{}"_s.format(x); });
  137. m.def("complex_cast", [](std::complex<float> x) { return "({}, {})"_s.format(x.real(), x.imag()); });
  138. }