1 /*
2 tests/test_pytypes.cpp -- Python type casters
3
4 Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8 */
9
10 #include "pybind11_tests.h"
11
12
TEST_SUBMODULE(pytypes,m)13 TEST_SUBMODULE(pytypes, m) {
14 // test_int
15 m.def("get_int", []{return py::int_(0);});
16 // test_iterator
17 m.def("get_iterator", []{return py::iterator();});
18 // test_iterable
19 m.def("get_iterable", []{return py::iterable();});
20 // test_list
21 m.def("get_list", []() {
22 py::list list;
23 list.append("value");
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");
28 return list;
29 });
30 m.def("print_list", [](py::list list) {
31 int index = 0;
32 for (auto item : list)
33 py::print("list item {}: {}"_s.format(index++, item));
34 });
35 // test_none
36 m.def("get_none", []{return py::none();});
37 m.def("print_none", [](py::none none) {
38 py::print("none: {}"_s.format(none));
39 });
40
41 // test_set
42 m.def("get_set", []() {
43 py::set set;
44 set.add(py::str("key1"));
45 set.add("key2");
46 set.add(std::string("key3"));
47 return set;
48 });
49 m.def("print_set", [](py::set set) {
50 for (auto item : set)
51 py::print("key:", item);
52 });
53 m.def("set_contains", [](py::set set, py::object key) {
54 return set.contains(key);
55 });
56 m.def("set_contains", [](py::set set, const char* key) {
57 return set.contains(key);
58 });
59
60 // test_dict
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));
65 });
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);
69 return d2;
70 });
71 m.def("dict_contains", [](py::dict dict, py::object val) {
72 return dict.contains(val);
73 });
74 m.def("dict_contains", [](py::dict dict, const char* val) {
75 return dict.contains(val);
76 });
77
78 // test_str
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); });
84
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);
88 return py::make_tuple(s1, s2);
89 });
90
91 // test_bytes
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)); });
94
95 // test_capsule
96 m.def("return_capsule_with_destructor", []() {
97 py::print("creating capsule");
98 return py::capsule([]() {
99 py::print("destructing capsule");
100 });
101 });
102
103 m.def("return_capsule_with_destructor_2", []() {
104 py::print("creating capsule");
105 return py::capsule((void *) 1234, [](void *ptr) {
106 py::print("destructing capsule: {}"_s.format((size_t) ptr));
107 });
108 });
109
110 m.def("return_capsule_with_name_and_destructor", []() {
111 auto capsule = py::capsule((void *) 12345, "pointer type description", [](PyObject *ptr) {
112 if (ptr) {
113 auto name = PyCapsule_GetName(ptr);
114 py::print("destructing capsule ({}, '{}')"_s.format(
115 (size_t) PyCapsule_GetPointer(ptr, name), name
116 ));
117 }
118 });
119
120 capsule.set_pointer((void *) 1234);
121
122 // Using get_pointer<T>()
123 void* contents1 = static_cast<void*>(capsule);
124 void* contents2 = capsule.get_pointer();
125 void* contents3 = capsule.get_pointer<void>();
126
127 auto result1 = reinterpret_cast<size_t>(contents1);
128 auto result2 = reinterpret_cast<size_t>(contents2);
129 auto result3 = reinterpret_cast<size_t>(contents3);
130
131 py::print("created capsule ({}, '{}')"_s.format(result1 & result2 & result3, capsule.name()));
132 return capsule;
133 });
134
135 // test_accessors
136 m.def("accessor_api", [](py::object o) {
137 auto d = py::dict();
138
139 d["basic_attr"] = o.attr("basic_attr");
140
141 auto l = py::list();
142 for (auto item : o.attr("begin_end")) {
143 l.append(item);
144 }
145 d["begin_end"] = l;
146
147 d["operator[object]"] = o.attr("d")["operator[object]"_s];
148 d["operator[char *]"] = o.attr("d")["operator[char *]"];
149
150 d["attr(object)"] = o.attr("sub").attr("attr_obj");
151 d["attr(char *)"] = o.attr("sub").attr("attr_char");
152 try {
153 o.attr("sub").attr("missing").ptr();
154 } catch (const py::error_already_set &) {
155 d["missing_attr_ptr"] = "raised"_s;
156 }
157 try {
158 o.attr("missing").attr("doesn't matter");
159 } catch (const py::error_already_set &) {
160 d["missing_attr_chain"] = "raised"_s;
161 }
162
163 d["is_none"] = o.attr("basic_attr").is_none();
164
165 d["operator()"] = o.attr("func")(1);
166 d["operator*"] = o.attr("func")(*o.attr("begin_end"));
167
168 // Test implicit conversion
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;
173
174 return d;
175 });
176
177 m.def("tuple_accessor", [](py::tuple existing_t) {
178 try {
179 existing_t[0] = 1;
180 } catch (const py::error_already_set &) {
181 // --> Python system error
182 // Only new tuples (refcount == 1) are mutable
183 auto new_t = py::tuple(3);
184 for (size_t i = 0; i < new_t.size(); ++i) {
185 new_t[i] = i;
186 }
187 return new_t;
188 }
189 return py::tuple();
190 });
191
192 m.def("accessor_assignment", []() {
193 auto l = py::list(1);
194 l[0] = 0;
195
196 auto d = py::dict();
197 d["get"] = l[0];
198 auto var = l[0];
199 d["deferred_get"] = var;
200 l[0] = 1;
201 d["set"] = l[0];
202 var = 99; // this assignment should not overwrite l[0]
203 d["deferred_set"] = l[0];
204 d["var"] = var;
205
206 return d;
207 });
208
209 // test_constructors
210 m.def("default_constructors", []() {
211 return py::dict(
212 "bytes"_a=py::bytes(),
213 "str"_a=py::str(),
214 "bool"_a=py::bool_(),
215 "int"_a=py::int_(),
216 "float"_a=py::float_(),
217 "tuple"_a=py::tuple(),
218 "list"_a=py::list(),
219 "dict"_a=py::dict(),
220 "set"_a=py::set()
221 );
222 });
223
224 m.def("converting_constructors", [](py::dict d) {
225 return py::dict(
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"])
236 );
237 });
238
239 m.def("cast_functions", [](py::dict d) {
240 // When converting between Python types, obj.cast<T>() should be the same as T(obj)
241 return py::dict(
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>()
252 );
253 });
254
255 m.def("convert_to_pybind11_str", [](py::object o) { return py::str(o); });
256
257 m.def("nonconverting_constructor", [](std::string type, py::object value, bool move) -> py::object {
258 if (type == "bytes") {
259 return move ? py::bytes(std::move(value)) : py::bytes(value);
260 }
261 else if (type == "none") {
262 return move ? py::none(std::move(value)) : py::none(value);
263 }
264 else if (type == "ellipsis") {
265 return move ? py::ellipsis(std::move(value)) : py::ellipsis(value);
266 }
267 else if (type == "type") {
268 return move ? py::type(std::move(value)) : py::type(value);
269 }
270 throw std::runtime_error("Invalid type");
271 });
272
273 m.def("get_implicit_casting", []() {
274 py::dict d;
275 d["char*_i1"] = "abc";
276 const char *c2 = "abc";
277 d["char*_i2"] = c2;
278 d["char*_e"] = py::cast(c2);
279 d["char*_p"] = py::str(c2);
280
281 d["int_i1"] = 42;
282 int i = 42;
283 d["int_i2"] = i;
284 i++;
285 d["int_e"] = py::cast(i);
286 i++;
287 d["int_p"] = py::int_(i);
288
289 d["str_i1"] = std::string("str");
290 std::string s2("str1");
291 d["str_i2"] = s2;
292 s2[3] = '2';
293 d["str_e"] = py::cast(s2);
294 s2[3] = '3';
295 d["str_p"] = py::str(s2);
296
297 py::list l(2);
298 l[0] = 3;
299 l[1] = py::cast(6);
300 l.append(9);
301 l.append(py::cast(12));
302 l.append(py::int_(15));
303
304 return py::dict(
305 "d"_a=d,
306 "l"_a=l
307 );
308 });
309
310 // test_print
311 m.def("print_function", []() {
312 py::print("Hello, World!");
313 py::print(1, 2.0, "three", true, std::string("-- multiple args"));
314 auto args = py::make_tuple("and", "a", "custom", "separator");
315 py::print("*args", *args, "sep"_a="-");
316 py::print("no new line here", "end"_a=" -- ");
317 py::print("next print");
318
319 auto py_stderr = py::module_::import("sys").attr("stderr");
320 py::print("this goes to stderr", "file"_a=py_stderr);
321
322 py::print("flush", "flush"_a=true);
323
324 py::print("{a} + {b} = {c}"_s.format("a"_a="py::print", "b"_a="str.format", "c"_a="this"));
325 });
326
327 m.def("print_failure", []() { py::print(42, UnregisteredType()); });
328
329 m.def("hash_function", [](py::object obj) { return py::hash(obj); });
330
331 m.def("test_number_protocol", [](py::object a, py::object b) {
332 py::list l;
333 l.append(a.equal(b));
334 l.append(a.not_equal(b));
335 l.append(a < b);
336 l.append(a <= b);
337 l.append(a > b);
338 l.append(a >= b);
339 l.append(a + b);
340 l.append(a - b);
341 l.append(a * b);
342 l.append(a / b);
343 l.append(a | b);
344 l.append(a & b);
345 l.append(a ^ b);
346 l.append(a >> b);
347 l.append(a << b);
348 return l;
349 });
350
351 m.def("test_list_slicing", [](py::list a) {
352 return a[py::slice(0, -1, 2)];
353 });
354
355 // See #2361
356 m.def("issue2361_str_implicit_copy_none", []() {
357 py::str is_this_none = py::none();
358 return is_this_none;
359 });
360 m.def("issue2361_dict_implicit_copy_none", []() {
361 py::dict is_this_none = py::none();
362 return is_this_none;
363 });
364
365 m.def("test_memoryview_object", [](py::buffer b) {
366 return py::memoryview(b);
367 });
368
369 m.def("test_memoryview_buffer_info", [](py::buffer b) {
370 return py::memoryview(b.request());
371 });
372
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 };
376 if (is_unsigned)
377 return py::memoryview::from_buffer(
378 ui16, { 4 }, { sizeof(uint16_t) });
379 else
380 return py::memoryview::from_buffer(
381 si16, { 5 }, { sizeof(int16_t) });
382 });
383
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) });
389 });
390
391 m.def("test_memoryview_from_buffer_empty_shape", []() {
392 static const char* buf = "";
393 return py::memoryview::from_buffer(buf, 1, "B", { }, { });
394 });
395
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 }, { });
399 });
400
401 m.def("test_memoryview_from_buffer_nullptr", []() {
402 return py::memoryview::from_buffer(
403 static_cast<void*>(nullptr), 1, "B", { }, { });
404 });
405
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(
410 buf, static_cast<py::ssize_t>(strlen(buf)));
411 });
412 #endif
413
414 // test_builtin_functions
415 m.def("get_len", [](py::handle h) { return py::len(h); });
416 }
417