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