@@ -27,6 +27,26 @@ class PybindWrapper:
2727 Class to generate binding code for Pybind11 specifically.
2828 """
2929
30+ ARG_POLICY_SUPPORT = """
31+ #include <type_traits>
32+
33+ namespace gtwrap {
34+ namespace internal {
35+
36+ template <typename T>
37+ struct PyArgPolicy {
38+ static pybind11::arg make(const char* name) { return pybind11::arg(name); }
39+ };
40+
41+ template <typename T>
42+ pybind11::arg py_arg(const char* name) {
43+ return PyArgPolicy<typename std::decay<T>::type>::make(name);
44+ }
45+
46+ } // namespace internal
47+ } // namespace gtwrap
48+ """
49+
3050 def __init__ (self ,
3151 module_name ,
3252 top_module_namespaces = '' ,
@@ -70,8 +90,11 @@ def _py_args_names(self, args):
7090 default = ' = {arg.default}' .format (arg = arg )
7191 else :
7292 default = ''
73- argument = 'py::arg("{name}"){default}' .format (
74- name = arg .name , default = '{0}' .format (default ))
93+ argument = (
94+ 'gtwrap::internal::py_arg<{ctype}>("{name}"){default}'
95+ ).format (ctype = arg .ctype .to_cpp (),
96+ name = arg .name ,
97+ default = '{0}' .format (default ))
7598 py_args .append (argument )
7699 return ", " + ", " .join (py_args )
77100 else :
@@ -251,12 +274,13 @@ def _wrap_method(self,
251274 method ,
252275 (parser .StaticMethod , instantiator .InstantiatedStaticMethod ))
253276 return_void = method .return_type .is_void ()
254- return_ref = getattr (
255- getattr (method .return_type , 'type1' , None ), 'is_ref' , False )
277+ return_type = getattr (method .return_type , 'type1' , None )
278+ return_ref = getattr (return_type , 'is_ref' , False )
279+ return_const = getattr (return_type , 'is_const' , False )
256280
257281 # For methods returning const T&, use reference_internal policy
258282 # to avoid unnecessary copies and keep the returned reference alive.
259- if return_ref and is_method :
283+ if return_ref and return_const and is_method :
260284 lambda_ret = ' -> const auto&'
261285 ref_policy = ', py::return_value_policy::reference_internal'
262286 else :
@@ -752,6 +776,8 @@ def wrap_file(self, content, module_name=None, submodules=None):
752776 module_def = "void {0}(py::module_ &m_)" .format (module_name )
753777 submodules = []
754778
779+ includes += self .ARG_POLICY_SUPPORT
780+
755781 return self .module_template .format (
756782 module_def = module_def ,
757783 module_name = module_name ,
0 commit comments