15 m.def(
"get_int", []{
return py::int_(0);});
17 m.def(
"get_iterator", []{
return py::iterator();});
19 m.def(
"get_iterable", []{
return py::iterable();});
21 m.def(
"get_list", []() {
25 list[0] = py::str(
"overwritten");
30 m.def(
"print_list", [](py::list
list) {
32 for (
auto item :
list)
33 py::print(
"list item {}: {}"_s.format(index++, item));
36 m.def(
"get_none", []{
return py::none();});
37 m.def(
"print_none", [](py::none
none) {
42 m.def(
"get_set", []() {
46 set.
add(std::string(
"key3"));
49 m.def(
"print_set", [](py::set
set) {
53 m.def(
"set_contains", [](py::set
set, py::object key) {
56 m.def(
"set_contains", [](py::set
set,
const char* key) {
61 m.def(
"get_dict", []() {
return py::dict(
"key"_a=
"value"); });
62 m.def(
"print_dict", [](py::dict
dict) {
63 for (
auto item :
dict)
64 py::print(
"key: {}, value={}"_s.format(item.first, item.second));
66 m.def(
"dict_keyword_constructor", []() {
67 auto d1 = py::dict(
"x"_a=1,
"y"_a=2);
68 auto d2 = py::dict(
"z"_a=3, **d1);
71 m.def(
"dict_contains", [](py::dict
dict, py::object val) {
74 m.def(
"dict_contains", [](py::dict
dict,
const char* val) {
79 m.def(
"str_from_string", []() {
return py::str(std::string(
"baz")); });
80 m.def(
"str_from_bytes", []() {
return py::str(py::bytes(
"boo", 3)); });
81 m.def(
"str_from_object", [](
const py::object& obj) {
return py::str(obj); });
82 m.def(
"repr_from_object", [](
const py::object& obj) {
return py::repr(obj); });
83 m.def(
"str_from_handle", [](py::handle h) {
return py::str(h); });
85 m.def(
"str_format", []() {
86 auto s1 =
"{} + {} = {}"_s.format(1, 2, 3);
87 auto s2 =
"{a} + {b} = {c}"_s.format(
"a"_a=1,
"b"_a=2,
"c"_a=3);
92 m.def(
"bytes_from_string", []() {
return py::bytes(std::string(
"foo")); });
93 m.def(
"bytes_from_str", []() {
return py::bytes(py::str(
"bar", 3)); });
96 m.def(
"return_capsule_with_destructor", []() {
98 return py::capsule([]() {
103 m.def(
"return_capsule_with_destructor_2", []() {
105 return py::capsule((
void *) 1234, [](
void *ptr) {
106 py::print(
"destructing capsule: {}"_s.format((
size_t) ptr));
110 m.def(
"return_capsule_with_name_and_destructor", []() {
111 auto capsule = py::capsule((
void *) 12345,
"pointer type description", [](PyObject *ptr) {
113 auto name = PyCapsule_GetName(ptr);
114 py::print(
"destructing capsule ({}, '{}')"_s.format(
115 (
size_t) PyCapsule_GetPointer(ptr,
name),
name
120 capsule.set_pointer((
void *) 1234);
123 void* contents1 =
static_cast<void*
>(
capsule);
124 void* contents2 =
capsule.get_pointer();
125 void* contents3 =
capsule.get_pointer<
void>();
127 auto result1 =
reinterpret_cast<size_t>(contents1);
128 auto result2 =
reinterpret_cast<size_t>(contents2);
129 auto result3 =
reinterpret_cast<size_t>(contents3);
131 py::print(
"created capsule ({}, '{}')"_s.format(result1 & result2 & result3,
capsule.name()));
136 m.def(
"accessor_api", [](py::object o) {
139 d[
"basic_attr"] = o.attr(
"basic_attr");
142 for (
auto item : o.attr(
"begin_end")) {
147 d[
"operator[object]"] = o.attr(
"d")[
"operator[object]"_s];
148 d[
"operator[char *]"] = o.attr(
"d")[
"operator[char *]"];
150 d[
"attr(object)"] = o.attr(
"sub").attr(
"attr_obj");
151 d[
"attr(char *)"] = o.attr(
"sub").attr(
"attr_char");
153 o.attr(
"sub").attr(
"missing").ptr();
154 }
catch (
const py::error_already_set &) {
155 d[
"missing_attr_ptr"] =
"raised"_s;
158 o.attr(
"missing").attr(
"doesn't matter");
159 }
catch (
const py::error_already_set &) {
160 d[
"missing_attr_chain"] =
"raised"_s;
163 d[
"is_none"] = o.attr(
"basic_attr").is_none();
165 d[
"operator()"] = o.attr(
"func")(1);
166 d[
"operator*"] = o.attr(
"func")(*o.attr(
"begin_end"));
169 py::list implicit_list = o.attr(
"begin_end");
170 d[
"implicit_list"] = implicit_list;
171 py::dict implicit_dict = o.attr(
"__dict__");
172 d[
"implicit_dict"] = implicit_dict;
177 m.def(
"tuple_accessor", [](py::tuple existing_t) {
180 }
catch (
const py::error_already_set &) {
183 auto new_t = py::tuple(3);
184 for (
size_t i = 0;
i < new_t.size(); ++
i) {
192 m.def(
"accessor_assignment", []() {
193 auto l = py::list(1);
199 d[
"deferred_get"] = var;
203 d[
"deferred_set"] = l[0];
210 m.def(
"default_constructors", []() {
212 "bytes"_a=py::bytes(),
214 "bool"_a=py::bool_(),
216 "float"_a=py::float_(),
217 "tuple"_a=py::tuple(),
224 m.def(
"converting_constructors", [](py::dict d) {
226 "bytes"_a=py::bytes(d[
"bytes"]),
227 "str"_a=py::str(d[
"str"]),
228 "bool"_a=py::bool_(d[
"bool"]),
229 "int"_a=py::int_(d[
"int"]),
230 "float"_a=py::float_(d[
"float"]),
231 "tuple"_a=py::tuple(d[
"tuple"]),
232 "list"_a=py::list(d[
"list"]),
233 "dict"_a=py::dict(d[
"dict"]),
234 "set"_a=py::set(d[
"set"]),
235 "memoryview"_a=py::memoryview(d[
"memoryview"])
239 m.def(
"cast_functions", [](py::dict d) {
242 "bytes"_a=d[
"bytes"].cast<py::bytes>(),
243 "str"_a=d[
"str"].cast<py::str>(),
244 "bool"_a=d[
"bool"].cast<py::bool_>(),
245 "int"_a=d[
"int"].cast<py::int_>(),
246 "float"_a=d[
"float"].cast<py::float_>(),
247 "tuple"_a=d[
"tuple"].cast<py::tuple>(),
248 "list"_a=d[
"list"].cast<py::list>(),
249 "dict"_a=d[
"dict"].cast<py::dict>(),
250 "set"_a=d[
"set"].cast<py::set>(),
251 "memoryview"_a=d[
"memoryview"].cast<py::memoryview>()
255 m.def(
"convert_to_pybind11_str", [](py::object o) {
return py::str(o); });
257 m.def(
"nonconverting_constructor", [](std::string
type, py::object
value,
bool move) -> py::object {
258 if (
type ==
"bytes") {
261 else if (
type ==
"none") {
264 else if (
type ==
"ellipsis") {
267 else if (
type ==
"type") {
270 throw std::runtime_error(
"Invalid type");
273 m.def(
"get_implicit_casting", []() {
275 d[
"char*_i1"] =
"abc";
276 const char *c2 =
"abc";
279 d[
"char*_p"] = py::str(c2);
287 d[
"int_p"] = py::int_(
i);
289 d[
"str_i1"] = std::string(
"str");
290 std::string s2(
"str1");
295 d[
"str_p"] = py::str(s2);
302 l.append(py::int_(15));
311 m.def(
"print_function", []() {
313 py::print(1, 2.0,
"three",
true, std::string(
"-- multiple args"));
316 py::print(
"no new line here",
"end"_a=
" -- ");
319 auto py_stderr = py::module_::import(
"sys").attr(
"stderr");
320 py::print(
"this goes to stderr",
"file"_a=py_stderr);
324 py::print(
"{a} + {b} = {c}"_s.format(
"a"_a=
"py::print",
"b"_a=
"str.format",
"c"_a=
"this"));
329 m.def(
"hash_function", [](py::object obj) {
return py::hash(obj); });
331 m.def(
"test_number_protocol", [](py::object a, py::object b) {
333 l.append(a.equal(b));
334 l.append(a.not_equal(b));
351 m.def(
"test_list_slicing", [](py::list a) {
352 return a[py::slice(0, -1, 2)];
356 m.def(
"issue2361_str_implicit_copy_none", []() {
357 py::str is_this_none = py::none();
360 m.def(
"issue2361_dict_implicit_copy_none", []() {
361 py::dict is_this_none = py::none();
365 m.def(
"test_memoryview_object", [](py::buffer b) {
366 return py::memoryview(b);
369 m.def(
"test_memoryview_buffer_info", [](py::buffer b) {
370 return py::memoryview(b.request());
373 m.def(
"test_memoryview_from_buffer", [](
bool is_unsigned) {
374 static const int16_t si16[] = { 3, 1, 4, 1, 5 };
375 static const uint16_t ui16[] = { 2, 7, 1, 8 };
377 return py::memoryview::from_buffer(
378 ui16, { 4 }, {
sizeof(uint16_t) });
380 return py::memoryview::from_buffer(
381 si16, { 5 }, {
sizeof(int16_t) });
384 m.def(
"test_memoryview_from_buffer_nativeformat", []() {
385 static const char* format =
"@i";
386 static const int32_t
arr[] = { 4, 7, 5 };
387 return py::memoryview::from_buffer(
388 arr,
sizeof(int32_t), format, { 3 }, {
sizeof(int32_t) });
391 m.def(
"test_memoryview_from_buffer_empty_shape", []() {
392 static const char* buf =
"";
393 return py::memoryview::from_buffer(buf, 1,
"B", { }, { });
396 m.def(
"test_memoryview_from_buffer_invalid_strides", []() {
397 static const char* buf =
"\x02\x03\x04";
398 return py::memoryview::from_buffer(buf, 1,
"B", { 3 }, { });
401 m.def(
"test_memoryview_from_buffer_nullptr", []() {
402 return py::memoryview::from_buffer(
403 static_cast<void*
>(
nullptr), 1,
"B", { }, { });
406 #if PY_MAJOR_VERSION >= 3
407 m.def(
"test_memoryview_from_memory", []() {
408 const char* buf =
"\xff\xe1\xab\x37";
409 return py::memoryview::from_memory(
415 m.def(
"get_len", [](py::handle h) {
return py::len(h); });
417 #ifdef PYBIND11_STR_LEGACY_PERMISSIVE
418 m.attr(
"PYBIND11_STR_LEGACY_PERMISSIVE") =
true;
421 m.def(
"isinstance_pybind11_bytes", [](py::object o) {
return py::isinstance<py::bytes>(o); });
422 m.def(
"isinstance_pybind11_str", [](py::object o) {
return py::isinstance<py::str>(o); });
424 m.def(
"pass_to_pybind11_bytes", [](py::bytes b) {
return py::len(b); });
425 m.def(
"pass_to_pybind11_str", [](py::str s) {
return py::len(s); });
426 m.def(
"pass_to_std_string", [](std::string s) {
return s.size(); });
429 m.def(
"weakref_from_handle",
430 [](py::handle h) {
return py::weakref(h); });
431 m.def(
"weakref_from_handle_and_function",
432 [](py::handle h, py::function f) {
return py::weakref(h, f); });
433 m.def(
"weakref_from_object",
434 [](py::object o) {
return py::weakref(o); });
435 m.def(
"weakref_from_object_and_function",
436 [](py::object o, py::function f) {
return py::weakref(o, f); });