test_builtin_casters.cpp 15.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
    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>

12
13
#include "pybind11_tests.h"

14
struct ConstRefCasted {
15
    int tag;
16
17
18
19
20
21
};

PYBIND11_NAMESPACE_BEGIN(pybind11)
PYBIND11_NAMESPACE_BEGIN(detail)
template <>
class type_caster<ConstRefCasted> {
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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};
71
72
73
74
};
PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(pybind11)

75
76
77
78
79
TEST_SUBMODULE(builtin_casters, m) {
    // test_simple_string
    m.def("string_roundtrip", [](const char *s) { return s; });

    // test_unicode_conversion
80
81
82
83
84
85
    // 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;
86
    std::wstring wstr;
87
    wstr.push_back(0x61);   // a
88
    wstr.push_back(0x2e18); // ⸘
89
90
91
92
93
94
95
    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
96
97
    wstr.push_back(0x7a); // z

98
99
100
101
102
103
104
105
106
    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
107
    m.def("good_wchar_string", [=]() { return wstr; }); // a‽𝐀z
108
109
110
111
112
    m.def("bad_utf8_string", []() {
        return std::string("abc\xd0"
                           "def");
    });
    m.def("bad_utf16_string", [=]() { return std::u16string({b16, char16_t(0xd800), z16}); });
113
    // Under Python 2.7, invalid unicode UTF-32 characters didn't appear to trigger
114
115
    // UnicodeDecodeError
    m.def("bad_utf32_string", [=]() { return std::u32string({a32, char32_t(0xd800), z32}); });
116
117
118
119
120
    if (PYBIND11_SILENCE_MSVC_C4127(sizeof(wchar_t) == 2)) {
        m.def("bad_wchar_string", [=]() {
            return std::wstring({wchar_t(0x61), wchar_t(0xd800)});
        });
    }
121
122
123
124
125
126
127
128
129
    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); });
130
    m.def("ord_char_lv", [](char &c) -> int { return static_cast<unsigned char>(c); });
131
    m.def("ord_char16", [](char16_t c) -> uint16_t { return c; });
132
    m.def("ord_char16_lv", [](char16_t &c) -> uint16_t { return c; });
133
134
135
136
137
    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); });
138
    m.def("string_length", [](const std::string &s) { return s.length(); });
139

140
141
#ifdef PYBIND11_HAS_U8STRING
    m.attr("has_u8string") = true;
142
143
144
145
146
147
148
    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");
    });
149
150
151
152
153
154
155
156

    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

157
158
159
    // test_string_view
#ifdef PYBIND11_HAS_STRING_VIEW
    m.attr("has_string_view") = true;
160
    m.def("string_view_print", [](std::string_view s) { py::print(s, s.size()); });
161
162
    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()); });
163
164
    m.def("string_view_chars", [](std::string_view s) {
        py::list l;
165
166
167
        for (auto c : s) {
            l.append((std::uint8_t) c);
        }
168
169
170
171
        return l;
    });
    m.def("string_view16_chars", [](std::u16string_view s) {
        py::list l;
172
173
174
        for (auto c : s) {
            l.append((int) c);
        }
175
176
177
178
        return l;
    });
    m.def("string_view32_chars", [](std::u32string_view s) {
        py::list l;
179
180
181
        for (auto c : s) {
            l.append((int) c);
        }
182
183
184
185
186
187
188
189
        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"); });
190

191
192
    // The inner lambdas here are to also test implicit conversion
    using namespace std::literals;
193
194
195
196
197
198
    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); });
199
200
201
202
203
    m.def("string_view_memoryview", []() {
        static constexpr auto val = "Have some \360\237\216\202"sv;
        return py::memoryview::from_memory(val);
    });

204
205
206
207
208
209
210
211
#    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;
    });
212
    m.def("string_view8_return", []() { return std::u8string_view(u8"utf8 secret \U0001f382"); });
213
214
    m.def("string_view8_str", []() { return py::str{std::u8string_view{u8"abc ‽ def"}}; });
#    endif
215
216
217
218
219
220
221
222
223
224
225

    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()); });
226
227
#endif

228
229
230
231
232
233
    // 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); });

234
235
    // test_int_convert
    m.def("int_passthrough", [](int arg) { return arg; });
236
237
    m.def(
        "int_passthrough_noconvert", [](int arg) { return arg; }, py::arg{}.noconvert());
238

239
    // test_tuple
240
241
242
243
244
245
    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");
246
247
248
249
250
251
    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");
252
    m.def("empty_tuple", []() { return std::tuple<>(); });
253
254
    static std::pair<RValueCaster, RValueCaster> lvpair;
    static std::tuple<RValueCaster, RValueCaster, RValueCaster> lvtuple;
255
256
    static std::pair<RValueCaster, std::tuple<RValueCaster, std::pair<RValueCaster, RValueCaster>>>
        lvnested;
257
258
    m.def("rvalue_pair", []() { return std::make_pair(RValueCaster{}, RValueCaster{}); });
    m.def("lvalue_pair", []() -> const decltype(lvpair) & { return lvpair; });
259
260
    m.def("rvalue_tuple",
          []() { return std::make_tuple(RValueCaster{}, RValueCaster{}, RValueCaster{}); });
261
262
    m.def("lvalue_tuple", []() -> const decltype(lvtuple) & { return lvtuple; });
    m.def("rvalue_nested", []() {
263
264
265
266
        return std::make_pair(
            RValueCaster{},
            std::make_tuple(RValueCaster{}, std::make_pair(RValueCaster{}, RValueCaster{})));
    });
267
    m.def("lvalue_nested", []() -> const decltype(lvnested) & { return lvnested; });
268

269
    m.def(
270
271
272
273
274
275
276
        "int_string_pair",
        []() {
            // Using no-destructor idiom to side-step warnings from overzealous compilers.
            static auto *int_string_pair = new std::pair<int, std::string>{2, "items"};
            return int_string_pair;
        },
        py::return_value_policy::reference);
277

278
279
    // test_builtins_cast_return_none
    m.def("return_none_string", []() -> std::string * { return nullptr; });
280
281
282
283
284
    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; });
285
286
287

    // test_none_deferred
    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; });
293
294
295
296
297

    // 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{}; });

298
299
    // [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works.

300
301
    // test_bool_caster
    m.def("bool_passthrough", [](bool arg) { return arg; });
302
303
    m.def(
        "bool_passthrough_noconvert", [](bool arg) { return arg; }, py::arg{}.noconvert());
304

305
306
307
308
309
310
    // 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.
311
312
    m.def(
        "bool_passthrough_noconvert2", [](bool arg) { return arg; }, py::arg().noconvert());
313
314
#endif

315
316
317
    // 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(); });
318
319
    m.def("refwrap_usertype_const",
          [](std::reference_wrapper<const UserType> p) { return p.get().value(); });
320
321
322
323
324
325
326
327
328
329

    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);
    });

330
331
    // Not currently supported (std::pair caster has return-by-value cast operator);
    // triggers static_assert failure.
332
    // m.def("refwrap_pair", [](std::reference_wrapper<std::pair<int, int>>) { });
333

334
335
336
337
338
339
340
341
342
343
344
345
    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);
346
347

    m.def("refwrap_iiw", [](const IncType &w) { return w.value(); });
348
    m.def("refwrap_call_iiw", [](IncType &w, const py::function &f) {
349
350
351
352
353
354
355
356
357
358
359
360
361
        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); });
362
363
    m.def("complex_cast",
          [](std::complex<float> x) { return "({}, {})"_s.format(x.real(), x.imag()); });
364
365

    // test int vs. long (Python 2)
366
367
368
    m.def("int_cast", []() { return (int) 42; });
    m.def("long_cast", []() { return (long) 42; });
    m.def("longlong_cast", []() { return ULLONG_MAX; });
Wenzel Jakob's avatar
Wenzel Jakob committed
369
370
371
372
373
374
375

    /// 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;
    });
376
377
378

    // Tests const/non-const propagation in cast_op.
    m.def("takes", [](ConstRefCasted x) { return x.tag; });
379
380
381
    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; });
382
    m.def("takes_ref_wrap", [](std::reference_wrapper<ConstRefCasted> x) { return x.get().tag; });
383
384
385
386
    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; });
387
}