14 # pragma warning(push) 15 # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant 20 m.def(
"string_roundtrip", [](
const char *
s) {
return s; });
24 char32_t a32 = 0x61 , z32 = 0x7a , ib32 = 0x203d , cake32 = 0x1f382 , mathbfA32 = 0x1d400 ;
25 char16_t b16 = 0x62 , z16 = 0x7a, ib16 = 0x203d, cake16_1 = 0xd83c, cake16_2 = 0xdf82, mathbfA16_1 = 0xd835, mathbfA16_2 = 0xdc00;
28 wstr.push_back(0x2e18);
29 if (
sizeof(
wchar_t) == 2) { wstr.push_back(mathbfA16_1); wstr.push_back(mathbfA16_2); }
30 else { wstr.push_back((
wchar_t) mathbfA32); }
33 m.def(
"good_utf8_string", []() {
return std::string((
const char*)u8
"Say utf8\u203d \U0001f382 \U0001d400"); });
34 m.def(
"good_utf16_string", [=]() {
return std::u16string({ b16, ib16, cake16_1, cake16_2, mathbfA16_1, mathbfA16_2, z16 }); });
35 m.def(
"good_utf32_string", [=]() {
return std::u32string({ a32, mathbfA32, cake32, ib32, z32 }); });
36 m.def(
"good_wchar_string", [=]() {
return wstr; });
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 }); });
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; });
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; });
60 m.def(
"strlen", [](
char *
s) {
return strlen(
s); });
61 m.def(
"string_length", [](std::string
s) {
return s.length(); });
63 #ifdef PYBIND11_HAS_U8STRING 64 m.attr(
"has_u8string") =
true;
65 m.def(
"good_utf8_u8string", []() {
return std::u8string(u8
"Say utf8\u203d \U0001f382 \U0001d400"); });
66 m.def(
"bad_utf8_u8string", []() {
return std::u8string((
const char8_t*)
"abc\xd0" "def"); });
68 m.def(
"u8_char8_Z", []() -> char8_t {
return u8
'Z'; });
71 m.def(
"ord_char8", [](char8_t
c) ->
int {
return static_cast<unsigned char>(
c); });
72 m.def(
"ord_char8_lv", [](char8_t &
c) ->
int {
return static_cast<unsigned char>(
c); });
76 #ifdef PYBIND11_HAS_STRING_VIEW 77 m.attr(
"has_string_view") =
true;
78 m.def(
"string_view_print", [](std::string_view
s) {
py::print(
s,
s.size()); });
79 m.def(
"string_view16_print", [](std::u16string_view
s) {
py::print(
s,
s.size()); });
80 m.def(
"string_view32_print", [](std::u32string_view
s) {
py::print(
s,
s.size()); });
81 m.def(
"string_view_chars", [](std::string_view
s) { py::list
l;
for (
auto c :
s) l.append((
std::uint8_t)
c);
return l; });
82 m.def(
"string_view16_chars", [](std::u16string_view
s) { py::list
l;
for (
auto c :
s) l.append((
int)
c);
return l; });
83 m.def(
"string_view32_chars", [](std::u32string_view
s) { py::list
l;
for (
auto c :
s) l.append((
int)
c);
return l; });
84 m.def(
"string_view_return", []() {
return std::string_view((
const char*)u8
"utf8 secret \U0001f382"); });
85 m.def(
"string_view16_return", []() {
return std::u16string_view(u
"utf16 secret \U0001f382"); });
86 m.def(
"string_view32_return", []() {
return std::u32string_view(
U"utf32 secret \U0001f382"); });
88 # ifdef PYBIND11_HAS_U8STRING 89 m.def(
"string_view8_print", [](std::u8string_view
s) {
py::print(
s,
s.size()); });
90 m.def(
"string_view8_chars", [](std::u8string_view
s) { py::list
l;
for (
auto c :
s) l.append((
std::uint8_t)
c);
return l; });
91 m.def(
"string_view8_return", []() {
return std::u8string_view(u8
"utf8 secret \U0001f382"); });
102 m.def(
"pair_passthrough", [](std::pair<bool, std::string> input) {
103 return std::make_pair(input.second, input.first);
104 },
"Return a pair in reversed order");
105 m.def(
"tuple_passthrough", [](std::tuple<bool, std::string, int> input) {
106 return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
107 },
"Return a triple in reversed order");
108 m.def(
"empty_tuple", []() {
return std::tuple<>(); });
109 static std::pair<RValueCaster, RValueCaster> lvpair;
110 static std::tuple<RValueCaster, RValueCaster, RValueCaster> lvtuple;
111 static std::pair<RValueCaster, std::tuple<RValueCaster, std::pair<RValueCaster, RValueCaster>>> lvnested;
113 m.def(
"lvalue_pair", []() ->
const decltype(lvpair) & {
return lvpair; });
115 m.def(
"lvalue_tuple", []() ->
const decltype(lvtuple) & {
return lvtuple; });
116 m.def(
"rvalue_nested", []() {
118 m.def(
"lvalue_nested", []() ->
const decltype(lvnested) & {
return lvnested; });
120 static std::pair<int, std::string> int_string_pair{2,
"items"};
121 m.def(
"int_string_pair", []() {
return &int_string_pair; });
124 m.def(
"return_none_string", []() -> std::string * {
return nullptr; });
125 m.def(
"return_none_char", []() ->
const char * {
return nullptr; });
126 m.def(
"return_none_bool", []() ->
bool * {
return nullptr; });
127 m.def(
"return_none_int", []() ->
int * {
return nullptr; });
128 m.def(
"return_none_float", []() ->
float * {
return nullptr; });
129 m.def(
"return_none_pair", []() -> std::pair<int,int> * {
return nullptr; });
132 m.def(
"defer_none_cstring", [](
char *) {
return false; });
133 m.def(
"defer_none_cstring", [](py::none) {
return true; });
134 m.def(
"defer_none_custom", [](UserType *) {
return false; });
135 m.def(
"defer_none_custom", [](py::none) {
return true; });
136 m.def(
"nodefer_none_void", [](
void *) {
return true; });
137 m.def(
"nodefer_none_void", [](py::none) {
return false; });
140 m.def(
"load_nullptr_t", [](std::nullptr_t) {});
141 m.def(
"cast_nullptr_t", []() {
return std::nullptr_t{}; });
144 m.def(
"bool_passthrough", [](
bool arg) {
return arg; });
145 m.def(
"bool_passthrough_noconvert", [](
bool arg) {
return arg; },
py::arg().noconvert());
148 m.def(
"refwrap_builtin", [](std::reference_wrapper<int>
p) {
return 10 *
p.get(); });
149 m.def(
"refwrap_usertype", [](std::reference_wrapper<UserType>
p) {
return p.get().value(); });
154 m.def(
"refwrap_list", [](
bool copy) {
155 static IncType
x1(1),
x2(2);
159 : py::return_value_policy::reference));
164 m.def(
"refwrap_iiw", [](
const IncType &
w) {
return w.value(); });
165 m.def(
"refwrap_call_iiw", [](IncType &
w, py::function
f) {
168 l.append(
f(std::cref(
w)));
169 IncType
x(
w.value());
171 IncType
y(
w.value());
178 m.def(
"complex_cast", [](
float x) {
return "{}"_s.format(
x); });
179 m.def(
"complex_cast", [](std::complex<float>
x) {
return "({}, {})"_s.format(
x.real(),
x.imag()); });
182 m.def(
"int_cast", []() {
return (
int) 42;});
183 m.def(
"long_cast", []() {
return (
long) 42;});
184 m.def(
"longlong_cast", []() {
return ULLONG_MAX;});
187 m.def(
"test_void_caster", []() ->
bool {
188 void *
v = (
void *) 0xabcd;
190 return py::cast<void *>(o) == v;
void print(const Matrix &A, const string &s, ostream &stream)
Pose3 x2(Rot3::Ypr(0.0, 0.0, 0.0), l2)
static const Line3 l(Rot3(), 1, 1)
Tuple< Args... > make_tuple(Args...args)
Creates a tuple object, deducing the target type from the types of arguments.
unsigned __int64 uint64_t
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
int EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
TEST_SUBMODULE(builtin_casters, m)