test_kwargs_and_defaults.cpp
Go to the documentation of this file.
1 /*
2  tests/test_kwargs_and_defaults.cpp -- keyword arguments and default values
3 
4  Copyright (c) 2016 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/stl.h>
11 
12 #include "constructor_stats.h"
13 #include "pybind11_tests.h"
14 
15 #include <utility>
16 
17 TEST_SUBMODULE(kwargs_and_defaults, m) {
18  auto kw_func
19  = [](int x, int y) { return "x=" + std::to_string(x) + ", y=" + std::to_string(y); };
20 
21  // test_named_arguments
22  m.def("kw_func0", kw_func);
23  m.def("kw_func1", kw_func, py::arg("x"), py::arg("y"));
24  m.def("kw_func2", kw_func, py::arg("x") = 100, py::arg("y") = 200);
25  m.def(
26  "kw_func3", [](const char *) {}, py::arg("data") = std::string("Hello world!"));
27 
28  /* A fancier default argument */
29  std::vector<int> list{{13, 17}};
30  m.def(
31  "kw_func4",
32  [](const std::vector<int> &entries) {
33  std::string ret = "{";
34  for (int i : entries) {
35  ret += std::to_string(i) + " ";
36  }
37  ret.back() = '}';
38  return ret;
39  },
40  py::arg("myList") = list);
41 
42  m.def("kw_func_udl", kw_func, "x"_a, "y"_a = 300);
43  m.def("kw_func_udl_z", kw_func, "x"_a, "y"_a = 0);
44 
45  // test_args_and_kwargs
46  m.def("args_function", [](py::args args) -> py::tuple { return std::move(args); });
47  m.def("args_kwargs_function", [](const py::args &args, const py::kwargs &kwargs) {
48  return py::make_tuple(args, kwargs);
49  });
50 
51  // test_mixed_args_and_kwargs
52  m.def("mixed_plus_args",
53  [](int i, double j, const py::args &args) { return py::make_tuple(i, j, args); });
54  m.def("mixed_plus_kwargs",
55  [](int i, double j, const py::kwargs &kwargs) { return py::make_tuple(i, j, kwargs); });
56  auto mixed_plus_both = [](int i, double j, const py::args &args, const py::kwargs &kwargs) {
57  return py::make_tuple(i, j, args, kwargs);
58  };
59  m.def("mixed_plus_args_kwargs", mixed_plus_both);
60 
61  m.def("mixed_plus_args_kwargs_defaults",
62  mixed_plus_both,
63  py::arg("i") = 1,
64  py::arg("j") = 3.14159);
65 
66  m.def(
67  "args_kwonly",
68  [](int i, double j, const py::args &args, int z) { return py::make_tuple(i, j, args, z); },
69  "i"_a,
70  "j"_a,
71  "z"_a);
72  m.def(
73  "args_kwonly_kwargs",
74  [](int i, double j, const py::args &args, int z, const py::kwargs &kwargs) {
75  return py::make_tuple(i, j, args, z, kwargs);
76  },
77  "i"_a,
78  "j"_a,
79  py::kw_only{},
80  "z"_a);
81  m.def(
82  "args_kwonly_kwargs_defaults",
83  [](int i, double j, const py::args &args, int z, const py::kwargs &kwargs) {
84  return py::make_tuple(i, j, args, z, kwargs);
85  },
86  "i"_a = 1,
87  "j"_a = 3.14159,
88  "z"_a = 42);
89  m.def(
90  "args_kwonly_full_monty",
91  [](int h, int i, double j, const py::args &args, int z, const py::kwargs &kwargs) {
92  return py::make_tuple(h, i, j, args, z, kwargs);
93  },
94  py::arg() = 1,
95  py::arg() = 2,
96  py::pos_only{},
97  "j"_a = 3.14159,
98  "z"_a = 42);
99 
100 // test_args_refcount
101 // PyPy needs a garbage collection to get the reference count values to match CPython's behaviour
102 #ifdef PYPY_VERSION
103 # define GC_IF_NEEDED ConstructorStats::gc()
104 #else
105 # define GC_IF_NEEDED
106 #endif
107  m.def("arg_refcount_h", [](py::handle h) {
108  GC_IF_NEEDED;
109  return h.ref_count();
110  });
111  m.def("arg_refcount_h", [](py::handle h, py::handle, py::handle) {
112  GC_IF_NEEDED;
113  return h.ref_count();
114  });
115  m.def("arg_refcount_o", [](const py::object &o) {
116  GC_IF_NEEDED;
117  return o.ref_count();
118  });
119  m.def("args_refcount", [](py::args a) {
120  GC_IF_NEEDED;
121  py::tuple t(a.size());
122  for (size_t i = 0; i < a.size(); i++) {
123  // Use raw Python API here to avoid an extra, intermediate incref on the tuple item:
124  t[i] = (int) Py_REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast<py::ssize_t>(i)));
125  }
126  return t;
127  });
128  m.def("mixed_args_refcount", [](const py::object &o, py::args a) {
129  GC_IF_NEEDED;
130  py::tuple t(a.size() + 1);
131  t[0] = o.ref_count();
132  for (size_t i = 0; i < a.size(); i++) {
133  // Use raw Python API here to avoid an extra, intermediate incref on the tuple item:
134  t[i + 1] = (int) Py_REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast<py::ssize_t>(i)));
135  }
136  return t;
137  });
138 
139  // pybind11 won't allow these to be bound: args and kwargs, if present, must be at the end.
140  // Uncomment these to test that the static_assert is indeed working:
141  // m.def("bad_args1", [](py::args, int) {});
142  // m.def("bad_args2", [](py::kwargs, int) {});
143  // m.def("bad_args3", [](py::kwargs, py::args) {});
144  // m.def("bad_args4", [](py::args, int, py::kwargs) {});
145  // m.def("bad_args5", [](py::args, py::kwargs, int) {});
146  // m.def("bad_args6", [](py::args, py::args) {});
147  // m.def("bad_args7", [](py::kwargs, py::kwargs) {});
148 
149  // test_keyword_only_args
150  m.def(
151  "kw_only_all",
152  [](int i, int j) { return py::make_tuple(i, j); },
153  py::kw_only(),
154  py::arg("i"),
155  py::arg("j"));
156  m.def(
157  "kw_only_some",
158  [](int i, int j, int k) { return py::make_tuple(i, j, k); },
159  py::arg(),
160  py::kw_only(),
161  py::arg("j"),
162  py::arg("k"));
163  m.def(
164  "kw_only_with_defaults",
165  [](int i, int j, int k, int z) { return py::make_tuple(i, j, k, z); },
166  py::arg() = 3,
167  "j"_a = 4,
168  py::kw_only(),
169  "k"_a = 5,
170  "z"_a);
171  m.def(
172  "kw_only_mixed",
173  [](int i, int j) { return py::make_tuple(i, j); },
174  "i"_a,
175  py::kw_only(),
176  "j"_a);
177  m.def(
178  "kw_only_plus_more",
179  [](int i, int j, int k, const py::kwargs &kwargs) {
180  return py::make_tuple(i, j, k, kwargs);
181  },
182  py::arg() /* positional */,
183  py::arg("j") = -1 /* both */,
184  py::kw_only(),
185  py::arg("k") /* kw-only */);
186 
187  m.def("register_invalid_kw_only", [](py::module_ m) {
188  m.def(
189  "bad_kw_only",
190  [](int i, int j) { return py::make_tuple(i, j); },
191  py::kw_only(),
192  py::arg() /* invalid unnamed argument */,
193  "j"_a);
194  });
195 
196  // test_positional_only_args
197  m.def(
198  "pos_only_all",
199  [](int i, int j) { return py::make_tuple(i, j); },
200  py::arg("i"),
201  py::arg("j"),
202  py::pos_only());
203  m.def(
204  "pos_only_mix",
205  [](int i, int j) { return py::make_tuple(i, j); },
206  py::arg("i"),
207  py::pos_only(),
208  py::arg("j"));
209  m.def(
210  "pos_kw_only_mix",
211  [](int i, int j, int k) { return py::make_tuple(i, j, k); },
212  py::arg("i"),
213  py::pos_only(),
214  py::arg("j"),
215  py::kw_only(),
216  py::arg("k"));
217  m.def(
218  "pos_only_def_mix",
219  [](int i, int j, int k) { return py::make_tuple(i, j, k); },
220  py::arg("i"),
221  py::arg("j") = 2,
222  py::pos_only(),
223  py::arg("k") = 3);
224 
225  // These should fail to compile:
226 #ifdef PYBIND11_NEVER_DEFINED_EVER
227  // argument annotations are required when using kw_only
228  m.def(
229  "bad_kw_only1", [](int) {}, py::kw_only());
230  // can't specify both `py::kw_only` and a `py::args` argument
231  m.def(
232  "bad_kw_only2", [](int i, py::args) {}, py::kw_only(), "i"_a);
233 #endif
234 
235  // test_function_signatures (along with most of the above)
236  struct KWClass {
237  void foo(int, float) {}
238  };
239  py::class_<KWClass>(m, "KWClass")
240  .def("foo0", &KWClass::foo)
241  .def("foo1", &KWClass::foo, "x"_a, "y"_a);
242 
243  // Make sure a class (not an instance) can be used as a default argument.
244  // The return value doesn't matter, only that the module is importable.
245  m.def(
246  "class_default_argument",
247  [](py::object a) { return py::repr(std::move(a)); },
248  "a"_a = py::module_::import("decimal").attr("Decimal"));
249 
250  // Initial implementation of kw_only was broken when used on a method/constructor before any
251  // other arguments
252  // https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987
253 
254  struct first_arg_kw_only {};
255  py::class_<first_arg_kw_only>(m, "first_arg_kw_only")
256  .def(py::init([](int) { return first_arg_kw_only(); }),
257  py::kw_only(), // This being before any args was broken
258  py::arg("i") = 0)
259  .def(
260  "method",
261  [](first_arg_kw_only &, int, int) {},
262  py::kw_only(), // and likewise here
263  py::arg("i") = 1,
264  py::arg("j") = 2)
265  // Closely related: pos_only marker didn't show up properly when it was before any other
266  // arguments (although that is fairly useless in practice).
267  .def(
268  "pos_only",
269  [](first_arg_kw_only &, int, int) {},
270  py::pos_only{},
271  py::arg("i"),
272  py::arg("j"));
273 }
Matrix3f m
Scalar * y
tuple make_tuple()
Definition: cast.h:1209
Definition: pytypes.h:2012
#define GC_IF_NEEDED
void foo(CV_QUALIFIER Matrix3d &m)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
TEST_SUBMODULE(kwargs_and_defaults, m)
DenseIndex ret
const double h
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1882
str repr(handle h)
Definition: pytypes.h:2265
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
std::ptrdiff_t j
Point2 t(10, 10)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:46