test_kwargs_and_defaults.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. tests/test_kwargs_and_defaults.cpp -- keyword arguments and default values
  3. Copyright (c) 2016 Wenzel Jakob <[email protected]>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. #include "constructor_stats.h"
  9. #include <pybind11/stl.h>
  10. TEST_SUBMODULE(kwargs_and_defaults, m) {
  11. auto kw_func = [](int x, int y) { return "x=" + std::to_string(x) + ", y=" + std::to_string(y); };
  12. // test_named_arguments
  13. m.def("kw_func0", kw_func);
  14. m.def("kw_func1", kw_func, py::arg("x"), py::arg("y"));
  15. m.def("kw_func2", kw_func, py::arg("x") = 100, py::arg("y") = 200);
  16. m.def("kw_func3", [](const char *) { }, py::arg("data") = std::string("Hello world!"));
  17. /* A fancier default argument */
  18. std::vector<int> list{{13, 17}};
  19. m.def("kw_func4", [](const std::vector<int> &entries) {
  20. std::string ret = "{";
  21. for (int i : entries)
  22. ret += std::to_string(i) + " ";
  23. ret.back() = '}';
  24. return ret;
  25. }, py::arg("myList") = list);
  26. m.def("kw_func_udl", kw_func, "x"_a, "y"_a=300);
  27. m.def("kw_func_udl_z", kw_func, "x"_a, "y"_a=0);
  28. // test_args_and_kwargs
  29. m.def("args_function", [](py::args args) -> py::tuple { return args; });
  30. m.def("args_kwargs_function", [](py::args args, py::kwargs kwargs) {
  31. return py::make_tuple(args, kwargs);
  32. });
  33. // test_mixed_args_and_kwargs
  34. m.def("mixed_plus_args", [](int i, double j, py::args args) {
  35. return py::make_tuple(i, j, args);
  36. });
  37. m.def("mixed_plus_kwargs", [](int i, double j, py::kwargs kwargs) {
  38. return py::make_tuple(i, j, kwargs);
  39. });
  40. auto mixed_plus_both = [](int i, double j, py::args args, py::kwargs kwargs) {
  41. return py::make_tuple(i, j, args, kwargs);
  42. };
  43. m.def("mixed_plus_args_kwargs", mixed_plus_both);
  44. m.def("mixed_plus_args_kwargs_defaults", mixed_plus_both,
  45. py::arg("i") = 1, py::arg("j") = 3.14159);
  46. // test_args_refcount
  47. // PyPy needs a garbage collection to get the reference count values to match CPython's behaviour
  48. #ifdef PYPY_VERSION
  49. #define GC_IF_NEEDED ConstructorStats::gc()
  50. #else
  51. #define GC_IF_NEEDED
  52. #endif
  53. m.def("arg_refcount_h", [](py::handle h) { GC_IF_NEEDED; return h.ref_count(); });
  54. m.def("arg_refcount_h", [](py::handle h, py::handle, py::handle) { GC_IF_NEEDED; return h.ref_count(); });
  55. m.def("arg_refcount_o", [](py::object o) { GC_IF_NEEDED; return o.ref_count(); });
  56. m.def("args_refcount", [](py::args a) {
  57. GC_IF_NEEDED;
  58. py::tuple t(a.size());
  59. for (size_t i = 0; i < a.size(); i++)
  60. // Use raw Python API here to avoid an extra, intermediate incref on the tuple item:
  61. t[i] = (int) Py_REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast<ssize_t>(i)));
  62. return t;
  63. });
  64. m.def("mixed_args_refcount", [](py::object o, py::args a) {
  65. GC_IF_NEEDED;
  66. py::tuple t(a.size() + 1);
  67. t[0] = o.ref_count();
  68. for (size_t i = 0; i < a.size(); i++)
  69. // Use raw Python API here to avoid an extra, intermediate incref on the tuple item:
  70. t[i + 1] = (int) Py_REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast<ssize_t>(i)));
  71. return t;
  72. });
  73. // pybind11 won't allow these to be bound: args and kwargs, if present, must be at the end.
  74. // Uncomment these to test that the static_assert is indeed working:
  75. // m.def("bad_args1", [](py::args, int) {});
  76. // m.def("bad_args2", [](py::kwargs, int) {});
  77. // m.def("bad_args3", [](py::kwargs, py::args) {});
  78. // m.def("bad_args4", [](py::args, int, py::kwargs) {});
  79. // m.def("bad_args5", [](py::args, py::kwargs, int) {});
  80. // m.def("bad_args6", [](py::args, py::args) {});
  81. // m.def("bad_args7", [](py::kwargs, py::kwargs) {});
  82. // test_function_signatures (along with most of the above)
  83. struct KWClass { void foo(int, float) {} };
  84. py::class_<KWClass>(m, "KWClass")
  85. .def("foo0", &KWClass::foo)
  86. .def("foo1", &KWClass::foo, "x"_a, "y"_a);
  87. }