Boilerplate changes: Skip all test_class_sh_* if PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT is not defined.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2024-07-19 09:37:45 -07:00
parent 6fb8b5157d
commit 95e9053f7f
40 changed files with 261 additions and 34 deletions

View File

@ -19,9 +19,16 @@ atyp rtrn_valu_atyp() { return atyp(); }
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_module_local::atyp) PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_module_local::atyp)
PYBIND11_MODULE(class_sh_module_local_0, m) { PYBIND11_MODULE(class_sh_module_local_0, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
using namespace pybind11_tests::class_sh_module_local; using namespace pybind11_tests::class_sh_module_local;
m.def("get_mtxt", get_mtxt); m.def("get_mtxt", get_mtxt);
m.def("rtrn_valu_atyp", rtrn_valu_atyp); m.def("rtrn_valu_atyp", rtrn_valu_atyp);
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -18,6 +18,12 @@ std::string get_mtxt(const atyp &obj) { return obj.mtxt; }
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_module_local::atyp) PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_module_local::atyp)
PYBIND11_MODULE(class_sh_module_local_1, m) { PYBIND11_MODULE(class_sh_module_local_1, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
namespace py = pybind11; namespace py = pybind11;
using namespace pybind11_tests::class_sh_module_local; using namespace pybind11_tests::class_sh_module_local;
@ -30,4 +36,5 @@ PYBIND11_MODULE(class_sh_module_local_1, m) {
.def("tag", [](const atyp &) { return 1; }); .def("tag", [](const atyp &) { return 1; });
m.def("get_mtxt", get_mtxt); m.def("get_mtxt", get_mtxt);
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -18,6 +18,12 @@ std::string get_mtxt(const atyp &obj) { return obj.mtxt; }
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_module_local::atyp) PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_module_local::atyp)
PYBIND11_MODULE(class_sh_module_local_2, m) { PYBIND11_MODULE(class_sh_module_local_2, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
namespace py = pybind11; namespace py = pybind11;
using namespace pybind11_tests::class_sh_module_local; using namespace pybind11_tests::class_sh_module_local;
@ -30,4 +36,5 @@ PYBIND11_MODULE(class_sh_module_local_2, m) {
.def("tag", [](const atyp &) { return 2; }); .def("tag", [](const atyp &) { return 2; });
m.def("get_mtxt", get_mtxt); m.def("get_mtxt", get_mtxt);
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -145,6 +145,12 @@ namespace pybind11_tests {
namespace class_sh_basic { namespace class_sh_basic {
TEST_SUBMODULE(class_sh_basic, m) { TEST_SUBMODULE(class_sh_basic, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
namespace py = pybind11; namespace py = pybind11;
py::classh<atyp>(m, "atyp").def(py::init<>()).def(py::init([](const std::string &mtxt) { py::classh<atyp>(m, "atyp").def(py::init<>()).def(py::init([](const std::string &mtxt) {
@ -238,6 +244,7 @@ TEST_SUBMODULE(class_sh_basic, m) {
[]() { return CastUnusualOpRefConstRef(LocalUnusualOpRef()); }); []() { return CastUnusualOpRefConstRef(LocalUnusualOpRef()); });
m.def("CallCastUnusualOpRefMovable", m.def("CallCastUnusualOpRefMovable",
[]() { return CastUnusualOpRefMovable(LocalUnusualOpRef()); }); []() { return CastUnusualOpRefMovable(LocalUnusualOpRef()); });
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }
} // namespace class_sh_basic } // namespace class_sh_basic

View File

@ -7,6 +7,9 @@ import pytest
from pybind11_tests import class_sh_basic as m from pybind11_tests import class_sh_basic as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
def test_atyp_constructors(): def test_atyp_constructors():
obj = m.atyp() obj = m.atyp()

View File

@ -32,6 +32,12 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning::Atype<1>)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning::Atype<2>) PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning::Atype<2>)
TEST_SUBMODULE(class_sh_disowning, m) { TEST_SUBMODULE(class_sh_disowning, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
using namespace pybind11_tests::class_sh_disowning; using namespace pybind11_tests::class_sh_disowning;
py::classh<Atype<1>>(m, "Atype1").def(py::init<int>()).def("get", &Atype<1>::get); py::classh<Atype<1>>(m, "Atype1").def(py::init<int>()).def("get", &Atype<1>::get);
@ -43,4 +49,5 @@ TEST_SUBMODULE(class_sh_disowning, m) {
m.def("overloaded", (int (*)(std::unique_ptr<Atype<1>>, int)) & overloaded); m.def("overloaded", (int (*)(std::unique_ptr<Atype<1>>, int)) & overloaded);
m.def("overloaded", (int (*)(std::unique_ptr<Atype<2>>, int)) & overloaded); m.def("overloaded", (int (*)(std::unique_ptr<Atype<2>>, int)) & overloaded);
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -4,6 +4,9 @@ import pytest
from pybind11_tests import class_sh_disowning as m from pybind11_tests import class_sh_disowning as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
def is_disowned(obj): def is_disowned(obj):
try: try:

View File

@ -57,6 +57,12 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning_mi::Base1)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning_mi::Base2) PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning_mi::Base2)
TEST_SUBMODULE(class_sh_disowning_mi, m) { TEST_SUBMODULE(class_sh_disowning_mi, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
using namespace pybind11_tests::class_sh_disowning_mi; using namespace pybind11_tests::class_sh_disowning_mi;
py::classh<B>(m, "B") py::classh<B>(m, "B")
@ -92,4 +98,5 @@ TEST_SUBMODULE(class_sh_disowning_mi, m) {
py::classh<Base2>(m, "Base2").def(py::init<int>()).def("bar", &Base2::bar); py::classh<Base2>(m, "Base2").def(py::init<int>()).def("bar", &Base2::bar);
m.def("disown_base1", disown_base1); m.def("disown_base1", disown_base1);
m.def("disown_base2", disown_base2); m.def("disown_base2", disown_base2);
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -5,6 +5,9 @@ import pytest
import env # noqa: F401 import env # noqa: F401
from pybind11_tests import class_sh_disowning_mi as m from pybind11_tests import class_sh_disowning_mi as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
def test_diamond_inheritance(): def test_diamond_inheritance():
# Very similar to test_multiple_inheritance.py:test_diamond_inheritance. # Very similar to test_multiple_inheritance.py:test_diamond_inheritance.

View File

@ -87,6 +87,12 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::with_alias) PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::with_alias)
TEST_SUBMODULE(class_sh_factory_constructors, m) { TEST_SUBMODULE(class_sh_factory_constructors, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
using namespace pybind11_tests::class_sh_factory_constructors; using namespace pybind11_tests::class_sh_factory_constructors;
py::classh<atyp_valu>(m, "atyp_valu") py::classh<atyp_valu>(m, "atyp_valu")
@ -177,4 +183,5 @@ TEST_SUBMODULE(class_sh_factory_constructors, m) {
[](int, int, int, int, int) { [](int, int, int, int, int) {
return std::make_shared<with_alias>(); // Invalid alias factory. return std::make_shared<with_alias>(); // Invalid alias factory.
})); }));
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -4,6 +4,9 @@ import pytest
from pybind11_tests import class_sh_factory_constructors as m from pybind11_tests import class_sh_factory_constructors as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
def test_atyp_factories(): def test_atyp_factories():
assert m.atyp_valu().get_mtxt() == "Valu" assert m.atyp_valu().get_mtxt() == "Valu"

View File

@ -73,6 +73,12 @@ namespace pybind11_tests {
namespace class_sh_inheritance { namespace class_sh_inheritance {
TEST_SUBMODULE(class_sh_inheritance, m) { TEST_SUBMODULE(class_sh_inheritance, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
py::classh<base>(m, "base"); py::classh<base>(m, "base");
py::classh<drvd, base>(m, "drvd"); py::classh<drvd, base>(m, "drvd");
@ -99,6 +105,7 @@ TEST_SUBMODULE(class_sh_inheritance, m) {
m.def("pass_cptr_base1", pass_cptr_base1); m.def("pass_cptr_base1", pass_cptr_base1);
m.def("pass_cptr_base2", pass_cptr_base2); m.def("pass_cptr_base2", pass_cptr_base2);
m.def("pass_cptr_drvd2", pass_cptr_drvd2); m.def("pass_cptr_drvd2", pass_cptr_drvd2);
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }
} // namespace class_sh_inheritance } // namespace class_sh_inheritance

View File

@ -1,7 +1,12 @@
from __future__ import annotations from __future__ import annotations
import pytest
from pybind11_tests import class_sh_inheritance as m from pybind11_tests import class_sh_inheritance as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
def test_rtrn_mptr_drvd_pass_cptr_base(): def test_rtrn_mptr_drvd_pass_cptr_base():
d = m.rtrn_mptr_drvd() d = m.rtrn_mptr_drvd()

View File

@ -40,6 +40,12 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_mi_thunks::Base1)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_mi_thunks::Derived) PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_mi_thunks::Derived)
TEST_SUBMODULE(class_sh_mi_thunks, m) { TEST_SUBMODULE(class_sh_mi_thunks, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
using namespace test_class_sh_mi_thunks; using namespace test_class_sh_mi_thunks;
m.def("ptrdiff_drvd_base0", []() { m.def("ptrdiff_drvd_base0", []() {
@ -97,4 +103,5 @@ TEST_SUBMODULE(class_sh_mi_thunks, m) {
} }
return obj_der->vec.size(); return obj_der->vec.size();
}); });
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -4,6 +4,9 @@ import pytest
from pybind11_tests import class_sh_mi_thunks as m from pybind11_tests import class_sh_mi_thunks as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
def test_ptrdiff_drvd_base0(): def test_ptrdiff_drvd_base0():
ptrdiff = m.ptrdiff_drvd_base0() ptrdiff = m.ptrdiff_drvd_base0()

View File

@ -5,6 +5,9 @@ import class_sh_module_local_1 as m1
import class_sh_module_local_2 as m2 import class_sh_module_local_2 as m2
import pytest import pytest
if not m0.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
def test_cross_module_get_mtxt(): def test_cross_module_get_mtxt():
obj1 = m1.atyp("A") obj1 = m1.atyp("A")

View File

@ -46,6 +46,12 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_property::Field)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_property::Outer) PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_property::Outer)
TEST_SUBMODULE(class_sh_property, m) { TEST_SUBMODULE(class_sh_property, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
using namespace test_class_sh_property; using namespace test_class_sh_property;
py::class_<ClassicField, std::unique_ptr<ClassicField>>(m, "ClassicField") py::class_<ClassicField, std::unique_ptr<ClassicField>>(m, "ClassicField")
@ -83,4 +89,5 @@ TEST_SUBMODULE(class_sh_property, m) {
.def_readwrite("m_shcp_readwrite", &Outer::m_shcp); .def_readwrite("m_shcp_readwrite", &Outer::m_shcp);
m.def("DisownOuter", DisownOuter); m.def("DisownOuter", DisownOuter);
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -7,6 +7,9 @@ import pytest
import env # noqa: F401 import env # noqa: F401
from pybind11_tests import class_sh_property as m from pybind11_tests import class_sh_property as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
@pytest.mark.xfail("env.PYPY", reason="gc after `del field` is apparently deferred") @pytest.mark.xfail("env.PYPY", reason="gc after `del field` is apparently deferred")
@pytest.mark.parametrize("m_attr", ["m_valu_readonly", "m_valu_readwrite"]) @pytest.mark.parametrize("m_attr", ["m_valu_readonly", "m_valu_readwrite"])

View File

@ -16,6 +16,12 @@ struct WithConstCharPtrMember {
} // namespace test_class_sh_property_bakein } // namespace test_class_sh_property_bakein
TEST_SUBMODULE(class_sh_property_bakein, m) { TEST_SUBMODULE(class_sh_property_bakein, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
using namespace test_class_sh_property_bakein; using namespace test_class_sh_property_bakein;
py::class_<WithCharArrayMember>(m, "WithCharArrayMember") py::class_<WithCharArrayMember>(m, "WithCharArrayMember")
@ -25,4 +31,5 @@ TEST_SUBMODULE(class_sh_property_bakein, m) {
py::class_<WithConstCharPtrMember>(m, "WithConstCharPtrMember") py::class_<WithConstCharPtrMember>(m, "WithConstCharPtrMember")
.def(py::init<>()) .def(py::init<>())
.def_readonly("const_char_ptr_member", &WithConstCharPtrMember::const_char_ptr_member); .def_readonly("const_char_ptr_member", &WithConstCharPtrMember::const_char_ptr_member);
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -1,7 +1,12 @@
from __future__ import annotations from __future__ import annotations
import pytest
from pybind11_tests import class_sh_property_bakein as m from pybind11_tests import class_sh_property_bakein as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
def test_readonly_char6_member(): def test_readonly_char6_member():
obj = m.WithCharArrayMember() obj = m.WithCharArrayMember()

View File

@ -51,6 +51,12 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(DataField)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(DataFieldsHolder) PYBIND11_SMART_HOLDER_TYPE_CASTERS(DataFieldsHolder)
TEST_SUBMODULE(class_sh_property_non_owning, m) { TEST_SUBMODULE(class_sh_property_non_owning, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
py::classh<CoreField>(m, "CoreField").def_readwrite("int_value", &CoreField::int_value); py::classh<CoreField>(m, "CoreField").def_readwrite("int_value", &CoreField::int_value);
py::classh<DataField>(m, "DataField") py::classh<DataField>(m, "DataField")
@ -65,4 +71,5 @@ TEST_SUBMODULE(class_sh_property_non_owning, m) {
py::classh<DataFieldsHolder>(m, "DataFieldsHolder") py::classh<DataFieldsHolder>(m, "DataFieldsHolder")
.def(py::init<std::size_t>()) .def(py::init<std::size_t>())
.def("vec_at", &DataFieldsHolder::vec_at, py::return_value_policy::reference_internal); .def("vec_at", &DataFieldsHolder::vec_at, py::return_value_policy::reference_internal);
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -4,6 +4,9 @@ import pytest
from pybind11_tests import class_sh_property_non_owning as m from pybind11_tests import class_sh_property_non_owning as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
@pytest.mark.parametrize("persistent_holder", [True, False]) @pytest.mark.parametrize("persistent_holder", [True, False])
@pytest.mark.parametrize( @pytest.mark.parametrize(

View File

@ -50,6 +50,12 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::FooSmHld)
namespace pybind11_tests { namespace pybind11_tests {
TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) { TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
namespace py = pybind11; namespace py = pybind11;
py::class_<FooShPtr, std::shared_ptr<FooShPtr>>(m, "FooShPtr") py::class_<FooShPtr, std::shared_ptr<FooShPtr>>(m, "FooShPtr")
@ -57,20 +63,20 @@ TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) {
py::classh<FooSmHld>(m, "FooSmHld").def("get_history", &FooSmHld::get_history); py::classh<FooSmHld>(m, "FooSmHld").def("get_history", &FooSmHld::get_history);
auto outer = py::class_<Outer>(m, "Outer").def(py::init()); auto outer = py::class_<Outer>(m, "Outer").def(py::init());
#define MAKE_PROP(PropTyp) \ # define MAKE_PROP(PropTyp) \
MAKE_PROP_FOO(ShPtr, PropTyp) \ MAKE_PROP_FOO(ShPtr, PropTyp) \
MAKE_PROP_FOO(SmHld, PropTyp) MAKE_PROP_FOO(SmHld, PropTyp)
#define MAKE_PROP_FOO(FooTyp, PropTyp) \ # define MAKE_PROP_FOO(FooTyp, PropTyp) \
.def_##PropTyp(#FooTyp "_" #PropTyp "_default", &Outer::FooTyp) \ .def_##PropTyp(#FooTyp "_" #PropTyp "_default", &Outer::FooTyp) \
.def_##PropTyp( \ .def_##PropTyp( \
#FooTyp "_" #PropTyp "_copy", &Outer::FooTyp, py::return_value_policy::copy) \ #FooTyp "_" #PropTyp "_copy", &Outer::FooTyp, py::return_value_policy::copy) \
.def_##PropTyp( \ .def_##PropTyp( \
#FooTyp "_" #PropTyp "_move", &Outer::FooTyp, py::return_value_policy::move) #FooTyp "_" #PropTyp "_move", &Outer::FooTyp, py::return_value_policy::move)
outer MAKE_PROP(readonly) MAKE_PROP(readwrite); outer MAKE_PROP(readonly) MAKE_PROP(readwrite);
#undef MAKE_PROP_FOO # undef MAKE_PROP_FOO
#define MAKE_PROP_FOO(FooTyp, PropTyp) \ # define MAKE_PROP_FOO(FooTyp, PropTyp) \
.def_##PropTyp(#FooTyp "_property_" #PropTyp "_default", &Outer::FooTyp) \ .def_##PropTyp(#FooTyp "_property_" #PropTyp "_default", &Outer::FooTyp) \
.def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_copy", \ .def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_copy", \
&Outer::get##FooTyp, \ &Outer::get##FooTyp, \
@ -79,8 +85,8 @@ TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) {
&Outer::get##FooTyp, \ &Outer::get##FooTyp, \
py::return_value_policy::move) py::return_value_policy::move)
outer MAKE_PROP(readonly); outer MAKE_PROP(readonly);
#undef MAKE_PROP_FOO # undef MAKE_PROP_FOO
#undef MAKE_PROP # undef MAKE_PROP
m.def("test_ShPtr_copy", []() { m.def("test_ShPtr_copy", []() {
auto o = std::make_shared<FooShPtr>("copy"); auto o = std::make_shared<FooShPtr>("copy");
@ -107,6 +113,7 @@ TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) {
l.append(std::move(o)); l.append(std::move(o));
return l; return l;
}); });
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }
} // namespace pybind11_tests } // namespace pybind11_tests

View File

@ -1,7 +1,12 @@
from __future__ import annotations from __future__ import annotations
import pytest
from pybind11_tests import class_sh_shared_ptr_copy_move as m from pybind11_tests import class_sh_shared_ptr_copy_move as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
def test_shptr_copy(): def test_shptr_copy():
txt = m.test_ShPtr_copy()[0].get_history() txt = m.test_ShPtr_copy()[0].get_history()

View File

@ -76,11 +76,19 @@ void wrap(py::module_ m, const char *py_class_name) {
} // namespace class_sh_trampoline_basic } // namespace class_sh_trampoline_basic
} // namespace pybind11_tests } // namespace pybind11_tests
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_trampoline_basic::Abase<0>) using namespace pybind11_tests::class_sh_trampoline_basic;
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_trampoline_basic::Abase<1>)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Abase<0>)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Abase<1>)
TEST_SUBMODULE(class_sh_trampoline_basic, m) { TEST_SUBMODULE(class_sh_trampoline_basic, m) {
using namespace pybind11_tests::class_sh_trampoline_basic; m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
wrap<0>(m, "Abase0"); wrap<0>(m, "Abase0");
wrap<1>(m, "Abase1"); wrap<1>(m, "Abase1");
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -4,6 +4,9 @@ import pytest
from pybind11_tests import class_sh_trampoline_basic as m from pybind11_tests import class_sh_trampoline_basic as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
class PyDrvd0(m.Abase0): class PyDrvd0(m.Abase0):
def __init__(self, val): def __init__(self, val):

View File

@ -46,6 +46,12 @@ struct Big5Trampoline : Big5, py::trampoline_self_life_support {
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Big5) PYBIND11_SMART_HOLDER_TYPE_CASTERS(Big5)
TEST_SUBMODULE(class_sh_trampoline_self_life_support, m) { TEST_SUBMODULE(class_sh_trampoline_self_life_support, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
py::classh<Big5, Big5Trampoline>(m, "Big5") py::classh<Big5, Big5Trampoline>(m, "Big5")
.def(py::init<std::string>()) .def(py::init<std::string>())
.def_readonly("history", &Big5::history); .def_readonly("history", &Big5::history);
@ -82,4 +88,5 @@ TEST_SUBMODULE(class_sh_trampoline_self_life_support, m) {
py::object o1 = py::cast(std::move(obj)); py::object o1 = py::cast(std::move(obj));
return py::make_tuple(o1, o2); return py::make_tuple(o1, o2);
}); });
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -4,6 +4,9 @@ import pytest
import pybind11_tests.class_sh_trampoline_self_life_support as m import pybind11_tests.class_sh_trampoline_self_life_support as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
class PyBig5(m.Big5): class PyBig5(m.Big5):
pass pass

View File

@ -8,7 +8,8 @@
#include <memory> #include <memory>
#include <string> #include <string>
namespace { namespace pybind11_tests {
namespace class_sh_trampoline_shared_from_this {
struct Sft : std::enable_shared_from_this<Sft> { struct Sft : std::enable_shared_from_this<Sft> {
std::string history; std::string history;
@ -98,12 +99,21 @@ std::shared_ptr<Sft> make_pure_cpp_sft_shd_ptr(const std::string &history_seed)
std::shared_ptr<Sft> pass_through_shd_ptr(const std::shared_ptr<Sft> &obj) { return obj; } std::shared_ptr<Sft> pass_through_shd_ptr(const std::shared_ptr<Sft> &obj) { return obj; }
} // namespace } // namespace class_sh_trampoline_shared_from_this
} // namespace pybind11_tests
using namespace pybind11_tests::class_sh_trampoline_shared_from_this;
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Sft) PYBIND11_SMART_HOLDER_TYPE_CASTERS(Sft)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(SftSharedPtrStash) PYBIND11_SMART_HOLDER_TYPE_CASTERS(SftSharedPtrStash)
TEST_SUBMODULE(class_sh_trampoline_shared_from_this, m) { TEST_SUBMODULE(class_sh_trampoline_shared_from_this, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
py::classh<Sft, SftTrampoline>(m, "Sft") py::classh<Sft, SftTrampoline>(m, "Sft")
.def(py::init<const std::string &>()) .def(py::init<const std::string &>())
.def(py::init([](const std::string &history, int) { .def(py::init([](const std::string &history, int) {
@ -128,4 +138,5 @@ TEST_SUBMODULE(class_sh_trampoline_shared_from_this, m) {
m.def("make_pure_cpp_sft_unq_ptr", make_pure_cpp_sft_unq_ptr); m.def("make_pure_cpp_sft_unq_ptr", make_pure_cpp_sft_unq_ptr);
m.def("make_pure_cpp_sft_shd_ptr", make_pure_cpp_sft_shd_ptr); m.def("make_pure_cpp_sft_shd_ptr", make_pure_cpp_sft_shd_ptr);
m.def("pass_through_shd_ptr", pass_through_shd_ptr); m.def("pass_through_shd_ptr", pass_through_shd_ptr);
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -8,6 +8,9 @@ import pytest
import env import env
import pybind11_tests.class_sh_trampoline_shared_from_this as m import pybind11_tests.class_sh_trampoline_shared_from_this as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
class PySft(m.Sft): class PySft(m.Sft):
pass pass

View File

@ -7,7 +7,8 @@
#include <utility> #include <utility>
namespace { namespace pybind11_tests {
namespace class_sh_trampoline_shared_ptr_cpp_arg {
// For testing whether a python subclass of a C++ object dies when the // For testing whether a python subclass of a C++ object dies when the
// last python reference is lost // last python reference is lost
@ -51,7 +52,10 @@ struct SpGoAwayTester {
std::shared_ptr<SpGoAway> m_obj; std::shared_ptr<SpGoAway> m_obj;
}; };
} // namespace } // namespace class_sh_trampoline_shared_ptr_cpp_arg
} // namespace pybind11_tests
using namespace pybind11_tests::class_sh_trampoline_shared_ptr_cpp_arg;
PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpBase) PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpBase)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpBaseTester) PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpBaseTester)
@ -59,6 +63,12 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpGoAway)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpGoAwayTester) PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpGoAwayTester)
TEST_SUBMODULE(class_sh_trampoline_shared_ptr_cpp_arg, m) { TEST_SUBMODULE(class_sh_trampoline_shared_ptr_cpp_arg, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
// For testing whether a python subclass of a C++ object dies when the // For testing whether a python subclass of a C++ object dies when the
// last python reference is lost // last python reference is lost
@ -91,4 +101,5 @@ TEST_SUBMODULE(class_sh_trampoline_shared_ptr_cpp_arg, m) {
py::classh<SpGoAwayTester>(m, "SpGoAwayTester") py::classh<SpGoAwayTester>(m, "SpGoAwayTester")
.def(py::init<>()) .def(py::init<>())
.def_readwrite("obj", &SpGoAwayTester::m_obj); .def_readwrite("obj", &SpGoAwayTester::m_obj);
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -4,6 +4,9 @@ import pytest
import pybind11_tests.class_sh_trampoline_shared_ptr_cpp_arg as m import pybind11_tests.class_sh_trampoline_shared_ptr_cpp_arg as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
def test_shared_ptr_cpp_arg(): def test_shared_ptr_cpp_arg():
import weakref import weakref

View File

@ -8,7 +8,8 @@
#include <cstdint> #include <cstdint>
namespace { namespace pybind11_tests {
namespace class_sh_trampoline_basic {
class Class { class Class {
public: public:
@ -30,11 +31,7 @@ private:
std::uint64_t val_ = 0; std::uint64_t val_ = 0;
}; };
} // namespace #ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Class)
namespace {
class PyClass : public Class, public py::trampoline_self_life_support { class PyClass : public Class, public py::trampoline_self_life_support {
public: public:
@ -45,9 +42,22 @@ public:
int foo() const override { PYBIND11_OVERRIDE_PURE(int, Class, foo); } int foo() const override { PYBIND11_OVERRIDE_PURE(int, Class, foo); }
}; };
} // namespace #endif
} // namespace class_sh_trampoline_basic
} // namespace pybind11_tests
using namespace pybind11_tests::class_sh_trampoline_basic;
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Class)
TEST_SUBMODULE(class_sh_trampoline_unique_ptr, m) { TEST_SUBMODULE(class_sh_trampoline_unique_ptr, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
py::classh<Class, PyClass>(m, "Class") py::classh<Class, PyClass>(m, "Class")
.def(py::init<>()) .def(py::init<>())
.def("set_val", &Class::setVal) .def("set_val", &Class::setVal)
@ -57,4 +67,5 @@ TEST_SUBMODULE(class_sh_trampoline_unique_ptr, m) {
m.def("clone", [](const Class &obj) { return obj.clone(); }); m.def("clone", [](const Class &obj) { return obj.clone(); });
m.def("clone_and_foo", [](const Class &obj) { return obj.clone()->foo(); }); m.def("clone_and_foo", [](const Class &obj) { return obj.clone()->foo(); });
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -1,7 +1,12 @@
from __future__ import annotations from __future__ import annotations
import pytest
import pybind11_tests.class_sh_trampoline_unique_ptr as m import pybind11_tests.class_sh_trampoline_unique_ptr as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
class MyClass(m.Class): class MyClass(m.Class):
def foo(self): def foo(self):

View File

@ -31,9 +31,16 @@ namespace pybind11_tests {
namespace class_sh_unique_ptr_custom_deleter { namespace class_sh_unique_ptr_custom_deleter {
TEST_SUBMODULE(class_sh_unique_ptr_custom_deleter, m) { TEST_SUBMODULE(class_sh_unique_ptr_custom_deleter, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
py::classh<Pet>(m, "Pet").def_readwrite("name", &Pet::name); py::classh<Pet>(m, "Pet").def_readwrite("name", &Pet::name);
m.def("create", &Pet::New); m.def("create", &Pet::New);
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }
} // namespace class_sh_unique_ptr_custom_deleter } // namespace class_sh_unique_ptr_custom_deleter

View File

@ -1,7 +1,12 @@
from __future__ import annotations from __future__ import annotations
import pytest
from pybind11_tests import class_sh_unique_ptr_custom_deleter as m from pybind11_tests import class_sh_unique_ptr_custom_deleter as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
def test_create(): def test_create():
pet = m.create("abc") pet = m.create("abc")

View File

@ -45,6 +45,12 @@ namespace pybind11_tests {
namespace class_sh_unique_ptr_member { namespace class_sh_unique_ptr_member {
TEST_SUBMODULE(class_sh_unique_ptr_member, m) { TEST_SUBMODULE(class_sh_unique_ptr_member, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
py::classh<pointee>(m, "pointee").def(py::init<>()).def("get_int", &pointee::get_int); py::classh<pointee>(m, "pointee").def(py::init<>()).def("get_int", &pointee::get_int);
m.def("make_unique_pointee", make_unique_pointee); m.def("make_unique_pointee", make_unique_pointee);
@ -54,6 +60,7 @@ TEST_SUBMODULE(class_sh_unique_ptr_member, m) {
.def("is_owner", &ptr_owner::is_owner) .def("is_owner", &ptr_owner::is_owner)
.def("give_up_ownership_via_unique_ptr", &ptr_owner::give_up_ownership_via_unique_ptr) .def("give_up_ownership_via_unique_ptr", &ptr_owner::give_up_ownership_via_unique_ptr)
.def("give_up_ownership_via_shared_ptr", &ptr_owner::give_up_ownership_via_shared_ptr); .def("give_up_ownership_via_shared_ptr", &ptr_owner::give_up_ownership_via_shared_ptr);
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }
} // namespace class_sh_unique_ptr_member } // namespace class_sh_unique_ptr_member

View File

@ -4,6 +4,9 @@ import pytest
from pybind11_tests import class_sh_unique_ptr_member as m from pybind11_tests import class_sh_unique_ptr_member as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
def test_make_unique_pointee(): def test_make_unique_pointee():
obj = m.make_unique_pointee() obj = m.make_unique_pointee()

View File

@ -51,6 +51,12 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_virtual_py_cpp_mix::
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_virtual_py_cpp_mix::CppDerived) PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_virtual_py_cpp_mix::CppDerived)
TEST_SUBMODULE(class_sh_virtual_py_cpp_mix, m) { TEST_SUBMODULE(class_sh_virtual_py_cpp_mix, m) {
m.attr("defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT") =
#ifndef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
false;
#else
true;
using namespace pybind11_tests::class_sh_virtual_py_cpp_mix; using namespace pybind11_tests::class_sh_virtual_py_cpp_mix;
py::classh<Base, BaseVirtualOverrider>(m, "Base").def(py::init<>()).def("get", &Base::get); py::classh<Base, BaseVirtualOverrider>(m, "Base").def(py::init<>()).def("get", &Base::get);
@ -61,4 +67,5 @@ TEST_SUBMODULE(class_sh_virtual_py_cpp_mix, m) {
m.def("get_from_cpp_plainc_ptr", get_from_cpp_plainc_ptr, py::arg("b")); m.def("get_from_cpp_plainc_ptr", get_from_cpp_plainc_ptr, py::arg("b"));
m.def("get_from_cpp_unique_ptr", get_from_cpp_unique_ptr, py::arg("b")); m.def("get_from_cpp_unique_ptr", get_from_cpp_unique_ptr, py::arg("b"));
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
} }

View File

@ -4,6 +4,9 @@ import pytest
from pybind11_tests import class_sh_virtual_py_cpp_mix as m from pybind11_tests import class_sh_virtual_py_cpp_mix as m
if not m.defined_PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT:
pytest.skip("smart_holder not available.", allow_module_level=True)
class PyBase(m.Base): # Avoiding name PyDerived, for more systematic naming. class PyBase(m.Base): # Avoiding name PyDerived, for more systematic naming.
def __init__(self): def __init__(self):