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", []() {
24 py::print(
"Entry at position 0:", list[0]);
25 list[0] =
py::str(
"overwritten");
26 list.insert(0,
"inserted-0");
27 list.insert(2,
"inserted-2");
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"));
53 m.def(
"set_contains", [](
py::set set, py::object
key) {
54 return set.contains(key);
56 m.def(
"set_contains", [](
py::set set,
const char* key) {
57 return set.contains(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) {
72 return dict.contains(val);
74 m.def(
"dict_contains", [](py::dict dict,
const char* val) {
75 return dict.contains(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 *) 1234,
"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 py::print(
"created capsule ({}, '{}')"_s.format((
size_t) contents,
capsule.name()));
125 m.def(
"accessor_api", [](py::object o) {
128 d[
"basic_attr"] = o.attr(
"basic_attr");
131 for (
const auto &item : o.attr(
"begin_end")) {
136 d[
"operator[object]"] = o.attr(
"d")[
"operator[object]"_s];
137 d[
"operator[char *]"] = o.attr(
"d")[
"operator[char *]"];
139 d[
"attr(object)"] = o.attr(
"sub").attr(
"attr_obj");
140 d[
"attr(char *)"] = o.attr(
"sub").attr(
"attr_char");
142 o.attr(
"sub").attr(
"missing").ptr();
143 }
catch (
const py::error_already_set &) {
144 d[
"missing_attr_ptr"] =
"raised"_s;
147 o.attr(
"missing").attr(
"doesn't matter");
148 }
catch (
const py::error_already_set &) {
149 d[
"missing_attr_chain"] =
"raised"_s;
152 d[
"is_none"] = o.attr(
"basic_attr").is_none();
154 d[
"operator()"] = o.attr(
"func")(1);
155 d[
"operator*"] = o.attr(
"func")(*o.attr(
"begin_end"));
158 py::list implicit_list = o.attr(
"begin_end");
159 d[
"implicit_list"] = implicit_list;
160 py::dict implicit_dict = o.attr(
"__dict__");
161 d[
"implicit_dict"] = implicit_dict;
166 m.def(
"tuple_accessor", [](py::tuple existing_t) {
169 }
catch (
const py::error_already_set &) {
172 auto new_t = py::tuple(3);
173 for (
size_t i = 0;
i < new_t.size(); ++
i) {
181 m.def(
"accessor_assignment", []() {
182 auto l = py::list(1);
188 d[
"deferred_get"] = var;
192 d[
"deferred_set"] =
l[0];
199 m.def(
"default_constructors", []() {
201 "bytes"_a=py::bytes(),
203 "bool"_a=py::bool_(),
205 "float"_a=py::float_(),
206 "tuple"_a=py::tuple(),
213 m.def(
"converting_constructors", [](py::dict
d) {
215 "bytes"_a=py::bytes(d[
"bytes"]),
217 "bool"_a=py::bool_(d[
"bool"]),
218 "int"_a=py::int_(d[
"int"]),
219 "float"_a=py::float_(d[
"float"]),
220 "tuple"_a=py::tuple(d[
"tuple"]),
221 "list"_a=py::list(d[
"list"]),
222 "dict"_a=py::dict(d[
"dict"]),
224 "memoryview"_a=py::memoryview(d[
"memoryview"])
228 m.def(
"cast_functions", [](py::dict d) {
231 "bytes"_a=d[
"bytes"].cast<py::bytes>(),
232 "str"_a=d[
"str"].cast<py::str>(),
233 "bool"_a=d[
"bool"].cast<py::bool_>(),
234 "int"_a=d[
"int"].cast<py::int_>(),
235 "float"_a=d[
"float"].cast<py::float_>(),
236 "tuple"_a=d[
"tuple"].cast<py::tuple>(),
237 "list"_a=d[
"list"].cast<py::list>(),
238 "dict"_a=d[
"dict"].cast<py::dict>(),
239 "set"_a=d[
"set"].cast<py::set>(),
240 "memoryview"_a=d[
"memoryview"].cast<py::memoryview>()
244 m.def(
"convert_to_pybind11_str", [](py::object o) {
return py::str(o); });
246 m.def(
"get_implicit_casting", []() {
248 d[
"char*_i1"] =
"abc";
249 const char *
c2 =
"abc";
260 d[
"int_p"] = py::int_(i);
262 d[
"str_i1"] = std::string(
"str");
263 std::string s2(
"str1");
275 l.append(py::int_(15));
284 m.def(
"print_function", []() {
286 py::print(1, 2.0,
"three",
true, std::string(
"-- multiple args"));
289 py::print(
"no new line here",
"end"_a=
" -- ");
292 auto py_stderr = py::module::import(
"sys").attr(
"stderr");
293 py::print(
"this goes to stderr",
"file"_a=py_stderr);
297 py::print(
"{a} + {b} = {c}"_s.format(
"a"_a=
"py::print",
"b"_a=
"str.format",
"c"_a=
"this"));
302 m.def(
"hash_function", [](py::object obj) {
return py::hash(obj); });
304 m.def(
"test_number_protocol", [](py::object
a, py::object
b) {
306 l.append(a.equal(b));
307 l.append(a.not_equal(b));
324 m.def(
"test_list_slicing", [](py::list a) {
325 return a[py::slice(0, -1, 2)];
329 m.def(
"issue2361_str_implicit_copy_none", []() {
330 py::str is_this_none = py::none();
333 m.def(
"issue2361_dict_implicit_copy_none", []() {
334 py::dict is_this_none = py::none();
338 m.def(
"test_memoryview_object", [](py::buffer b) {
339 return py::memoryview(b);
342 m.def(
"test_memoryview_buffer_info", [](py::buffer b) {
343 return py::memoryview(b.request());
346 m.def(
"test_memoryview_from_buffer", [](
bool is_unsigned) {
347 static const int16_t si16[] = { 3, 1, 4, 1, 5 };
348 static const uint16_t ui16[] = { 2, 7, 1, 8 };
350 return py::memoryview::from_buffer(
353 return py::memoryview::from_buffer(
354 si16, { 5 }, {
sizeof(
int16_t) });
357 m.def(
"test_memoryview_from_buffer_nativeformat", []() {
358 static const char* format =
"@i";
360 return py::memoryview::from_buffer(
364 m.def(
"test_memoryview_from_buffer_empty_shape", []() {
365 static const char* buf =
"";
366 return py::memoryview::from_buffer(buf, 1,
"B", { }, { });
369 m.def(
"test_memoryview_from_buffer_invalid_strides", []() {
370 static const char* buf =
"\x02\x03\x04";
371 return py::memoryview::from_buffer(buf, 1,
"B", { 3 }, { });
374 m.def(
"test_memoryview_from_buffer_nullptr", []() {
375 return py::memoryview::from_buffer(
376 static_cast<void*>(
nullptr), 1,
"B", { }, { });
379 #if PY_MAJOR_VERSION >= 3 380 m.def(
"test_memoryview_from_memory", []() {
381 const char* buf =
"\xff\xe1\xab\x37";
382 return py::memoryview::from_memory(
383 buf, static_cast<ssize_t>(strlen(buf)));
void print(const Matrix &A, const string &s, ostream &stream)
TEST_SUBMODULE(pytypes, m)
Dummy type which is not exported anywhere – something to trigger a conversion error.
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.
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
void set(Container &c, Position position, const Value &value)
Annotation for function names.