mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
* Strictly enforce: trampoline must inherit from trampoline_self_life_support when used in combination with smart_holder
* Simplify test_class_sh_trampoline_basic.cpp,py (only one Abase is needed now)
* Replace obsolete sophisticated `throw value_error()` with a simple `assert()`
* Strictly enforce: trampoline should inherit from trampoline_self_life_support only if used in combination with smart_holder
* Resolve clang-tidy error
```
/__w/pybind11/pybind11/tests/test_class_sh_trampoline_basic.cpp:35:46: error: the parameter 'obj' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param,-warnings-as-errors]
35 | int AddInCppSharedPtr(std::shared_ptr<Abase> obj, int other_val) {
| ^
| const &
```
* Disable new static_assert if PYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE is defined.
93 lines
3.3 KiB
C++
93 lines
3.3 KiB
C++
// Copyright (c) 2021 The Pybind Development Team.
|
|
// All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
#include <utility>
|
|
|
|
namespace pybind11_tests {
|
|
namespace class_sh_trampoline_shared_ptr_cpp_arg {
|
|
|
|
// For testing whether a python subclass of a C++ object dies when the
|
|
// last python reference is lost
|
|
struct SpBase {
|
|
// returns true if the base virtual function is called
|
|
virtual bool is_base_used() { return true; }
|
|
|
|
// returns true if there's an associated python instance
|
|
bool has_python_instance() {
|
|
auto *tinfo = py::detail::get_type_info(typeid(SpBase));
|
|
return (bool) py::detail::get_object_handle(this, tinfo);
|
|
}
|
|
|
|
SpBase() = default;
|
|
SpBase(const SpBase &) = delete;
|
|
virtual ~SpBase() = default;
|
|
};
|
|
|
|
std::shared_ptr<SpBase> pass_through_shd_ptr(const std::shared_ptr<SpBase> &obj) { return obj; }
|
|
|
|
struct PySpBase : SpBase, py::trampoline_self_life_support {
|
|
using SpBase::SpBase;
|
|
bool is_base_used() override { PYBIND11_OVERRIDE(bool, SpBase, is_base_used); }
|
|
};
|
|
|
|
struct SpBaseTester {
|
|
std::shared_ptr<SpBase> get_object() const { return m_obj; }
|
|
void set_object(std::shared_ptr<SpBase> obj) { m_obj = std::move(obj); }
|
|
bool is_base_used() { return m_obj->is_base_used(); }
|
|
bool has_instance() { return (bool) m_obj; }
|
|
bool has_python_instance() { return m_obj && m_obj->has_python_instance(); }
|
|
void set_nonpython_instance() { m_obj = std::make_shared<SpBase>(); }
|
|
std::shared_ptr<SpBase> m_obj;
|
|
};
|
|
|
|
// For testing that a C++ class without an alias does not retain the python
|
|
// portion of the object
|
|
struct SpGoAway {};
|
|
|
|
struct SpGoAwayTester {
|
|
std::shared_ptr<SpGoAway> m_obj;
|
|
};
|
|
|
|
} // namespace class_sh_trampoline_shared_ptr_cpp_arg
|
|
} // namespace pybind11_tests
|
|
|
|
using namespace pybind11_tests::class_sh_trampoline_shared_ptr_cpp_arg;
|
|
|
|
TEST_SUBMODULE(class_sh_trampoline_shared_ptr_cpp_arg, m) {
|
|
// For testing whether a python subclass of a C++ object dies when the
|
|
// last python reference is lost
|
|
|
|
py::classh<SpBase, PySpBase>(m, "SpBase")
|
|
.def(py::init<>())
|
|
.def(py::init([](int) { return std::make_shared<PySpBase>(); }))
|
|
.def("is_base_used", &SpBase::is_base_used)
|
|
.def("has_python_instance", &SpBase::has_python_instance);
|
|
|
|
m.def("pass_through_shd_ptr", pass_through_shd_ptr);
|
|
m.def("pass_through_shd_ptr_release_gil",
|
|
pass_through_shd_ptr,
|
|
py::call_guard<py::gil_scoped_release>()); // PR #4196
|
|
|
|
py::classh<SpBaseTester>(m, "SpBaseTester")
|
|
.def(py::init<>())
|
|
.def("get_object", &SpBaseTester::get_object)
|
|
.def("set_object", &SpBaseTester::set_object)
|
|
.def("is_base_used", &SpBaseTester::is_base_used)
|
|
.def("has_instance", &SpBaseTester::has_instance)
|
|
.def("has_python_instance", &SpBaseTester::has_python_instance)
|
|
.def("set_nonpython_instance", &SpBaseTester::set_nonpython_instance)
|
|
.def_readwrite("obj", &SpBaseTester::m_obj);
|
|
|
|
// For testing that a C++ class without an alias does not retain the python
|
|
// portion of the object
|
|
|
|
py::classh<SpGoAway>(m, "SpGoAway").def(py::init<>());
|
|
|
|
py::classh<SpGoAwayTester>(m, "SpGoAwayTester")
|
|
.def(py::init<>())
|
|
.def_readwrite("obj", &SpGoAwayTester::m_obj);
|
|
}
|