23 static constexpr
auto name = const_name<ConstRefCasted>();
32 return std::move(
value);
53 template <
typename T_>
80 m.def(
"string_roundtrip", [](
const char *
s) {
return s; });
85 char32_t a32 = 0x61 , z32 = 0x7a , ib32 = 0x203d , cake32 = 0x1f382 ,
87 char16_t b16 = 0x62 , z16 = 0x7a, ib16 = 0x203d, cake16_1 = 0xd83c, cake16_2 = 0xdf82,
88 mathbfA16_1 = 0xd835, mathbfA16_2 = 0xdc00;
91 wstr.push_back(0x2e18);
92 if (
sizeof(
wchar_t) == 2) {
93 wstr.push_back(mathbfA16_1);
94 wstr.push_back(mathbfA16_2);
97 wstr.push_back((
wchar_t) mathbfA32);
101 m.def(
"good_utf8_string", []() {
102 return std::string((
const char *) u8
"Say utf8\u203d \U0001f382 \U0001d400");
104 m.def(
"good_utf16_string", [=]() {
105 return std::u16string({b16, ib16, cake16_1, cake16_2, mathbfA16_1, mathbfA16_2, z16});
107 m.def(
"good_utf32_string",
108 [=]() {
return std::u32string({a32, mathbfA32, cake32, ib32, z32}); });
109 m.def(
"good_wchar_string", [=]() {
return wstr; });
110 m.def(
"bad_utf8_string", []() {
111 return std::string(
"abc\xd0"
114 m.def(
"bad_utf16_string", [=]() {
return std::u16string({b16, char16_t(0xd800), z16}); });
117 m.def(
"bad_utf32_string", [=]() {
return std::u32string({a32, char32_t(0xd800), z32}); });
118 if (
sizeof(
wchar_t) == 2) {
119 m.def(
"bad_wchar_string",
120 [=]() {
return std::wstring({wchar_t(0x61), wchar_t(0xd800)}); });
122 m.def(
"u8_Z", []() ->
char {
return 'Z'; });
123 m.def(
"u8_eacute", []() ->
char {
return '\xe9'; });
124 m.def(
"u16_ibang", [=]() -> char16_t {
return ib16; });
125 m.def(
"u32_mathbfA", [=]() -> char32_t {
return mathbfA32; });
126 m.def(
"wchar_heart", []() ->
wchar_t {
return 0x2665; });
129 m.attr(
"wchar_size") =
py::cast(
sizeof(
wchar_t));
130 m.def(
"ord_char", [](
char c) ->
int {
return static_cast<unsigned char>(
c); });
131 m.def(
"ord_char_lv", [](
char &
c) ->
int {
return static_cast<unsigned char>(
c); });
132 m.def(
"ord_char16", [](char16_t
c) ->
uint16_t {
return c; });
133 m.def(
"ord_char16_lv", [](char16_t &
c) ->
uint16_t {
return c; });
134 m.def(
"ord_char32", [](char32_t
c) ->
uint32_t {
return c; });
135 m.def(
"ord_wchar", [](
wchar_t c) ->
int {
return c; });
138 m.def(
"strlen", [](
char *
s) {
return strlen(
s); });
139 m.def(
"string_length", [](
const std::string &
s) {
return s.length(); });
141 #ifdef PYBIND11_HAS_U8STRING
142 m.attr(
"has_u8string") =
true;
143 m.def(
"good_utf8_u8string", []() {
144 return std::u8string(u8
"Say utf8\u203d \U0001f382 \U0001d400");
146 m.def(
"bad_utf8_u8string", []() {
147 return std::u8string((
const char8_t *)
"abc\xd0"
151 m.def(
"u8_char8_Z", []() -> char8_t {
return u8
'Z'; });
154 m.def(
"ord_char8", [](char8_t
c) ->
int {
return static_cast<unsigned char>(
c); });
155 m.def(
"ord_char8_lv", [](char8_t &
c) ->
int {
return static_cast<unsigned char>(
c); });
159 #ifdef PYBIND11_HAS_STRING_VIEW
160 m.attr(
"has_string_view") =
true;
161 m.def(
"string_view_print", [](std::string_view
s) {
py::print(
s,
s.size()); });
162 m.def(
"string_view16_print", [](std::u16string_view
s) {
py::print(
s,
s.size()); });
163 m.def(
"string_view32_print", [](std::u32string_view
s) {
py::print(
s,
s.size()); });
164 m.def(
"string_view_chars", [](std::string_view
s) {
171 m.def(
"string_view16_chars", [](std::u16string_view
s) {
178 m.def(
"string_view32_chars", [](std::u32string_view
s) {
185 m.def(
"string_view_return",
186 []() {
return std::string_view((
const char *) u8
"utf8 secret \U0001f382"); });
187 m.def(
"string_view16_return",
188 []() {
return std::u16string_view(u
"utf16 secret \U0001f382"); });
189 m.def(
"string_view32_return",
190 []() {
return std::u32string_view(
U"utf32 secret \U0001f382"); });
193 using namespace std::literals;
194 m.def(
"string_view_bytes",
195 []() {
return [](py::bytes
b) {
return b; }(
"abc \x80\x80 def"sv); });
196 m.def(
"string_view_str",
197 []() {
return [](
py::str s) {
return s; }(
"abc \342\200\275 def"sv); });
198 m.def(
"string_view_from_bytes",
199 [](
const py::bytes &
b) {
return [](std::string_view
s) {
return s; }(
b); });
200 m.def(
"string_view_memoryview", []() {
201 static constexpr
auto val =
"Have some \360\237\216\202"sv;
202 return py::memoryview::from_memory(val);
205 # ifdef PYBIND11_HAS_U8STRING
206 m.def(
"string_view8_print", [](std::u8string_view
s) {
py::print(
s,
s.size()); });
207 m.def(
"string_view8_chars", [](std::u8string_view
s) {
213 m.def(
"string_view8_return", []() {
return std::u8string_view(u8
"utf8 secret \U0001f382"); });
214 m.def(
"string_view8_str", []() {
return py::str{std::u8string_view{u8
"abc β½ def"}}; });
217 struct TypeWithBothOperatorStringAndStringView {
219 operator std::string()
const {
return "success"; }
221 operator std::string_view()
const {
return "failure"; }
223 m.def(
"bytes_from_type_with_both_operator_string_and_string_view",
224 []() {
return py::bytes(TypeWithBothOperatorStringAndStringView()); });
225 m.def(
"str_from_type_with_both_operator_string_and_string_view",
226 []() {
return py::str(TypeWithBothOperatorStringAndStringView()); });
236 m.def(
"int_passthrough", [](
int arg) {
return arg; });
237 m.def(
"int_passthrough_noconvert", [](
int arg) {
return arg; },
py::arg{}.noconvert());
242 [](
const std::pair<bool, std::string> &input) {
243 return std::make_pair(input.second, input.first);
245 "Return a pair in reversed order");
248 [](std::tuple<bool, std::string, int> input) {
249 return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
251 "Return a triple in reversed order");
252 m.def(
"empty_tuple", []() {
return std::tuple<>(); });
253 static std::pair<RValueCaster, RValueCaster> lvpair;
254 static std::tuple<RValueCaster, RValueCaster, RValueCaster> lvtuple;
255 static std::pair<RValueCaster, std::tuple<RValueCaster, std::pair<RValueCaster, RValueCaster>>>
258 m.def(
"lvalue_pair", []() ->
const decltype(lvpair) & {
return lvpair; });
259 m.def(
"rvalue_tuple",
261 m.def(
"lvalue_tuple", []() ->
const decltype(lvtuple) & {
return lvtuple; });
262 m.def(
"rvalue_nested", []() {
263 return std::make_pair(
267 m.def(
"lvalue_nested", []() ->
const decltype(lvnested) & {
return lvnested; });
273 static auto *int_string_pair =
new std::pair<int, std::string>{2,
"items"};
274 return int_string_pair;
276 py::return_value_policy::reference);
279 m.def(
"return_none_string", []() -> std::string * {
return nullptr; });
280 m.def(
"return_none_char", []() ->
const char * {
return nullptr; });
281 m.def(
"return_none_bool", []() ->
bool * {
return nullptr; });
282 m.def(
"return_none_int", []() ->
int * {
return nullptr; });
283 m.def(
"return_none_float", []() ->
float * {
return nullptr; });
284 m.def(
"return_none_pair", []() -> std::pair<int, int> * {
return nullptr; });
287 m.def(
"defer_none_cstring", [](
char *) {
return false; });
288 m.def(
"defer_none_cstring", [](
const py::none &) {
return true; });
289 m.def(
"defer_none_custom", [](UserType *) {
return false; });
290 m.def(
"defer_none_custom", [](
const py::none &) {
return true; });
291 m.def(
"nodefer_none_void", [](
void *) {
return true; });
292 m.def(
"nodefer_none_void", [](
const py::none &) {
return false; });
295 m.def(
"load_nullptr_t", [](std::nullptr_t) {});
296 m.def(
"cast_nullptr_t", []() {
return std::nullptr_t{}; });
301 m.def(
"bool_passthrough", [](
bool arg) {
return arg; });
302 m.def(
"bool_passthrough_noconvert", [](
bool arg) {
return arg; },
py::arg{}.noconvert());
305 #if !defined(__INTEL_COMPILER)
310 m.def(
"bool_passthrough_noconvert2", [](
bool arg) {
return arg; },
py::arg().noconvert());
314 m.def(
"refwrap_builtin", [](std::reference_wrapper<int>
p) {
return 10 *
p.get(); });
315 m.def(
"refwrap_usertype", [](std::reference_wrapper<UserType>
p) {
return p.get().value(); });
316 m.def(
"refwrap_usertype_const",
317 [](std::reference_wrapper<const UserType>
p) {
return p.get().value(); });
319 m.def(
"refwrap_lvalue", []() -> std::reference_wrapper<UserType> {
320 static UserType
x(1);
323 m.def(
"refwrap_lvalue_const", []() -> std::reference_wrapper<const UserType> {
324 static UserType
x(1);
335 static IncType
x1(1),
x2(2);
345 m.def(
"refwrap_iiw", [](
const IncType &
w) {
return w.value(); });
346 m.def(
"refwrap_call_iiw", [](IncType &
w,
const py::function &
f) {
349 l.append(
f(std::cref(
w)));
350 IncType
x(
w.value());
352 IncType
y(
w.value());
359 m.def(
"complex_cast", [](
float x) {
return "{}"_s.format(
x); });
360 m.def(
"complex_cast",
361 [](std::complex<float>
x) {
return "({}, {})"_s.format(
x.real(),
x.imag()); });
364 m.def(
"int_cast", []() {
return (
int) 42; });
365 m.def(
"long_cast", []() {
return (
long) 42; });
366 m.def(
"longlong_cast", []() {
return ULLONG_MAX; });
369 m.def(
"test_void_caster", []() ->
bool {
370 void *
v = (
void *) 0xabcd;
372 return py::cast<void *>(o) ==
v;
380 m.def(
"takes_ref_wrap", [](std::reference_wrapper<ConstRefCasted>
x) {
return x.get().tag; });
383 m.def(
"takes_const_ref_wrap",
384 [](std::reference_wrapper<const ConstRefCasted>
x) {
return x.get().tag; });