μHAL (v2.8.0)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_pytypes.cpp
Go to the documentation of this file.
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 
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) -> py::object {
258  if (type == "bytes") {
259  return py::bytes(value);
260  }
261  else if (type == "none") {
262  return py::none(value);
263  }
264  else if (type == "ellipsis") {
265  return py::ellipsis(value);
266  }
267  throw std::runtime_error("Invalid type");
268  });
269 
270  m.def("get_implicit_casting", []() {
271  py::dict d;
272  d["char*_i1"] = "abc";
273  const char *c2 = "abc";
274  d["char*_i2"] = c2;
275  d["char*_e"] = py::cast(c2);
276  d["char*_p"] = py::str(c2);
277 
278  d["int_i1"] = 42;
279  int i = 42;
280  d["int_i2"] = i;
281  i++;
282  d["int_e"] = py::cast(i);
283  i++;
284  d["int_p"] = py::int_(i);
285 
286  d["str_i1"] = std::string("str");
287  std::string s2("str1");
288  d["str_i2"] = s2;
289  s2[3] = '2';
290  d["str_e"] = py::cast(s2);
291  s2[3] = '3';
292  d["str_p"] = py::str(s2);
293 
294  py::list l(2);
295  l[0] = 3;
296  l[1] = py::cast(6);
297  l.append(9);
298  l.append(py::cast(12));
299  l.append(py::int_(15));
300 
301  return py::dict(
302  "d"_a=d,
303  "l"_a=l
304  );
305  });
306 
307  // test_print
308  m.def("print_function", []() {
309  py::print("Hello, World!");
310  py::print(1, 2.0, "three", true, std::string("-- multiple args"));
311  auto args = py::make_tuple("and", "a", "custom", "separator");
312  py::print("*args", *args, "sep"_a="-");
313  py::print("no new line here", "end"_a=" -- ");
314  py::print("next print");
315 
316  auto py_stderr = py::module_::import("sys").attr("stderr");
317  py::print("this goes to stderr", "file"_a=py_stderr);
318 
319  py::print("flush", "flush"_a=true);
320 
321  py::print("{a} + {b} = {c}"_s.format("a"_a="py::print", "b"_a="str.format", "c"_a="this"));
322  });
323 
324  m.def("print_failure", []() { py::print(42, UnregisteredType()); });
325 
326  m.def("hash_function", [](py::object obj) { return py::hash(obj); });
327 
328  m.def("test_number_protocol", [](py::object a, py::object b) {
329  py::list l;
330  l.append(a.equal(b));
331  l.append(a.not_equal(b));
332  l.append(a < b);
333  l.append(a <= b);
334  l.append(a > 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  return l;
346  });
347 
348  m.def("test_list_slicing", [](py::list a) {
349  return a[py::slice(0, -1, 2)];
350  });
351 
352  // See #2361
353  m.def("issue2361_str_implicit_copy_none", []() {
354  py::str is_this_none = py::none();
355  return is_this_none;
356  });
357  m.def("issue2361_dict_implicit_copy_none", []() {
358  py::dict is_this_none = py::none();
359  return is_this_none;
360  });
361 
362  m.def("test_memoryview_object", [](py::buffer b) {
363  return py::memoryview(b);
364  });
365 
366  m.def("test_memoryview_buffer_info", [](py::buffer b) {
367  return py::memoryview(b.request());
368  });
369 
370  m.def("test_memoryview_from_buffer", [](bool is_unsigned) {
371  static const int16_t si16[] = { 3, 1, 4, 1, 5 };
372  static const uint16_t ui16[] = { 2, 7, 1, 8 };
373  if (is_unsigned)
374  return py::memoryview::from_buffer(
375  ui16, { 4 }, { sizeof(uint16_t) });
376  else
377  return py::memoryview::from_buffer(
378  si16, { 5 }, { sizeof(int16_t) });
379  });
380 
381  m.def("test_memoryview_from_buffer_nativeformat", []() {
382  static const char* format = "@i";
383  static const int32_t arr[] = { 4, 7, 5 };
384  return py::memoryview::from_buffer(
385  arr, sizeof(int32_t), format, { 3 }, { sizeof(int32_t) });
386  });
387 
388  m.def("test_memoryview_from_buffer_empty_shape", []() {
389  static const char* buf = "";
390  return py::memoryview::from_buffer(buf, 1, "B", { }, { });
391  });
392 
393  m.def("test_memoryview_from_buffer_invalid_strides", []() {
394  static const char* buf = "\x02\x03\x04";
395  return py::memoryview::from_buffer(buf, 1, "B", { 3 }, { });
396  });
397 
398  m.def("test_memoryview_from_buffer_nullptr", []() {
399  return py::memoryview::from_buffer(
400  static_cast<void*>(nullptr), 1, "B", { }, { });
401  });
402 
403 #if PY_MAJOR_VERSION >= 3
404  m.def("test_memoryview_from_memory", []() {
405  const char* buf = "\xff\xe1\xab\x37";
406  return py::memoryview::from_memory(
407  buf, static_cast<py::ssize_t>(strlen(buf)));
408  });
409 #endif
410 
411  // test_builtin_functions
412  m.def("get_len", [](py::handle h) { return py::len(h); });
413 }
test_multiple_inheritance.i
i
Definition: test_multiple_inheritance.py:22
name
Annotation for function names.
Definition: attr.h:36
cast
T cast(const handle &handle)
Definition: cast.h:1718
set
Definition: pytypes.h:1344
list
Definition: pytypes.h:1320
set::add
bool add(T &&val) const
Definition: pytypes.h:1352
capsule
Definition: pytypes.h:1195
type
Definition: pytypes.h:904
list::insert
void insert(size_t index, T &&val) const
Definition: pytypes.h:1335
uhal::print
void print(std::ostream &aStr, const tm *aTm, const uint32_t &aUsec)
Format a time element for for sending to the log.
hash
ssize_t hash(handle obj)
Definition: pytypes.h:457
dict
Definition: pytypes.h:1274
ssize_t
Py_ssize_t ssize_t
Definition: common.h:333
make_tuple
tuple make_tuple()
Definition: cast.h:1815
UnregisteredType
Dummy type which is not exported anywhere – something to trigger a conversion error.
Definition: pybind11_tests.h:27
set::contains
bool contains(T &&val) const
Definition: pytypes.h:1356
arr
py::array arr
Definition: test_numpy_array.cpp:78
list::append
void append(T &&val) const
Definition: pytypes.h:1332
dict::contains
bool contains(T &&key) const
Definition: pytypes.h:1291
pybind11_tests.h
args
Definition: pytypes.h:1341
TEST_SUBMODULE
TEST_SUBMODULE(pytypes, m)
Definition: test_pytypes.cpp:13
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:1535
none
Definition: pytypes.h:1064
test_async.m
m
Definition: test_async.py:5
test_callbacks.value
value
Definition: test_callbacks.py:126
repr
str repr(handle h)
Definition: pytypes.h:1559