You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.0 KiB

8 years ago
  1. /*
  2. tests/test_kwargs_and_defaults.cpp -- keyword arguments and default values
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  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 <pybind11/stl.h>
  9. std::string kw_func(int x, int y) { return "x=" + std::to_string(x) + ", y=" + std::to_string(y); }
  10. std::string kw_func4(const std::vector<int> &entries) {
  11. std::string ret = "{";
  12. for (int i : entries)
  13. ret += std::to_string(i) + " ";
  14. ret.back() = '}';
  15. return ret;
  16. }
  17. py::tuple args_function(py::args args) {
  18. return args;
  19. }
  20. py::tuple args_kwargs_function(py::args args, py::kwargs kwargs) {
  21. return py::make_tuple(args, kwargs);
  22. }
  23. py::tuple mixed_plus_args(int i, double j, py::args args) {
  24. return py::make_tuple(i, j, args);
  25. }
  26. py::tuple mixed_plus_kwargs(int i, double j, py::kwargs kwargs) {
  27. return py::make_tuple(i, j, kwargs);
  28. }
  29. py::tuple mixed_plus_args_kwargs(int i, double j, py::args args, py::kwargs kwargs) {
  30. return py::make_tuple(i, j, args, kwargs);
  31. }
  32. // pybind11 won't allow these to be bound: args and kwargs, if present, must be at the end.
  33. void bad_args1(py::args, int) {}
  34. void bad_args2(py::kwargs, int) {}
  35. void bad_args3(py::kwargs, py::args) {}
  36. void bad_args4(py::args, int, py::kwargs) {}
  37. void bad_args5(py::args, py::kwargs, int) {}
  38. void bad_args6(py::args, py::args) {}
  39. void bad_args7(py::kwargs, py::kwargs) {}
  40. struct KWClass {
  41. void foo(int, float) {}
  42. };
  43. test_initializer arg_keywords_and_defaults([](py::module &m) {
  44. m.def("kw_func0", &kw_func);
  45. m.def("kw_func1", &kw_func, py::arg("x"), py::arg("y"));
  46. m.def("kw_func2", &kw_func, py::arg("x") = 100, py::arg("y") = 200);
  47. m.def("kw_func3", [](const char *) { }, py::arg("data") = std::string("Hello world!"));
  48. /* A fancier default argument */
  49. std::vector<int> list;
  50. list.push_back(13);
  51. list.push_back(17);
  52. m.def("kw_func4", &kw_func4, py::arg("myList") = list);
  53. m.def("args_function", &args_function);
  54. m.def("args_kwargs_function", &args_kwargs_function);
  55. m.def("kw_func_udl", &kw_func, "x"_a, "y"_a=300);
  56. m.def("kw_func_udl_z", &kw_func, "x"_a, "y"_a=0);
  57. py::class_<KWClass>(m, "KWClass")
  58. .def("foo0", &KWClass::foo)
  59. .def("foo1", &KWClass::foo, "x"_a, "y"_a);
  60. m.def("mixed_plus_args", &mixed_plus_args);
  61. m.def("mixed_plus_kwargs", &mixed_plus_kwargs);
  62. m.def("mixed_plus_args_kwargs", &mixed_plus_args_kwargs);
  63. m.def("mixed_plus_args_kwargs_defaults", &mixed_plus_args_kwargs,
  64. py::arg("i") = 1, py::arg("j") = 3.14159);
  65. // Uncomment these to test that the static_assert is indeed working:
  66. // m.def("bad_args1", &bad_args1);
  67. // m.def("bad_args2", &bad_args2);
  68. // m.def("bad_args3", &bad_args3);
  69. // m.def("bad_args4", &bad_args4);
  70. // m.def("bad_args5", &bad_args5);
  71. // m.def("bad_args6", &bad_args6);
  72. // m.def("bad_args7", &bad_args7);
  73. });