allow passing a 'return value policy' to handle::operator()
diff --git a/docs/advanced.rst b/docs/advanced.rst
index 6138af8..f5b48d4 100644
--- a/docs/advanced.rst
+++ b/docs/advanced.rst
@@ -461,7 +461,9 @@
| | See below for a description of what all of these different policies do. |
+--------------------------------------------------+----------------------------------------------------------------------------+
| :enum:`return_value_policy::automatic_reference` | As above, but use policy :enum:`return_value_policy::reference` when the |
-| | return value is a pointer. You probably won't need to use this. |
+| | return value is a pointer. This is the default conversion policy for |
+| | function arguments when calling Python functions manually from C++ code |
+| | (i.e. via handle::operator()). You probably won't need to use this. |
+--------------------------------------------------+----------------------------------------------------------------------------+
| :enum:`return_value_policy::take_ownership` | Reference an existing object (i.e. do not create a new copy) and take |
| | ownership. Python will call the destructor and delete operator when the |
diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h
index 35726bf..a4e76aa 100644
--- a/include/pybind11/cast.h
+++ b/include/pybind11/cast.h
@@ -842,16 +842,18 @@
return result;
}
-template <typename... Args> object handle::operator()(Args&&... args) const {
- tuple args_tuple = pybind11::make_tuple(std::forward<Args>(args)...);
+template <return_value_policy policy,
+ typename... Args> object handle::operator()(Args&&... args) const {
+ tuple args_tuple = pybind11::make_tuple<policy>(std::forward<Args>(args)...);
object result(PyObject_CallObject(m_ptr, args_tuple.ptr()), false);
if (!result)
throw error_already_set();
return result;
}
-template <typename... Args> object handle::call(Args &&... args) const {
- return operator()(std::forward<Args>(args)...);
+template <return_value_policy policy,
+ typename... Args> object handle::call(Args &&... args) const {
+ return operator()<policy>(std::forward<Args>(args)...);
}
inline object handle::operator()(detail::args_proxy args) const {
diff --git a/include/pybind11/common.h b/include/pybind11/common.h
index daafa7d..3075fb3 100644
--- a/include/pybind11/common.h
+++ b/include/pybind11/common.h
@@ -151,7 +151,9 @@
automatic = 0,
/** As above, but use policy return_value_policy::reference when the return
- value is a pointer. You probably won't need to use this. */
+ value is a pointer. This is the default conversion policy for function
+ arguments when calling Python functions manually from C++ code (i.e. via
+ handle::operator()). You probably won't need to use this. */
automatic_reference,
/** Reference an existing object (i.e. do not create a new copy) and take
diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h
index 207e57d..6eee186 100644
--- a/include/pybind11/pytypes.h
+++ b/include/pybind11/pytypes.h
@@ -39,12 +39,13 @@
inline detail::accessor attr(const char *key) const;
inline pybind11::str str() const;
template <typename T> T cast() const;
- template <typename ... Args>
+ template <return_value_policy policy = return_value_policy::automatic_reference, typename ... Args>
#if __cplusplus > 201103L
[[deprecated("call(...) was deprecated in favor of operator()(...)")]]
#endif
object call(Args&&... args) const;
- template <typename ... Args> object operator()(Args&&... args) const;
+ template <return_value_policy policy = return_value_policy::automatic_reference, typename ... Args>
+ object operator()(Args&&... args) const;
inline object operator()(detail::args_proxy args) const;
inline object operator()(detail::args_proxy f_args, detail::kwargs_proxy kwargs) const;
operator bool() const { return m_ptr != nullptr; }