diff --git a/example/example11.cpp b/example/example11.cpp index 1a472c9e8..5a91ee298 100644 --- a/example/example11.cpp +++ b/example/example11.cpp @@ -19,6 +19,13 @@ void kw_func4(const std::vector &entries) { std::cout << endl; } +void call_kw_func(py::function f) { + py::tuple args = py::make_tuple(1234); + py::dict kwargs; + kwargs["y"] = py::cast(5678); + f(*args, **kwargs); +} + void init_ex11(py::module &m) { m.def("kw_func", &kw_func, py::arg("x"), py::arg("y")); m.def("kw_func2", &kw_func, py::arg("x") = 100, py::arg("y") = 200); @@ -30,4 +37,5 @@ void init_ex11(py::module &m) { list.push_back(17); m.def("kw_func4", &kw_func4, py::arg("myList") = list); + m.def("call_kw_func", &call_kw_func); } diff --git a/example/example11.py b/example/example11.py index 04baa7bc9..948b0fd7c 100755 --- a/example/example11.py +++ b/example/example11.py @@ -5,7 +5,7 @@ import pydoc sys.path.append('.') -from example import kw_func, kw_func2, kw_func3, kw_func4 +from example import kw_func, kw_func2, kw_func3, kw_func4, call_kw_func print(pydoc.render_doc(kw_func, "Help on %s")) print(pydoc.render_doc(kw_func2, "Help on %s")) @@ -33,3 +33,5 @@ except Exception as e: kw_func4() kw_func4(myList = [1, 2, 3]) + +call_kw_func(kw_func2) diff --git a/example/example5.cpp b/example/example5.cpp index 4ba6a34f4..11d37d0ef 100644 --- a/example/example5.cpp +++ b/example/example5.cpp @@ -43,12 +43,12 @@ void dog_bark(const Dog &dog) { } bool test_callback1(py::object func) { - func.call(); + func(); return false; } int test_callback2(py::object func) { - py::object result = func.call("Hello", 'x', true, 5); + py::object result = func("Hello", 'x', true, 5); return result.cast(); } diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 5beb21ace..4ee79d204 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -796,7 +796,7 @@ template object handle::call(Args&&... args) const { +template object handle::operator()(Args&&... args) const { tuple args_tuple = pybind11::make_tuple(std::forward(args)...); object result(PyObject_CallObject(m_ptr, args_tuple.ptr()), false); if (!result) @@ -804,6 +804,24 @@ template object handle::call(Args&&... args) const { return result; } +template object handle::call(Args &&... args) const { + return operator()(std::forward(args)...); +} + +inline object handle::operator()(detail::args args) const { + object result(PyObject_CallObject(m_ptr, args.ptr()), false); + if (!result) + throw error_already_set(); + return result; +} + +inline object handle::operator()(detail::args args, detail::kwargs kwargs) const { + object result(PyObject_Call(m_ptr, args.ptr(), kwargs.ptr()), false); + if (!result) + throw error_already_set(); + return result; +} + #define PYBIND11_MAKE_OPAQUE(Type) \ namespace pybind11 { namespace detail { \ template<> class type_caster : public type_caster_base { }; \ diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index f2f098592..fa61a1188 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -145,7 +145,7 @@ struct type_caster::value>:: if (obj.get_type() != matrix_type.ptr()) { try { - obj = matrix_type.call(obj); + obj = matrix_type(obj); } catch (const error_already_set &) { PyErr_Clear(); return false; @@ -233,7 +233,7 @@ struct type_caster::value>:: { sizeof(StorageIndex) } )); - return matrix_type.call( + return matrix_type( std::make_tuple(data, innerIndices, outerIndices), std::make_pair(src.rows(), src.cols()) ).release(); diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index ae3e8b38a..f74a9bdf9 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -26,7 +26,7 @@ public: object src(src_, true); value = [src](Args... args) -> Return { gil_scoped_acquire acq; - object retval(src.call(std::move(args)...)); + object retval(src(std::move(args)...)); /* Visual studio 2015 parser issue: need parentheses around this expression */ return (retval.template cast()); }; diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index c9b40b4b0..efa2fa898 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -176,7 +176,7 @@ protected: if (a.descr) a.descr = strdup(a.descr); else if (a.value) - a.descr = strdup(((std::string) ((object) handle(a.value).attr("__repr__")).call().str()).c_str()); + a.descr = strdup(((std::string) ((object) handle(a.value).attr("__repr__"))().str()).c_str()); } auto const ®istered_types = detail::get_internals().registered_types_cpp; @@ -1202,7 +1202,7 @@ inline function get_overload(const void *this_ptr, const char *name) { pybind11::gil_scoped_acquire gil; \ pybind11::function overload = pybind11::get_overload(this, #name); \ if (overload) \ - return overload.call(__VA_ARGS__).template cast(); } + return overload(__VA_ARGS__).template cast(); } #define PYBIND11_OVERLOAD(ret_type, class_name, name, ...) \ PYBIND11_OVERLOAD_INT(ret_type, class_name, name, __VA_ARGS__) \ diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 82725ce47..99fb5034e 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -21,7 +21,7 @@ class str; class object; class dict; class iterator; -namespace detail { class accessor; } +namespace detail { class accessor; class args; class kwargs; } /// Holds a reference to a Python object (no reference counting) class handle { @@ -43,11 +43,17 @@ public: inline detail::accessor attr(const char *key) const; inline pybind11::str str() const; template T cast() const; - template object call(Args&&... args_) const; + template + [[deprecated("call(...) was deprecated in favor of operator()(...)")]] + object call(Args&&... args) const; + template object operator()(Args&&... args) const; + inline object operator()(detail::args args) const; + inline object operator()(detail::args args, detail::kwargs kwargs) const; operator bool() const { return m_ptr != nullptr; } bool operator==(const handle &h) const { return m_ptr == h.m_ptr; } bool operator!=(const handle &h) const { return m_ptr != h.m_ptr; } bool check() const { return m_ptr != nullptr; } + inline detail::args operator*() const; protected: PyObject *m_ptr; }; @@ -212,6 +218,17 @@ private: ssize_t pos = 0; }; +class kwargs : public handle { +public: + kwargs(handle h) : handle(h) { } +}; + +class args : public handle { +public: + args(handle h) : handle(h) { } + kwargs operator*() const { return kwargs(*this); } +}; + inline bool PyIterable_Check(PyObject *obj) { PyObject *iter = PyObject_GetIter(obj); if (iter) { @@ -315,6 +332,7 @@ inline detail::accessor handle::attr(handle key) const { return detail::accessor inline detail::accessor handle::attr(const char *key) const { return detail::accessor(ptr(), key, true); } inline iterator handle::begin() const { return iterator(PyObject_GetIter(ptr()), false); } inline iterator handle::end() const { return iterator(nullptr, false); } +inline detail::args handle::operator*() const { return detail::args(*this); } class str : public object { public: