Blame external/pybind11/tests/test_methods_and_attributes.cpp

Packit 534379
/*
Packit 534379
    tests/test_methods_and_attributes.cpp -- constructors, deconstructors, attribute access,
Packit 534379
    __str__, argument and return value conventions
Packit 534379
Packit 534379
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Packit 534379
Packit 534379
    All rights reserved. Use of this source code is governed by a
Packit 534379
    BSD-style license that can be found in the LICENSE file.
Packit 534379
*/
Packit 534379
Packit 534379
#include "pybind11_tests.h"
Packit 534379
#include "constructor_stats.h"
Packit 534379
Packit 534379
#if !defined(PYBIND11_OVERLOAD_CAST)
Packit 534379
template <typename... Args>
Packit 534379
using overload_cast_ = pybind11::detail::overload_cast_impl<Args...>;
Packit 534379
#endif
Packit 534379
Packit 534379
class ExampleMandA {
Packit 534379
public:
Packit 534379
    ExampleMandA() { print_default_created(this); }
Packit 534379
    ExampleMandA(int value) : value(value) { print_created(this, value); }
Packit 534379
    ExampleMandA(const ExampleMandA &e) : value(e.value) { print_copy_created(this); }
Packit 534379
    ExampleMandA(ExampleMandA &&e) : value(e.value) { print_move_created(this); }
Packit 534379
    ~ExampleMandA() { print_destroyed(this); }
Packit 534379
Packit 534379
    std::string toString() {
Packit 534379
        return "ExampleMandA[value=" + std::to_string(value) + "]";
Packit 534379
    }
Packit 534379
Packit 534379
    void operator=(const ExampleMandA &e) { print_copy_assigned(this); value = e.value; }
Packit 534379
    void operator=(ExampleMandA &&e) { print_move_assigned(this); value = e.value; }
Packit 534379
Packit 534379
    void add1(ExampleMandA other) { value += other.value; }         // passing by value
Packit 534379
    void add2(ExampleMandA &other) { value += other.value; }        // passing by reference
Packit 534379
    void add3(const ExampleMandA &other) { value += other.value; }  // passing by const reference
Packit 534379
    void add4(ExampleMandA *other) { value += other->value; }       // passing by pointer
Packit 534379
    void add5(const ExampleMandA *other) { value += other->value; } // passing by const pointer
Packit 534379
Packit 534379
    void add6(int other) { value += other; }                        // passing by value
Packit 534379
    void add7(int &other) { value += other; }                       // passing by reference
Packit 534379
    void add8(const int &other) { value += other; }                 // passing by const reference
Packit 534379
    void add9(int *other) { value += *other; }                      // passing by pointer
Packit 534379
    void add10(const int *other) { value += *other; }               // passing by const pointer
Packit 534379
Packit 534379
    ExampleMandA self1() { return *this; }                          // return by value
Packit 534379
    ExampleMandA &self2() { return *this; }                         // return by reference
Packit 534379
    const ExampleMandA &self3() { return *this; }                   // return by const reference
Packit 534379
    ExampleMandA *self4() { return this; }                          // return by pointer
Packit 534379
    const ExampleMandA *self5() { return this; }                    // return by const pointer
Packit 534379
Packit 534379
    int internal1() { return value; }                               // return by value
Packit 534379
    int &internal2() { return value; }                              // return by reference
Packit 534379
    const int &internal3() { return value; }                        // return by const reference
Packit 534379
    int *internal4() { return &value; }                             // return by pointer
Packit 534379
    const int *internal5() { return &value; }                       // return by const pointer
Packit 534379
Packit 534379
    py::str overloaded()             { return "()"; }
Packit 534379
    py::str overloaded(int)          { return "(int)"; }
Packit 534379
    py::str overloaded(int, float)   { return "(int, float)"; }
Packit 534379
    py::str overloaded(float, int)   { return "(float, int)"; }
Packit 534379
    py::str overloaded(int, int)     { return "(int, int)"; }
Packit 534379
    py::str overloaded(float, float) { return "(float, float)"; }
Packit 534379
    py::str overloaded(int)          const { return "(int) const"; }
Packit 534379
    py::str overloaded(int, float)   const { return "(int, float) const"; }
Packit 534379
    py::str overloaded(float, int)   const { return "(float, int) const"; }
Packit 534379
    py::str overloaded(int, int)     const { return "(int, int) const"; }
Packit 534379
    py::str overloaded(float, float) const { return "(float, float) const"; }
Packit 534379
Packit 534379
    static py::str overloaded(float) { return "static float"; }
Packit 534379
Packit 534379
    int value = 0;
Packit 534379
};
Packit 534379
Packit 534379
struct TestProperties {
Packit 534379
    int value = 1;
Packit 534379
    static int static_value;
Packit 534379
Packit 534379
    int get() const { return value; }
Packit 534379
    void set(int v) { value = v; }
Packit 534379
Packit 534379
    static int static_get() { return static_value; }
Packit 534379
    static void static_set(int v) { static_value = v; }
Packit 534379
};
Packit 534379
int TestProperties::static_value = 1;
Packit 534379
Packit 534379
struct TestPropertiesOverride : TestProperties {
Packit 534379
    int value = 99;
Packit 534379
    static int static_value;
Packit 534379
};
Packit 534379
int TestPropertiesOverride::static_value = 99;
Packit 534379
Packit 534379
struct TestPropRVP {
Packit 534379
    UserType v1{1};
Packit 534379
    UserType v2{1};
Packit 534379
    static UserType sv1;
Packit 534379
    static UserType sv2;
Packit 534379
Packit 534379
    const UserType &get1() const { return v1; }
Packit 534379
    const UserType &get2() const { return v2; }
Packit 534379
    UserType get_rvalue() const { return v2; }
Packit 534379
    void set1(int v) { v1.set(v); }
Packit 534379
    void set2(int v) { v2.set(v); }
Packit 534379
};
Packit 534379
UserType TestPropRVP::sv1(1);
Packit 534379
UserType TestPropRVP::sv2(1);
Packit 534379
Packit 534379
// py::arg/py::arg_v testing: these arguments just record their argument when invoked
Packit 534379
class ArgInspector1 { public: std::string arg = "(default arg inspector 1)"; };
Packit 534379
class ArgInspector2 { public: std::string arg = "(default arg inspector 2)"; };
Packit 534379
class ArgAlwaysConverts { };
Packit 534379
namespace pybind11 { namespace detail {
Packit 534379
template <> struct type_caster<ArgInspector1> {
Packit 534379
public:
Packit 534379
    PYBIND11_TYPE_CASTER(ArgInspector1, _("ArgInspector1"));
Packit 534379
Packit 534379
    bool load(handle src, bool convert) {
Packit 534379
        value.arg = "loading ArgInspector1 argument " +
Packit 534379
            std::string(convert ? "WITH" : "WITHOUT") + " conversion allowed.  "
Packit 534379
            "Argument value = " + (std::string) str(src);
Packit 534379
        return true;
Packit 534379
    }
Packit 534379
Packit 534379
    static handle cast(const ArgInspector1 &src, return_value_policy, handle) {
Packit 534379
        return str(src.arg).release();
Packit 534379
    }
Packit 534379
};
Packit 534379
template <> struct type_caster<ArgInspector2> {
Packit 534379
public:
Packit 534379
    PYBIND11_TYPE_CASTER(ArgInspector2, _("ArgInspector2"));
Packit 534379
Packit 534379
    bool load(handle src, bool convert) {
Packit 534379
        value.arg = "loading ArgInspector2 argument " +
Packit 534379
            std::string(convert ? "WITH" : "WITHOUT") + " conversion allowed.  "
Packit 534379
            "Argument value = " + (std::string) str(src);
Packit 534379
        return true;
Packit 534379
    }
Packit 534379
Packit 534379
    static handle cast(const ArgInspector2 &src, return_value_policy, handle) {
Packit 534379
        return str(src.arg).release();
Packit 534379
    }
Packit 534379
};
Packit 534379
template <> struct type_caster<ArgAlwaysConverts> {
Packit 534379
public:
Packit 534379
    PYBIND11_TYPE_CASTER(ArgAlwaysConverts, _("ArgAlwaysConverts"));
Packit 534379
Packit 534379
    bool load(handle, bool convert) {
Packit 534379
        return convert;
Packit 534379
    }
Packit 534379
Packit 534379
    static handle cast(const ArgAlwaysConverts &, return_value_policy, handle) {
Packit 534379
        return py::none().release();
Packit 534379
    }
Packit 534379
};
Packit 534379
}}
Packit 534379
Packit 534379
// test_custom_caster_destruction
Packit 534379
class DestructionTester {
Packit 534379
public:
Packit 534379
    DestructionTester() { print_default_created(this); }
Packit 534379
    ~DestructionTester() { print_destroyed(this); }
Packit 534379
    DestructionTester(const DestructionTester &) { print_copy_created(this); }
Packit 534379
    DestructionTester(DestructionTester &&) { print_move_created(this); }
Packit 534379
    DestructionTester &operator=(const DestructionTester &) { print_copy_assigned(this); return *this; }
Packit 534379
    DestructionTester &operator=(DestructionTester &&) { print_move_assigned(this); return *this; }
Packit 534379
};
Packit 534379
namespace pybind11 { namespace detail {
Packit 534379
template <> struct type_caster<DestructionTester> {
Packit 534379
    PYBIND11_TYPE_CASTER(DestructionTester, _("DestructionTester"));
Packit 534379
    bool load(handle, bool) { return true; }
Packit 534379
Packit 534379
    static handle cast(const DestructionTester &, return_value_policy, handle) {
Packit 534379
        return py::bool_(true).release();
Packit 534379
    }
Packit 534379
};
Packit 534379
}}
Packit 534379
Packit 534379
// Test None-allowed py::arg argument policy
Packit 534379
class NoneTester { public: int answer = 42; };
Packit 534379
int none1(const NoneTester &obj) { return obj.answer; }
Packit 534379
int none2(NoneTester *obj) { return obj ? obj->answer : -1; }
Packit 534379
int none3(std::shared_ptr<NoneTester> &obj) { return obj ? obj->answer : -1; }
Packit 534379
int none4(std::shared_ptr<NoneTester> *obj) { return obj && *obj ? (*obj)->answer : -1; }
Packit 534379
int none5(std::shared_ptr<NoneTester> obj) { return obj ? obj->answer : -1; }
Packit 534379
Packit 534379
struct StrIssue {
Packit 534379
    int val = -1;
Packit 534379
Packit 534379
    StrIssue() = default;
Packit 534379
    StrIssue(int i) : val{i} {}
Packit 534379
};
Packit 534379
Packit 534379
// Issues #854, #910: incompatible function args when member function/pointer is in unregistered base class
Packit 534379
class UnregisteredBase {
Packit 534379
public:
Packit 534379
    void do_nothing() const {}
Packit 534379
    void increase_value() { rw_value++; ro_value += 0.25; }
Packit 534379
    void set_int(int v) { rw_value = v; }
Packit 534379
    int get_int() const { return rw_value; }
Packit 534379
    double get_double() const { return ro_value; }
Packit 534379
    int rw_value = 42;
Packit 534379
    double ro_value = 1.25;
Packit 534379
};
Packit 534379
class RegisteredDerived : public UnregisteredBase {
Packit 534379
public:
Packit 534379
    using UnregisteredBase::UnregisteredBase;
Packit 534379
    double sum() const { return rw_value + ro_value; }
Packit 534379
};
Packit 534379
Packit 534379
TEST_SUBMODULE(methods_and_attributes, m) {
Packit 534379
    // test_methods_and_attributes
Packit 534379
    py::class_<ExampleMandA> emna(m, "ExampleMandA");
Packit 534379
    emna.def(py::init<>())
Packit 534379
        .def(py::init<int>())
Packit 534379
        .def(py::init<const ExampleMandA&>())
Packit 534379
        .def("add1", &ExampleMandA::add1)
Packit 534379
        .def("add2", &ExampleMandA::add2)
Packit 534379
        .def("add3", &ExampleMandA::add3)
Packit 534379
        .def("add4", &ExampleMandA::add4)
Packit 534379
        .def("add5", &ExampleMandA::add5)
Packit 534379
        .def("add6", &ExampleMandA::add6)
Packit 534379
        .def("add7", &ExampleMandA::add7)
Packit 534379
        .def("add8", &ExampleMandA::add8)
Packit 534379
        .def("add9", &ExampleMandA::add9)
Packit 534379
        .def("add10", &ExampleMandA::add10)
Packit 534379
        .def("self1", &ExampleMandA::self1)
Packit 534379
        .def("self2", &ExampleMandA::self2)
Packit 534379
        .def("self3", &ExampleMandA::self3)
Packit 534379
        .def("self4", &ExampleMandA::self4)
Packit 534379
        .def("self5", &ExampleMandA::self5)
Packit 534379
        .def("internal1", &ExampleMandA::internal1)
Packit 534379
        .def("internal2", &ExampleMandA::internal2)
Packit 534379
        .def("internal3", &ExampleMandA::internal3)
Packit 534379
        .def("internal4", &ExampleMandA::internal4)
Packit 534379
        .def("internal5", &ExampleMandA::internal5)
Packit 534379
#if defined(PYBIND11_OVERLOAD_CAST)
Packit 534379
        .def("overloaded", py::overload_cast<>(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded", py::overload_cast<int>(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded", py::overload_cast<int,   float>(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded", py::overload_cast<float,   int>(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded", py::overload_cast<int,     int>(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded", py::overload_cast<float, float>(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded_float", py::overload_cast<float, float>(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded_const", py::overload_cast<int         >(&ExampleMandA::overloaded, py::const_))
Packit 534379
        .def("overloaded_const", py::overload_cast<int,   float>(&ExampleMandA::overloaded, py::const_))
Packit 534379
        .def("overloaded_const", py::overload_cast<float,   int>(&ExampleMandA::overloaded, py::const_))
Packit 534379
        .def("overloaded_const", py::overload_cast<int,     int>(&ExampleMandA::overloaded, py::const_))
Packit 534379
        .def("overloaded_const", py::overload_cast<float, float>(&ExampleMandA::overloaded, py::const_))
Packit 534379
#else
Packit 534379
        // Use both the traditional static_cast method and the C++11 compatible overload_cast_
Packit 534379
        .def("overloaded", overload_cast_<>()(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded", overload_cast_<int>()(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded", overload_cast_<int,   float>()(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded", static_cast<py::str (ExampleMandA::*)(float,   int)>(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded", static_cast<py::str (ExampleMandA::*)(int,     int)>(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded", static_cast<py::str (ExampleMandA::*)(float, float)>(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded_float", overload_cast_<float, float>()(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded_const", overload_cast_<int         >()(&ExampleMandA::overloaded, py::const_))
Packit 534379
        .def("overloaded_const", overload_cast_<int,   float>()(&ExampleMandA::overloaded, py::const_))
Packit 534379
        .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(float,   int) const>(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(int,     int) const>(&ExampleMandA::overloaded))
Packit 534379
        .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(float, float) const>(&ExampleMandA::overloaded))
Packit 534379
#endif
Packit 534379
        // test_no_mixed_overloads
Packit 534379
        // Raise error if trying to mix static/non-static overloads on the same name:
Packit 534379
        .def_static("add_mixed_overloads1", []() {
Packit 534379
            auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(py::module::import("pybind11_tests.methods_and_attributes").attr("ExampleMandA"));
Packit 534379
            emna.def       ("overload_mixed1", static_cast<py::str (ExampleMandA::*)(int, int)>(&ExampleMandA::overloaded))
Packit 534379
                .def_static("overload_mixed1", static_cast<py::str (              *)(float   )>(&ExampleMandA::overloaded));
Packit 534379
        })
Packit 534379
        .def_static("add_mixed_overloads2", []() {
Packit 534379
            auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(py::module::import("pybind11_tests.methods_and_attributes").attr("ExampleMandA"));
Packit 534379
            emna.def_static("overload_mixed2", static_cast<py::str (              *)(float   )>(&ExampleMandA::overloaded))
Packit 534379
                .def       ("overload_mixed2", static_cast<py::str (ExampleMandA::*)(int, int)>(&ExampleMandA::overloaded));
Packit 534379
        })
Packit 534379
        .def("__str__", &ExampleMandA::toString)
Packit 534379
        .def_readwrite("value", &ExampleMandA::value);
Packit 534379
Packit 534379
    // test_copy_method
Packit 534379
    // Issue #443: can't call copied methods in Python 3
Packit 534379
    emna.attr("add2b") = emna.attr("add2");
Packit 534379
Packit 534379
    // test_properties, test_static_properties, test_static_cls
Packit 534379
    py::class_<TestProperties>(m, "TestProperties")
Packit 534379
        .def(py::init<>())
Packit 534379
        .def_readonly("def_readonly", &TestProperties::value)
Packit 534379
        .def_readwrite("def_readwrite", &TestProperties::value)
Packit 534379
        .def_property("def_writeonly", nullptr,
Packit 534379
                      [](TestProperties& s,int v) { s.value = v; } )
Packit 534379
        .def_property("def_property_writeonly", nullptr, &TestProperties::set)
Packit 534379
        .def_property_readonly("def_property_readonly", &TestProperties::get)
Packit 534379
        .def_property("def_property", &TestProperties::get, &TestProperties::set)
Packit 534379
        .def_property("def_property_impossible", nullptr, nullptr)
Packit 534379
        .def_readonly_static("def_readonly_static", &TestProperties::static_value)
Packit 534379
        .def_readwrite_static("def_readwrite_static", &TestProperties::static_value)
Packit 534379
        .def_property_static("def_writeonly_static", nullptr,
Packit 534379
                             [](py::object, int v) { TestProperties::static_value = v; })
Packit 534379
        .def_property_readonly_static("def_property_readonly_static",
Packit 534379
                                      [](py::object) { return TestProperties::static_get(); })
Packit 534379
        .def_property_static("def_property_writeonly_static", nullptr,
Packit 534379
                             [](py::object, int v) { return TestProperties::static_set(v); })
Packit 534379
        .def_property_static("def_property_static",
Packit 534379
                             [](py::object) { return TestProperties::static_get(); },
Packit 534379
                             [](py::object, int v) { TestProperties::static_set(v); })
Packit 534379
        .def_property_static("static_cls",
Packit 534379
                             [](py::object cls) { return cls; },
Packit 534379
                             [](py::object cls, py::function f) { f(cls); });
Packit 534379
Packit 534379
    py::class_<TestPropertiesOverride, TestProperties>(m, "TestPropertiesOverride")
Packit 534379
        .def(py::init<>())
Packit 534379
        .def_readonly("def_readonly", &TestPropertiesOverride::value)
Packit 534379
        .def_readonly_static("def_readonly_static", &TestPropertiesOverride::static_value);
Packit 534379
Packit 534379
    auto static_get1 = [](py::object) -> const UserType & { return TestPropRVP::sv1; };
Packit 534379
    auto static_get2 = [](py::object) -> const UserType & { return TestPropRVP::sv2; };
Packit 534379
    auto static_set1 = [](py::object, int v) { TestPropRVP::sv1.set(v); };
Packit 534379
    auto static_set2 = [](py::object, int v) { TestPropRVP::sv2.set(v); };
Packit 534379
    auto rvp_copy = py::return_value_policy::copy;
Packit 534379
Packit 534379
    // test_property_return_value_policies
Packit 534379
    py::class_<TestPropRVP>(m, "TestPropRVP")
Packit 534379
        .def(py::init<>())
Packit 534379
        .def_property_readonly("ro_ref", &TestPropRVP::get1)
Packit 534379
        .def_property_readonly("ro_copy", &TestPropRVP::get2, rvp_copy)
Packit 534379
        .def_property_readonly("ro_func", py::cpp_function(&TestPropRVP::get2, rvp_copy))
Packit 534379
        .def_property("rw_ref", &TestPropRVP::get1, &TestPropRVP::set1)
Packit 534379
        .def_property("rw_copy", &TestPropRVP::get2, &TestPropRVP::set2, rvp_copy)
Packit 534379
        .def_property("rw_func", py::cpp_function(&TestPropRVP::get2, rvp_copy), &TestPropRVP::set2)
Packit 534379
        .def_property_readonly_static("static_ro_ref", static_get1)
Packit 534379
        .def_property_readonly_static("static_ro_copy", static_get2, rvp_copy)
Packit 534379
        .def_property_readonly_static("static_ro_func", py::cpp_function(static_get2, rvp_copy))
Packit 534379
        .def_property_static("static_rw_ref", static_get1, static_set1)
Packit 534379
        .def_property_static("static_rw_copy", static_get2, static_set2, rvp_copy)
Packit 534379
        .def_property_static("static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2)
Packit 534379
        // test_property_rvalue_policy
Packit 534379
        .def_property_readonly("rvalue", &TestPropRVP::get_rvalue)
Packit 534379
        .def_property_readonly_static("static_rvalue", [](py::object) { return UserType(1); });
Packit 534379
Packit 534379
    // test_metaclass_override
Packit 534379
    struct MetaclassOverride { };
Packit 534379
    py::class_<MetaclassOverride>(m, "MetaclassOverride", py::metaclass((PyObject *) &PyType_Type))
Packit 534379
        .def_property_readonly_static("readonly", [](py::object) { return 1; });
Packit 534379
Packit 534379
#if !defined(PYPY_VERSION)
Packit 534379
    // test_dynamic_attributes
Packit 534379
    class DynamicClass {
Packit 534379
    public:
Packit 534379
        DynamicClass() { print_default_created(this); }
Packit 534379
        ~DynamicClass() { print_destroyed(this); }
Packit 534379
    };
Packit 534379
    py::class_<DynamicClass>(m, "DynamicClass", py::dynamic_attr())
Packit 534379
        .def(py::init());
Packit 534379
Packit 534379
    class CppDerivedDynamicClass : public DynamicClass { };
Packit 534379
    py::class_<CppDerivedDynamicClass, DynamicClass>(m, "CppDerivedDynamicClass")
Packit 534379
        .def(py::init());
Packit 534379
#endif
Packit 534379
Packit 534379
    // test_noconvert_args
Packit 534379
    //
Packit 534379
    // Test converting.  The ArgAlwaysConverts is just there to make the first no-conversion pass
Packit 534379
    // fail so that our call always ends up happening via the second dispatch (the one that allows
Packit 534379
    // some conversion).
Packit 534379
    class ArgInspector {
Packit 534379
    public:
Packit 534379
        ArgInspector1 f(ArgInspector1 a, ArgAlwaysConverts) { return a; }
Packit 534379
        std::string g(ArgInspector1 a, const ArgInspector1 &b, int c, ArgInspector2 *d, ArgAlwaysConverts) {
Packit 534379
            return a.arg + "\n" + b.arg + "\n" + std::to_string(c) + "\n" + d->arg;
Packit 534379
        }
Packit 534379
        static ArgInspector2 h(ArgInspector2 a, ArgAlwaysConverts) { return a; }
Packit 534379
    };
Packit 534379
    py::class_<ArgInspector>(m, "ArgInspector")
Packit 534379
        .def(py::init<>())
Packit 534379
        .def("f", &ArgInspector::f, py::arg(), py::arg() = ArgAlwaysConverts())
Packit 534379
        .def("g", &ArgInspector::g, "a"_a.noconvert(), "b"_a, "c"_a.noconvert()=13, "d"_a=ArgInspector2(), py::arg() = ArgAlwaysConverts())
Packit 534379
        .def_static("h", &ArgInspector::h, py::arg().noconvert(), py::arg() = ArgAlwaysConverts())
Packit 534379
        ;
Packit 534379
    m.def("arg_inspect_func", [](ArgInspector2 a, ArgInspector1 b, ArgAlwaysConverts) { return a.arg + "\n" + b.arg; },
Packit 534379
            py::arg().noconvert(false), py::arg_v(nullptr, ArgInspector1()).noconvert(true), py::arg() = ArgAlwaysConverts());
Packit 534379
Packit 534379
    m.def("floats_preferred", [](double f) { return 0.5 * f; }, py::arg("f"));
Packit 534379
    m.def("floats_only", [](double f) { return 0.5 * f; }, py::arg("f").noconvert());
Packit 534379
    m.def("ints_preferred", [](int i) { return i / 2; }, py::arg("i"));
Packit 534379
    m.def("ints_only", [](int i) { return i / 2; }, py::arg("i").noconvert());
Packit 534379
Packit 534379
    // test_bad_arg_default
Packit 534379
    // Issue/PR #648: bad arg default debugging output
Packit 534379
#if !defined(NDEBUG)
Packit 534379
    m.attr("debug_enabled") = true;
Packit 534379
#else
Packit 534379
    m.attr("debug_enabled") = false;
Packit 534379
#endif
Packit 534379
    m.def("bad_arg_def_named", []{
Packit 534379
        auto m = py::module::import("pybind11_tests");
Packit 534379
        m.def("should_fail", [](int, UnregisteredType) {}, py::arg(), py::arg("a") = UnregisteredType());
Packit 534379
    });
Packit 534379
    m.def("bad_arg_def_unnamed", []{
Packit 534379
        auto m = py::module::import("pybind11_tests");
Packit 534379
        m.def("should_fail", [](int, UnregisteredType) {}, py::arg(), py::arg() = UnregisteredType());
Packit 534379
    });
Packit 534379
Packit 534379
    // test_accepts_none
Packit 534379
    py::class_<NoneTester, std::shared_ptr<NoneTester>>(m, "NoneTester")
Packit 534379
        .def(py::init<>());
Packit 534379
    m.def("no_none1", &none1, py::arg().none(false));
Packit 534379
    m.def("no_none2", &none2, py::arg().none(false));
Packit 534379
    m.def("no_none3", &none3, py::arg().none(false));
Packit 534379
    m.def("no_none4", &none4, py::arg().none(false));
Packit 534379
    m.def("no_none5", &none5, py::arg().none(false));
Packit 534379
    m.def("ok_none1", &none1);
Packit 534379
    m.def("ok_none2", &none2, py::arg().none(true));
Packit 534379
    m.def("ok_none3", &none3);
Packit 534379
    m.def("ok_none4", &none4, py::arg().none(true));
Packit 534379
    m.def("ok_none5", &none5);
Packit 534379
Packit 534379
    // test_str_issue
Packit 534379
    // Issue #283: __str__ called on uninitialized instance when constructor arguments invalid
Packit 534379
    py::class_<StrIssue>(m, "StrIssue")
Packit 534379
        .def(py::init<int>())
Packit 534379
        .def(py::init<>())
Packit 534379
        .def("__str__", [](const StrIssue &si) {
Packit 534379
            return "StrIssue[" + std::to_string(si.val) + "]"; }
Packit 534379
        );
Packit 534379
Packit 534379
    // test_unregistered_base_implementations
Packit 534379
    //
Packit 534379
    // Issues #854/910: incompatible function args when member function/pointer is in unregistered
Packit 534379
    // base class The methods and member pointers below actually resolve to members/pointers in
Packit 534379
    // UnregisteredBase; before this test/fix they would be registered via lambda with a first
Packit 534379
    // argument of an unregistered type, and thus uncallable.
Packit 534379
    py::class_<RegisteredDerived>(m, "RegisteredDerived")
Packit 534379
        .def(py::init<>())
Packit 534379
        .def("do_nothing", &RegisteredDerived::do_nothing)
Packit 534379
        .def("increase_value", &RegisteredDerived::increase_value)
Packit 534379
        .def_readwrite("rw_value", &RegisteredDerived::rw_value)
Packit 534379
        .def_readonly("ro_value", &RegisteredDerived::ro_value)
Packit 534379
        // These should trigger a static_assert if uncommented
Packit 534379
        //.def_readwrite("fails", &UserType::value) // should trigger a static_assert if uncommented
Packit 534379
        //.def_readonly("fails", &UserType::value) // should trigger a static_assert if uncommented
Packit 534379
        .def_property("rw_value_prop", &RegisteredDerived::get_int, &RegisteredDerived::set_int)
Packit 534379
        .def_property_readonly("ro_value_prop", &RegisteredDerived::get_double)
Packit 534379
        // This one is in the registered class:
Packit 534379
        .def("sum", &RegisteredDerived::sum)
Packit 534379
        ;
Packit 534379
Packit 534379
    using Adapted = decltype(py::method_adaptor<RegisteredDerived>(&RegisteredDerived::do_nothing));
Packit 534379
    static_assert(std::is_same<Adapted, void (RegisteredDerived::*)() const>::value, "");
Packit 534379
Packit 534379
    // test_custom_caster_destruction
Packit 534379
    // Test that `take_ownership` works on types with a custom type caster when given a pointer
Packit 534379
Packit 534379
    // default policy: don't take ownership:
Packit 534379
    m.def("custom_caster_no_destroy", []() { static auto *dt = new DestructionTester(); return dt; });
Packit 534379
Packit 534379
    m.def("custom_caster_destroy", []() { return new DestructionTester(); },
Packit 534379
            py::return_value_policy::take_ownership); // Takes ownership: destroy when finished
Packit 534379
    m.def("custom_caster_destroy_const", []() -> const DestructionTester * { return new DestructionTester(); },
Packit 534379
            py::return_value_policy::take_ownership); // Likewise (const doesn't inhibit destruction)
Packit 534379
    m.def("destruction_tester_cstats", &ConstructorStats::get<DestructionTester>, py::return_value_policy::reference);
Packit 534379
}