You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistent default return_value_policy for positional and named arguments #1072
m.def("foo", [](py::function func) {
Bar a, b, x, y;
...
returnfunc(a, &b, "x"_a=x, "y"_a=&y);
}
In the code above, a and b are positional arguments and are converted to Python using return_value_policy::automatic_reference. On the other hand, the named arguments x and y use return_value_policy::automatic by default. Because of the inconsistency, &y will be converted as an owned pointer and it will later try to free memory on the stack (crash).
The ideal solution would be to apply automatic_reference to named arguments as well. However, this would be a breaking change so I'd like to propose it for v3.0. The break would only change the behavior for owned pointers, e.g. "name"_a=new Bar(...), which seems like it would pretty uncommon given that "name"_a=Bar(...) does the same thing.
The default automatic policy originally comes from the py::arg annotation for def with which named call arguments shares the syntax. So this change would also affect bindings of default arguments, but unless I'm overlooking something, switching to automatic_reference would similarly only affect the new T() cases.
It seems that this would be clearer, since changing the argument by adding a name really is likely to cause confusion. Would this be reasonable to apply to 2.3 along with #1043 ?
Activity
henryiii commentedon Sep 11, 2017
It seems that this would be clearer, since changing the argument by adding a name really is likely to cause confusion. Would this be reasonable to apply to 2.3 along with #1043 ?