• Jason Rhinelander's avatar
    Add support for non-converting arguments · abc29cad
    Jason Rhinelander authored
    This adds support for controlling the `convert` flag of arguments
    through the py::arg annotation.  This then allows arguments to be
    flagged as non-converting, which the type_caster is able to use to
    request different behaviour.
    
    Currently, AFAICS `convert` is only used for type converters of regular
    pybind11-registered types; all of the other core type_casters ignore it.
    We can, however, repurpose it to control internal conversion of
    converters like Eigen and `array`: most usefully to give callers a way
    to disable the conversion that would otherwise occur when a
    `Eigen::Ref<const Eigen::Matrix>` argument is passed a numpy array that
    requires conversion (either because it has an incompatible stride or the
    wrong dtype).
    
    Specifying a noconvert looks like one of these:
    
        m.def("f1", &f, "a"_a.noconvert() = "default"); // Named, default, noconvert
        m.def("f2", &f, "a"_a.noconvert()); // Named, no default, no converting
        m.def("f3", &f, py::arg().noconvert()); // Unnamed, no default, no converting
    
    (The last part--being able to declare a py::arg without a name--is new:
    previous py::arg() only accepted named keyword arguments).
    
    Such an non-convert argument is then passed `convert = false` by the
    type caster when loading the argument.  Whether this has an effect is up
    to the type caster itself, but as mentioned above, this would be
    extremely helpful for the Eigen support to give a nicer way to specify
    a "no-copy" mode than the custom wrapper in the current PR, and
    moreover isn't an Eigen-specific hack.
    abc29cad
test_methods_and_attributes.cpp 11.2 KB