test_kwargs_and_defaults.cpp revision 14299
12068SN/A/* 22068SN/A tests/test_kwargs_and_defaults.cpp -- keyword arguments and default values 32068SN/A 42068SN/A Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> 52068SN/A 62068SN/A All rights reserved. Use of this source code is governed by a 72068SN/A BSD-style license that can be found in the LICENSE file. 82068SN/A*/ 92068SN/A 102068SN/A#include "pybind11_tests.h" 112068SN/A#include "constructor_stats.h" 122068SN/A#include <pybind11/stl.h> 132068SN/A 142068SN/ATEST_SUBMODULE(kwargs_and_defaults, m) { 152068SN/A auto kw_func = [](int x, int y) { return "x=" + std::to_string(x) + ", y=" + std::to_string(y); }; 162068SN/A 172068SN/A // test_named_arguments 182068SN/A m.def("kw_func0", kw_func); 192068SN/A m.def("kw_func1", kw_func, py::arg("x"), py::arg("y")); 202068SN/A m.def("kw_func2", kw_func, py::arg("x") = 100, py::arg("y") = 200); 212068SN/A m.def("kw_func3", [](const char *) { }, py::arg("data") = std::string("Hello world!")); 222068SN/A 232068SN/A /* A fancier default argument */ 242068SN/A std::vector<int> list{{13, 17}}; 252068SN/A m.def("kw_func4", [](const std::vector<int> &entries) { 262068SN/A std::string ret = "{"; 272068SN/A for (int i : entries) 282665Ssaidi@eecs.umich.edu ret += std::to_string(i) + " "; 292665Ssaidi@eecs.umich.edu ret.back() = '}'; 302068SN/A return ret; 312649Ssaidi@eecs.umich.edu }, py::arg("myList") = list); 322649Ssaidi@eecs.umich.edu 332649Ssaidi@eecs.umich.edu m.def("kw_func_udl", kw_func, "x"_a, "y"_a=300); 342649Ssaidi@eecs.umich.edu m.def("kw_func_udl_z", kw_func, "x"_a, "y"_a=0); 357799Sgblack@eecs.umich.edu 367799Sgblack@eecs.umich.edu // test_args_and_kwargs 377799Sgblack@eecs.umich.edu m.def("args_function", [](py::args args) -> py::tuple { 382649Ssaidi@eecs.umich.edu return std::move(args); 392649Ssaidi@eecs.umich.edu }); 402068SN/A m.def("args_kwargs_function", [](py::args args, py::kwargs kwargs) { 412068SN/A return py::make_tuple(args, kwargs); 422068SN/A }); 432090SN/A 442090SN/A // test_mixed_args_and_kwargs 452068SN/A m.def("mixed_plus_args", [](int i, double j, py::args args) { 462132SN/A return py::make_tuple(i, j, args); 472068SN/A }); 487799Sgblack@eecs.umich.edu m.def("mixed_plus_kwargs", [](int i, double j, py::kwargs kwargs) { 495568Snate@binkert.org return py::make_tuple(i, j, kwargs); 502147SN/A }); 512068SN/A auto mixed_plus_both = [](int i, double j, py::args args, py::kwargs kwargs) { 522068SN/A return py::make_tuple(i, j, args, kwargs); 532068SN/A }; 542068SN/A m.def("mixed_plus_args_kwargs", mixed_plus_both); 552132SN/A 562068SN/A m.def("mixed_plus_args_kwargs_defaults", mixed_plus_both, 572090SN/A py::arg("i") = 1, py::arg("j") = 3.14159); 582068SN/A 592068SN/A // test_args_refcount 602068SN/A // PyPy needs a garbage collection to get the reference count values to match CPython's behaviour 612068SN/A #ifdef PYPY_VERSION 622068SN/A #define GC_IF_NEEDED ConstructorStats::gc() 632068SN/A #else 642068SN/A #define GC_IF_NEEDED 652068SN/A #endif 662068SN/A m.def("arg_refcount_h", [](py::handle h) { GC_IF_NEEDED; return h.ref_count(); }); 672068SN/A m.def("arg_refcount_h", [](py::handle h, py::handle, py::handle) { GC_IF_NEEDED; return h.ref_count(); }); 682068SN/A m.def("arg_refcount_o", [](py::object o) { GC_IF_NEEDED; return o.ref_count(); }); 692068SN/A m.def("args_refcount", [](py::args a) { 702068SN/A GC_IF_NEEDED; 712068SN/A py::tuple t(a.size()); 722068SN/A for (size_t i = 0; i < a.size(); i++) 732068SN/A // Use raw Python API here to avoid an extra, intermediate incref on the tuple item: 747799Sgblack@eecs.umich.edu t[i] = (int) Py_REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast<ssize_t>(i))); 752068SN/A return t; 767799Sgblack@eecs.umich.edu }); 777799Sgblack@eecs.umich.edu m.def("mixed_args_refcount", [](py::object o, py::args a) { 787799Sgblack@eecs.umich.edu GC_IF_NEEDED; 792068SN/A py::tuple t(a.size() + 1); 802068SN/A t[0] = o.ref_count(); 812068SN/A for (size_t i = 0; i < a.size(); i++) 822068SN/A // Use raw Python API here to avoid an extra, intermediate incref on the tuple item: 832068SN/A t[i + 1] = (int) Py_REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast<ssize_t>(i))); 842068SN/A return t; 852068SN/A }); 862068SN/A 877799Sgblack@eecs.umich.edu // pybind11 won't allow these to be bound: args and kwargs, if present, must be at the end. 882068SN/A // Uncomment these to test that the static_assert is indeed working: 897799Sgblack@eecs.umich.edu// m.def("bad_args1", [](py::args, int) {}); 907799Sgblack@eecs.umich.edu// m.def("bad_args2", [](py::kwargs, int) {}); 912068SN/A// m.def("bad_args3", [](py::kwargs, py::args) {}); 922068SN/A// m.def("bad_args4", [](py::args, int, py::kwargs) {}); 932068SN/A// m.def("bad_args5", [](py::args, py::kwargs, int) {}); 942068SN/A// m.def("bad_args6", [](py::args, py::args) {}); 952068SN/A// m.def("bad_args7", [](py::kwargs, py::kwargs) {}); 962068SN/A 972068SN/A // test_function_signatures (along with most of the above) 982068SN/A struct KWClass { void foo(int, float) {} }; 992068SN/A py::class_<KWClass>(m, "KWClass") 1002068SN/A .def("foo0", &KWClass::foo) 1012068SN/A .def("foo1", &KWClass::foo, "x"_a, "y"_a); 1022068SN/A} 1032068SN/A