Skip to content

Inconsistent default return_value_policy for positional and named arguments #1072

Open

Description

@dean0x7d
m.def("foo", [](py::function func) {
    Bar a, b, x, y;
    ...
    return func(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.

Activity

  1. henryiii commented on Sep 11, 2017

    @henryiii
    Collaborator

    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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions