15 m.def(
"get_list", []() {
18 py::print(
"Entry at position 0:", list[0]);
19 list[0] =
py::str(
"overwritten");
20 list.insert(0,
"inserted-0");
21 list.insert(2,
"inserted-2");
24 m.def(
"print_list", [](
py::list list) {
26 for (
auto item : list)
27 py::print(
"list item {}: {}"_s.format(index++, item));
31 m.def(
"get_set", []() {
35 set.add(std::string(
"key3"));
38 m.def(
"print_set", [](
py::set set) {
43 return set.contains(key);
45 m.def(
"set_contains", [](
py::set set,
const char* key) {
46 return set.contains(key);
50 m.def(
"get_dict", []() {
return py::dict(
"key"_a=
"value"); });
51 m.def(
"print_dict", [](
py::dict dict) {
52 for (
auto item : dict)
53 py::print(
"key: {}, value={}"_s.format(item.first, item.second));
55 m.def(
"dict_keyword_constructor", []() {
56 auto d1 =
py::dict(
"x"_a=1,
"y"_a=2);
63 m.def(
"dict_contains", [](
py::dict dict,
const char* val) {
68 m.def(
"str_from_string", []() {
return py::str(std::string(
"baz")); });
73 m.def(
"str_format", []() {
74 auto s1 =
"{} + {} = {}"_s.format(1, 2, 3);
75 auto s2 =
"{a} + {b} = {c}"_s.format(
"a"_a=1,
"b"_a=2,
"c"_a=3);
80 m.def(
"bytes_from_string", []() {
return py::bytes(std::string(
"foo")); });
84 m.def(
"return_capsule_with_destructor", []() {
91 m.def(
"return_capsule_with_destructor_2", []() {
94 py::print(
"destructing capsule: {}"_s.format((
size_t) ptr));
98 m.def(
"return_capsule_with_name_and_destructor", []() {
99 auto capsule =
py::capsule((
void *) 1234,
"pointer type description", [](PyObject *ptr) {
101 auto name = PyCapsule_GetName(ptr);
102 py::print(
"destructing capsule ({}, '{}')"_s.format(
103 (
size_t) PyCapsule_GetPointer(ptr, name), name
107 void *contents = capsule;
108 py::print(
"created capsule ({}, '{}')"_s.format((
size_t) contents, capsule.name()));
116 d[
"basic_attr"] = o.
attr(
"basic_attr");
119 for (
const auto &item : o.
attr(
"begin_end")) {
124 d[
"operator[object]"] = o.
attr(
"d")[
"operator[object]"_s];
125 d[
"operator[char *]"] = o.
attr(
"d")[
"operator[char *]"];
127 d[
"attr(object)"] = o.
attr(
"sub").
attr(
"attr_obj");
128 d[
"attr(char *)"] = o.
attr(
"sub").
attr(
"attr_char");
130 o.
attr(
"sub").
attr(
"missing").ptr();
132 d[
"missing_attr_ptr"] =
"raised"_s;
135 o.
attr(
"missing").
attr(
"doesn't matter");
137 d[
"missing_attr_chain"] =
"raised"_s;
142 d[
"operator()"] = o.
attr(
"func")(1);
143 d[
"operator*"] = o.
attr(
"func")(*o.
attr(
"begin_end"));
147 d[
"implicit_list"] = implicit_list;
149 d[
"implicit_dict"] = implicit_dict;
154 m.def(
"tuple_accessor", [](
py::tuple existing_t) {
161 for (
size_t i = 0;
i < new_t.size(); ++
i) {
169 m.def(
"accessor_assignment", []() {
176 d[
"deferred_get"] = var;
180 d[
"deferred_set"] = l[0];
187 m.def(
"default_constructors", []() {
200 m.def(
"converting_constructors", [](
py::dict d) {
214 m.def(
"cast_functions", [](
py::dict d) {
217 "str"_a=d[
"str"].cast<py::str>(),
218 "bool"_a=d[
"bool"].cast<py::bool_>(),
219 "int"_a=d[
"int"].cast<py::int_>(),
220 "float"_a=d[
"float"].cast<py::float_>(),
221 "tuple"_a=d[
"tuple"].cast<py::tuple>(),
222 "list"_a=d[
"list"].cast<py::list>(),
223 "dict"_a=d[
"dict"].cast<py::dict>(),
224 "set"_a=d[
"set"].cast<py::set>(),
225 "memoryview"_a=d[
"memoryview"].cast<py::memoryview>()
229 m.def(
"get_implicit_casting", []() {
231 d[
"char*_i1"] =
"abc";
232 const char *c2 =
"abc";
245 d[
"str_i1"] = std::string(
"str");
246 std::string s2(
"str1");
267 m.def(
"print_function", []() {
269 py::print(1, 2.0,
"three",
true, std::string(
"-- multiple args"));
272 py::print(
"no new line here",
"end"_a=
" -- ");
275 auto py_stderr = py::module::import(
"sys").attr(
"stderr");
276 py::print(
"this goes to stderr",
"file"_a=py_stderr);
280 py::print(
"{a} + {b} = {c}"_s.format(
"a"_a=
"py::print",
"b"_a=
"str.format",
"c"_a=
"this"));
289 l.append(a.
equal(b));
307 m.def(
"test_list_slicing", [](
py::list a) {
Dummy type which is not exported anywhere – something to trigger a conversion error.
bool equal(object_api const &other) const
Equivalent to obj == other in Python.
bool contains(T &&key) const
obj_attr_accessor attr(handle key) const
bool is_none() const
Equivalent to obj is None in Python.
op_< op_int, op_u, self_t, undefined_t > int_(const self_t &)
op_< op_hash, op_u, self_t, undefined_t > hash(const self_t &)
test_initializer pytypes("pytypes", test_submodule_pytypes)
T cast(const handle &handle)
void print(Args &&...args)
#define TEST_SUBMODULE(name, variable)
bool not_equal(object_api const &other) const