mirror of
https://github.com/pybind/pybind11.git
synced 2025-02-27 08:04:56 +00:00
[smart_holder] Simplification: Enable smart_holder functionality unconditionally. (#5531)
* git merge --squash purge_internals_versions_4_5
* Remove PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT, set PYBIND11_INTERNALS_VERSION 7
* Remove all uses of PYBIND11_SMART_HOLDER_ENABLED under include/pybind11
* Remove obsolete PYBIND11_ACTUALLY_USING_SMART_HOLDER_AS_DEFAULT macro.
* Remove PYBIND11_SMART_HOLDER_ENABLED in ubench/holder_comparison.cpp
* Remove all uses of PYBIND11_SMART_HOLDER_ENABLED under tests/
* Remove `#define PYBIND11_SMART_HOLDER_ENABLED`
* Remove all uses of PYBIND11_SMART_HOLDER_TYPE_CASTERS under tests/
* Remove all uses of PYBIND11_TYPE_CASTER_BASE_HOLDER under tests/
* Add missing `#include <cstdint>`
Example error message (🐍 3.11 • ubuntu-latest • x64, GNU 13.3.0):
```
include/pybind11/detail/value_and_holder.h:56:52: error: ‘uint8_t’ is not a member of ‘std’; did you mean ‘wint_t’?
56 | inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_holder_constructed;
| ^~~~~~~
```
* Change PYBIND11_INTERNALS_VERSION to 106: It will be changed to 7 in a follow-on PR that actually changes the internals.
This commit is contained in:
parent
dc37cba85d
commit
5ab036bf08
14
.github/workflows/ci.yml
vendored
14
.github/workflows/ci.yml
vendored
@ -102,20 +102,6 @@ jobs:
|
||||
python: '3.12'
|
||||
args: >
|
||||
-DCMAKE_CXX_FLAGS="/DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT /GR /EHsc"
|
||||
# Exercise PYBIND11_SMART_HOLDER_DISABLE
|
||||
# with recent (or ideally latest) released Python version.
|
||||
- runs-on: ubuntu-latest
|
||||
python: '3.12'
|
||||
args: >
|
||||
-DCMAKE_CXX_FLAGS="-DPYBIND11_SMART_HOLDER_DISABLE"
|
||||
- runs-on: macos-13
|
||||
python: '3.12'
|
||||
args: >
|
||||
-DCMAKE_CXX_FLAGS="-DPYBIND11_SMART_HOLDER_DISABLE"
|
||||
- runs-on: windows-2022
|
||||
python: '3.12'
|
||||
args: >
|
||||
-DCMAKE_CXX_FLAGS="/DPYBIND11_SMART_HOLDER_DISABLE /GR /EHsc"
|
||||
exclude:
|
||||
# The setup-python action currently doesn't have graalpy for windows
|
||||
- python: 'graalpy-24.1'
|
||||
|
@ -339,9 +339,7 @@ struct type_record {
|
||||
/// Solves pybind/pybind11#1446
|
||||
bool release_gil_before_calling_cpp_dtor : 1;
|
||||
|
||||
#ifdef PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
|
||||
holder_enum_t holder_enum_v = holder_enum_t::undefined;
|
||||
#endif
|
||||
|
||||
PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *) ) {
|
||||
auto *base_info = detail::get_type_info(base, false);
|
||||
|
@ -836,8 +836,6 @@ protected:
|
||||
holder_type holder;
|
||||
};
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
template <typename, typename SFINAE = void>
|
||||
struct copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled : std::true_type {};
|
||||
|
||||
@ -968,8 +966,6 @@ protected:
|
||||
std::shared_ptr<type> shared_ptr_storage;
|
||||
};
|
||||
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
/// Specialize for the common std::shared_ptr, so users don't need to
|
||||
template <typename T>
|
||||
class type_caster<std::shared_ptr<T>> : public copyable_holder_caster<T, std::shared_ptr<T>> {};
|
||||
@ -990,8 +986,6 @@ struct move_only_holder_caster {
|
||||
static constexpr auto name = type_caster_base<type>::name;
|
||||
};
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
template <typename, typename SFINAE = void>
|
||||
struct move_only_holder_caster_unique_ptr_with_smart_holder_support_enabled : std::true_type {};
|
||||
|
||||
@ -1129,8 +1123,6 @@ public:
|
||||
std::shared_ptr<std::unique_ptr<type, deleter>> unique_ptr_storage;
|
||||
};
|
||||
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
template <typename type, typename deleter>
|
||||
class type_caster<std::unique_ptr<type, deleter>>
|
||||
: public move_only_holder_caster<type, std::unique_ptr<type, deleter>> {};
|
||||
@ -1169,10 +1161,8 @@ struct is_holder_type
|
||||
template <typename base, typename deleter>
|
||||
struct is_holder_type<base, std::unique_ptr<base, deleter>> : std::true_type {};
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
template <typename base>
|
||||
struct is_holder_type<base, smart_holder> : std::true_type {};
|
||||
#endif
|
||||
|
||||
#ifdef PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION // See PR #4888
|
||||
|
||||
|
@ -605,11 +605,8 @@ struct instance {
|
||||
bool simple_instance_registered : 1;
|
||||
/// If true, get_internals().patients has an entry for this object
|
||||
bool has_patients : 1;
|
||||
// Cannot use PYBIND11_INTERNALS_VERSION >= 106 here without refactoring.
|
||||
#if PYBIND11_VERSION_MAJOR >= 3
|
||||
/// If true, this Python object needs to be kept alive for the lifetime of the C++ value.
|
||||
bool is_alias : 1;
|
||||
#endif
|
||||
|
||||
/// Initializes all of the above type/values/holders data (but not the instance values
|
||||
/// themselves)
|
||||
|
@ -198,8 +198,6 @@ void construct(value_and_holder &v_h, Alias<Class> &&result, bool) {
|
||||
v_h.value_ptr() = new Alias<Class>(std::move(result));
|
||||
}
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
template <typename T, typename D>
|
||||
smart_holder init_smart_holder_from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr,
|
||||
bool void_cast_raw_ptr) {
|
||||
@ -268,8 +266,6 @@ void construct(value_and_holder &v_h,
|
||||
v_h.type->init_instance(v_h.inst, &smhldr);
|
||||
}
|
||||
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
// Implementing class for py::init<...>()
|
||||
template <typename... Args>
|
||||
struct constructor {
|
||||
|
@ -37,15 +37,11 @@
|
||||
/// further ABI-incompatible changes may be made before the ABI is officially
|
||||
/// changed to the new version.
|
||||
#ifndef PYBIND11_INTERNALS_VERSION
|
||||
# if PYBIND11_VERSION_MAJOR >= 3
|
||||
# define PYBIND11_INTERNALS_VERSION 106
|
||||
# else
|
||||
# define PYBIND11_INTERNALS_VERSION 6
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if PYBIND11_INTERNALS_VERSION < 6
|
||||
# error "PYBIND11_INTERNALS_VERSION 6 is the minimum for all platforms for pybind11v3."
|
||||
#if PYBIND11_INTERNALS_VERSION < 106
|
||||
# error "PYBIND11_INTERNALS_VERSION 106 is the minimum (SPECIAL SITUATION)."
|
||||
#endif
|
||||
|
||||
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
||||
@ -215,10 +211,6 @@ struct internals {
|
||||
}
|
||||
};
|
||||
|
||||
#if PYBIND11_INTERNALS_VERSION >= 106
|
||||
|
||||
# define PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
|
||||
|
||||
enum class holder_enum_t : uint8_t {
|
||||
undefined,
|
||||
std_unique_ptr, // Default, lacking interop with std::shared_ptr.
|
||||
@ -227,13 +219,6 @@ enum class holder_enum_t : uint8_t {
|
||||
custom_holder,
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT) \
|
||||
&& !defined(PYBIND11_SMART_HOLDER_DISABLE)
|
||||
# define PYBIND11_SMART_HOLDER_ENABLED
|
||||
#endif
|
||||
|
||||
/// Additional type information which does not fit into the PyTypeObject.
|
||||
/// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`.
|
||||
struct type_info {
|
||||
@ -262,9 +247,7 @@ struct type_info {
|
||||
bool default_holder : 1;
|
||||
/* true if this is a type registered with py::module_local */
|
||||
bool module_local : 1;
|
||||
#ifdef PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
|
||||
holder_enum_t holder_enum_v = holder_enum_t::undefined;
|
||||
#endif
|
||||
};
|
||||
|
||||
#define PYBIND11_INTERNALS_ID \
|
||||
|
@ -511,8 +511,6 @@ inline PyThreadState *get_thread_state_unchecked() {
|
||||
void keep_alive_impl(handle nurse, handle patient);
|
||||
inline PyObject *make_new_instance(PyTypeObject *type);
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
// PYBIND11:REMINDER: Needs refactoring of existing pybind11 code.
|
||||
inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo);
|
||||
|
||||
@ -868,8 +866,6 @@ struct load_helper : value_and_holder_helper {
|
||||
|
||||
PYBIND11_NAMESPACE_END(smart_holder_type_caster_support)
|
||||
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
class type_caster_generic {
|
||||
public:
|
||||
PYBIND11_NOINLINE explicit type_caster_generic(const std::type_info &type_info)
|
||||
@ -974,7 +970,6 @@ public:
|
||||
|
||||
// Base methods for generic caster; there are overridden in copyable_holder_caster
|
||||
void load_value(value_and_holder &&v_h) {
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) {
|
||||
smart_holder_type_caster_support::value_and_holder_helper v_h_helper;
|
||||
v_h_helper.loaded_v_h = v_h;
|
||||
@ -984,7 +979,6 @@ public:
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
auto *&vptr = v_h.value_ptr();
|
||||
// Lazy allocation for unallocated values:
|
||||
if (vptr == nullptr) {
|
||||
|
@ -5,29 +5,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
#include "internals.h"
|
||||
#include "struct_smart_holder.h"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
# include "struct_smart_holder.h"
|
||||
#endif
|
||||
|
||||
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
using pybindit::memory::smart_holder;
|
||||
#endif
|
||||
|
||||
PYBIND11_NAMESPACE_BEGIN(detail)
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
template <typename H>
|
||||
using is_smart_holder = std::is_same<H, smart_holder>;
|
||||
#else
|
||||
template <typename>
|
||||
struct is_smart_holder : std::false_type {};
|
||||
#endif
|
||||
|
||||
PYBIND11_NAMESPACE_END(detail)
|
||||
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include "common.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <typeinfo>
|
||||
|
||||
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
||||
|
@ -1448,9 +1448,7 @@ protected:
|
||||
tinfo->simple_ancestors = true;
|
||||
tinfo->default_holder = rec.default_holder;
|
||||
tinfo->module_local = rec.module_local;
|
||||
#ifdef PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
|
||||
tinfo->holder_enum_v = rec.holder_enum_v;
|
||||
#endif
|
||||
|
||||
with_internals([&](internals &internals) {
|
||||
auto tindex = std::type_index(*rec.type);
|
||||
@ -1665,8 +1663,6 @@ PYBIND11_NAMESPACE_END(detail)
|
||||
template <typename T, typename D, typename SFINAE = void>
|
||||
struct property_cpp_function : detail::property_cpp_function_classic<T, D> {};
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
PYBIND11_NAMESPACE_BEGIN(detail)
|
||||
|
||||
template <typename T, typename D, typename SFINAE = void>
|
||||
@ -1844,9 +1840,7 @@ struct property_cpp_function<
|
||||
detail::both_t_and_d_use_type_caster_base<T, typename D::element_type>>::value>>
|
||||
: detail::property_cpp_function_sh_unique_ptr_member<T, D> {};
|
||||
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
#if defined(PYBIND11_USE_SMART_HOLDER_AS_DEFAULT) && defined(PYBIND11_SMART_HOLDER_ENABLED)
|
||||
#if defined(PYBIND11_USE_SMART_HOLDER_AS_DEFAULT)
|
||||
// NOTE: THIS IS MEANT FOR STRESS-TESTING ONLY!
|
||||
// As of PR #5257, for production use, there is no longer a strong reason to make
|
||||
// smart_holder the default holder:
|
||||
@ -1854,7 +1848,6 @@ struct property_cpp_function<
|
||||
// Running the pybind11 unit tests with smart_holder as the default holder is to ensure
|
||||
// that `py::smart_holder` / `py::classh` is backward-compatible with all pre-existing
|
||||
// functionality.
|
||||
# define PYBIND11_ACTUALLY_USING_SMART_HOLDER_AS_DEFAULT
|
||||
template <typename>
|
||||
using default_holder_type = smart_holder;
|
||||
#else
|
||||
@ -1913,7 +1906,6 @@ public:
|
||||
// A more fitting name would be uses_unique_ptr_holder.
|
||||
record.default_holder = detail::is_instantiation<std::unique_ptr, holder_type>::value;
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
if (detail::is_instantiation<std::unique_ptr, holder_type>::value) {
|
||||
record.holder_enum_v = detail::holder_enum_t::std_unique_ptr;
|
||||
} else if (detail::is_instantiation<std::shared_ptr, holder_type>::value) {
|
||||
@ -1923,7 +1915,6 @@ public:
|
||||
} else {
|
||||
record.holder_enum_v = detail::holder_enum_t::custom_holder;
|
||||
}
|
||||
#endif
|
||||
|
||||
set_operator_new<type>(&record);
|
||||
|
||||
@ -2265,8 +2256,6 @@ private:
|
||||
init_holder(inst, v_h, (const holder_type *) holder_ptr, v_h.value_ptr<type>());
|
||||
}
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
template <typename WrappedType>
|
||||
static bool try_initialization_using_shared_from_this(holder_type *, WrappedType *, ...) {
|
||||
return false;
|
||||
@ -2326,8 +2315,6 @@ private:
|
||||
v_h.set_holder_constructed();
|
||||
}
|
||||
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
// Deallocates an instance; via holder, if constructed; otherwise via operator delete.
|
||||
// NOTE: The Python error indicator needs to cleared BEFORE this function is called.
|
||||
// This is because we could be deallocating while cleaning up after a Python exception.
|
||||
@ -2393,8 +2380,6 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
// Supports easier switching between py::class_<T> and py::class_<T, py::smart_holder>:
|
||||
// users can simply replace the `_` in `class_` with `h` or vice versa.
|
||||
template <typename type_, typename... options>
|
||||
@ -2403,8 +2388,6 @@ public:
|
||||
using class_<type_, smart_holder, options...>::class_;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
/// Binds an existing constructor taking arguments Args...
|
||||
template <typename... Args>
|
||||
detail::initimpl::constructor<Args...> init() {
|
||||
|
@ -4,10 +4,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "detail/internals.h"
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
#include "detail/common.h"
|
||||
#include "detail/using_smart_holder.h"
|
||||
#include "detail/value_and_holder.h"
|
||||
@ -62,5 +58,3 @@ struct trampoline_self_life_support {
|
||||
};
|
||||
|
||||
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|
||||
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
@ -109,11 +109,7 @@ TEST_SUBMODULE(class_, m) {
|
||||
struct ToBeHeldByUniquePtr {};
|
||||
py::class_<ToBeHeldByUniquePtr, std::unique_ptr<ToBeHeldByUniquePtr>>(m, "ToBeHeldByUniquePtr")
|
||||
.def(py::init<>());
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
m.def("pass_unique_ptr", [](std::unique_ptr<ToBeHeldByUniquePtr> &&) {});
|
||||
#else
|
||||
m.attr("pass_unique_ptr") = py::none();
|
||||
#endif
|
||||
|
||||
// test_inheritance
|
||||
class Pet {
|
||||
@ -636,7 +632,7 @@ CHECK_NOALIAS(8);
|
||||
CHECK_HOLDER(1, unique);
|
||||
CHECK_HOLDER(2, unique);
|
||||
CHECK_HOLDER(3, unique);
|
||||
#ifndef PYBIND11_ACTUALLY_USING_SMART_HOLDER_AS_DEFAULT
|
||||
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
|
||||
CHECK_HOLDER(4, unique);
|
||||
CHECK_HOLDER(5, unique);
|
||||
#endif
|
||||
|
@ -51,8 +51,6 @@ def test_instance_new():
|
||||
|
||||
def test_pass_unique_ptr():
|
||||
obj = m.ToBeHeldByUniquePtr()
|
||||
if m.pass_unique_ptr is None:
|
||||
pytest.skip("smart_holder not available.")
|
||||
with pytest.raises(RuntimeError) as execinfo:
|
||||
m.pass_unique_ptr(obj)
|
||||
assert str(execinfo.value).startswith(
|
||||
|
@ -144,24 +144,7 @@ class LocalUnusualOpRef : UnusualOpRef {}; // To avoid clashing with `py::class_
|
||||
py::object CastUnusualOpRefConstRef(const LocalUnusualOpRef &cref) { return py::cast(cref); }
|
||||
py::object CastUnusualOpRefMovable(LocalUnusualOpRef &&mvbl) { return py::cast(std::move(mvbl)); }
|
||||
|
||||
} // namespace class_sh_basic
|
||||
} // namespace pybind11_tests
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_basic::atyp)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_basic::uconsumer)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_basic::SharedPtrStash)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_basic::LocalUnusualOpRef)
|
||||
|
||||
namespace pybind11_tests {
|
||||
namespace class_sh_basic {
|
||||
|
||||
TEST_SUBMODULE(class_sh_basic, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
py::classh<atyp>(m, "atyp").def(py::init<>()).def(py::init([](const std::string &mtxt) {
|
||||
@ -260,7 +243,6 @@ TEST_SUBMODULE(class_sh_basic, m) {
|
||||
[]() { return CastUnusualOpRefConstRef(LocalUnusualOpRef()); });
|
||||
m.def("CallCastUnusualOpRefMovable",
|
||||
[]() { return CastUnusualOpRefMovable(LocalUnusualOpRef()); });
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
||||
} // namespace class_sh_basic
|
||||
|
@ -7,9 +7,6 @@ import pytest
|
||||
|
||||
from pybind11_tests import class_sh_basic as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
def test_atyp_constructors():
|
||||
obj = m.atyp()
|
||||
|
@ -28,16 +28,7 @@ int overloaded(std::unique_ptr<Atype<2>> at2, int i) { return at2->get() * 40 +
|
||||
} // namespace class_sh_disowning
|
||||
} // namespace pybind11_tests
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning::Atype<1>)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning::Atype<2>)
|
||||
|
||||
TEST_SUBMODULE(class_sh_disowning, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
using namespace pybind11_tests::class_sh_disowning;
|
||||
|
||||
py::classh<Atype<1>>(m, "Atype1").def(py::init<int>()).def("get", &Atype<1>::get);
|
||||
@ -49,5 +40,4 @@ 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<2>>, int)) &overloaded);
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
@ -4,9 +4,6 @@ import pytest
|
||||
|
||||
from pybind11_tests import class_sh_disowning as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
def is_disowned(obj):
|
||||
try:
|
||||
|
@ -48,21 +48,7 @@ int disown_base2(std::unique_ptr<Base2> b2) { return b2->j * 2000 + 2; }
|
||||
} // namespace class_sh_disowning_mi
|
||||
} // namespace pybind11_tests
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning_mi::B)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning_mi::C0)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning_mi::C1)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning_mi::D)
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning_mi::Base1)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_disowning_mi::Base2)
|
||||
|
||||
TEST_SUBMODULE(class_sh_disowning_mi, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
using namespace pybind11_tests::class_sh_disowning_mi;
|
||||
|
||||
py::classh<B>(m, "B")
|
||||
@ -98,5 +84,4 @@ TEST_SUBMODULE(class_sh_disowning_mi, m) {
|
||||
py::classh<Base2>(m, "Base2").def(py::init<int>()).def("bar", &Base2::bar);
|
||||
m.def("disown_base1", disown_base1);
|
||||
m.def("disown_base2", disown_base2);
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
@ -5,9 +5,6 @@ import pytest
|
||||
import env # noqa: F401
|
||||
from pybind11_tests import class_sh_disowning_mi as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
def test_diamond_inheritance():
|
||||
# Very similar to test_multiple_inheritance.py:test_diamond_inheritance.
|
||||
|
@ -72,27 +72,7 @@ struct sddwaa : std::default_delete<with_alias_alias> {};
|
||||
} // namespace class_sh_factory_constructors
|
||||
} // namespace pybind11_tests
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::atyp_valu)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::atyp_rref)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::atyp_cref)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::atyp_mref)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::atyp_cptr)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::atyp_mptr)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::atyp_shmp)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::atyp_shcp)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::atyp_uqmp)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::atyp_uqcp)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::atyp_udmp)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::atyp_udcp)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_factory_constructors::with_alias)
|
||||
|
||||
TEST_SUBMODULE(class_sh_factory_constructors, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
using namespace pybind11_tests::class_sh_factory_constructors;
|
||||
|
||||
py::classh<atyp_valu>(m, "atyp_valu")
|
||||
@ -183,5 +163,4 @@ TEST_SUBMODULE(class_sh_factory_constructors, m) {
|
||||
[](int, int, int, int, int) {
|
||||
return std::make_shared<with_alias>(); // Invalid alias factory.
|
||||
}));
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
@ -4,9 +4,6 @@ import pytest
|
||||
|
||||
from pybind11_tests import class_sh_factory_constructors as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
def test_atyp_factories():
|
||||
assert m.atyp_valu().get_mtxt() == "Valu"
|
||||
|
@ -59,26 +59,7 @@ inline int pass_cptr_base2(base2 const *b) { return b->id() + 22; }
|
||||
inline int pass_cptr_drvd2(drvd2 const *d) { return d->id() + 23; }
|
||||
// clang-format on
|
||||
|
||||
} // namespace class_sh_inheritance
|
||||
} // namespace pybind11_tests
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_inheritance::base)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_inheritance::drvd)
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_inheritance::base1)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_inheritance::base2)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_inheritance::drvd2)
|
||||
|
||||
namespace pybind11_tests {
|
||||
namespace class_sh_inheritance {
|
||||
|
||||
TEST_SUBMODULE(class_sh_inheritance, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
py::classh<base>(m, "base");
|
||||
py::classh<drvd, base>(m, "drvd");
|
||||
|
||||
@ -105,7 +86,6 @@ TEST_SUBMODULE(class_sh_inheritance, m) {
|
||||
m.def("pass_cptr_base1", pass_cptr_base1);
|
||||
m.def("pass_cptr_base2", pass_cptr_base2);
|
||||
m.def("pass_cptr_drvd2", pass_cptr_drvd2);
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
||||
} // namespace class_sh_inheritance
|
||||
|
@ -1,12 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from pybind11_tests import class_sh_inheritance as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
def test_rtrn_mptr_drvd_pass_cptr_base():
|
||||
d = m.rtrn_mptr_drvd()
|
||||
|
@ -35,17 +35,7 @@ struct Derived : Base1, Base0 {
|
||||
|
||||
} // namespace test_class_sh_mi_thunks
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_mi_thunks::Base0)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_mi_thunks::Base1)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_mi_thunks::Derived)
|
||||
|
||||
TEST_SUBMODULE(class_sh_mi_thunks, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
using namespace test_class_sh_mi_thunks;
|
||||
|
||||
m.def("ptrdiff_drvd_base0", []() {
|
||||
@ -103,5 +93,4 @@ TEST_SUBMODULE(class_sh_mi_thunks, m) {
|
||||
}
|
||||
return obj_der->vec.size();
|
||||
});
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
@ -4,9 +4,6 @@ import pytest
|
||||
|
||||
from pybind11_tests import class_sh_mi_thunks as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
def test_ptrdiff_drvd_base0():
|
||||
ptrdiff = m.ptrdiff_drvd_base0()
|
||||
|
@ -46,24 +46,7 @@ struct WithConstCharPtrMember {
|
||||
|
||||
} // namespace test_class_sh_property
|
||||
|
||||
PYBIND11_TYPE_CASTER_BASE_HOLDER(test_class_sh_property::ClassicField,
|
||||
std::unique_ptr<test_class_sh_property::ClassicField>)
|
||||
PYBIND11_TYPE_CASTER_BASE_HOLDER(test_class_sh_property::ClassicOuter,
|
||||
std::unique_ptr<test_class_sh_property::ClassicOuter>)
|
||||
|
||||
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::WithCharArrayMember)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_property::WithConstCharPtrMember)
|
||||
|
||||
TEST_SUBMODULE(class_sh_property, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
using namespace test_class_sh_property;
|
||||
|
||||
py::class_<ClassicField, std::unique_ptr<ClassicField>>(m, "ClassicField")
|
||||
@ -109,5 +92,4 @@ TEST_SUBMODULE(class_sh_property, m) {
|
||||
py::classh<WithConstCharPtrMember>(m, "WithConstCharPtrMember")
|
||||
.def(py::init<>())
|
||||
.def_readonly("const_char_ptr_member", &WithConstCharPtrMember::const_char_ptr_member);
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
@ -7,9 +7,6 @@ import pytest
|
||||
import env # noqa: F401
|
||||
from pybind11_tests import class_sh_property as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
"env.PYPY or env.GRAALPY", reason="gc after `del field` is apparently deferred"
|
||||
|
@ -46,17 +46,7 @@ public:
|
||||
|
||||
using namespace test_class_sh_property_non_owning;
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(CoreField)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(DataField)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(DataFieldsHolder)
|
||||
|
||||
TEST_SUBMODULE(class_sh_property_non_owning, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
py::classh<CoreField>(m, "CoreField").def_readwrite("int_value", &CoreField::int_value);
|
||||
|
||||
py::classh<DataField>(m, "DataField")
|
||||
@ -71,5 +61,4 @@ TEST_SUBMODULE(class_sh_property_non_owning, m) {
|
||||
py::classh<DataFieldsHolder>(m, "DataFieldsHolder")
|
||||
.def(py::init<std::size_t>())
|
||||
.def("vec_at", &DataFieldsHolder::vec_at, py::return_value_policy::reference_internal);
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
@ -4,9 +4,6 @@ import pytest
|
||||
|
||||
from pybind11_tests import class_sh_property_non_owning as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("persistent_holder", [True, False])
|
||||
@pytest.mark.parametrize(
|
||||
|
@ -41,21 +41,8 @@ struct Outer {
|
||||
};
|
||||
|
||||
} // namespace
|
||||
} // namespace pybind11_tests
|
||||
|
||||
PYBIND11_TYPE_CASTER_BASE_HOLDER(pybind11_tests::FooShPtr,
|
||||
std::shared_ptr<pybind11_tests::FooShPtr>)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::FooSmHld)
|
||||
|
||||
namespace pybind11_tests {
|
||||
|
||||
TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
py::class_<FooShPtr, std::shared_ptr<FooShPtr>>(m, "FooShPtr")
|
||||
@ -113,7 +100,6 @@ TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) {
|
||||
l.append(std::move(o));
|
||||
return l;
|
||||
});
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
||||
} // namespace pybind11_tests
|
||||
|
@ -1,12 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from pybind11_tests import class_sh_shared_ptr_copy_move as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
def test_shptr_copy():
|
||||
txt = m.test_ShPtr_copy()[0].get_history()
|
||||
|
@ -34,7 +34,6 @@ struct AbaseAlias : Abase<SerNo> {
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
template <>
|
||||
struct AbaseAlias<1> : Abase<1>, py::trampoline_self_life_support {
|
||||
using Abase<1>::Abase;
|
||||
@ -46,7 +45,6 @@ struct AbaseAlias<1> : Abase<1>, py::trampoline_self_life_support {
|
||||
other_val);
|
||||
}
|
||||
};
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
template <int SerNo>
|
||||
int AddInCppRawPtr(const Abase<SerNo> *obj, int other_val) {
|
||||
@ -63,7 +61,6 @@ int AddInCppUniquePtr(std::unique_ptr<Abase<SerNo>> obj, int other_val) {
|
||||
return obj->Add(other_val) * 100 + 13;
|
||||
}
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
template <int SerNo>
|
||||
void wrap(py::module_ m, const char *py_class_name) {
|
||||
py::classh<Abase<SerNo>, AbaseAlias<SerNo>>(m, py_class_name)
|
||||
@ -75,24 +72,13 @@ void wrap(py::module_ m, const char *py_class_name) {
|
||||
m.def("AddInCppSharedPtr", AddInCppSharedPtr<SerNo>, py::arg("obj"), py::arg("other_val"));
|
||||
m.def("AddInCppUniquePtr", AddInCppUniquePtr<SerNo>, py::arg("obj"), py::arg("other_val"));
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace class_sh_trampoline_basic
|
||||
} // namespace pybind11_tests
|
||||
|
||||
using namespace pybind11_tests::class_sh_trampoline_basic;
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Abase<0>)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Abase<1>)
|
||||
|
||||
TEST_SUBMODULE(class_sh_trampoline_basic, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
wrap<0>(m, "Abase0");
|
||||
wrap<1>(m, "Abase1");
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
@ -4,9 +4,6 @@ import pytest
|
||||
|
||||
from pybind11_tests import class_sh_trampoline_basic as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
class PyDrvd0(m.Abase0):
|
||||
def __init__(self, val):
|
||||
|
@ -38,26 +38,16 @@ protected:
|
||||
Big5() : history{"DefaultConstructor"} {}
|
||||
};
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
struct Big5Trampoline : Big5, py::trampoline_self_life_support {
|
||||
using Big5::Big5;
|
||||
};
|
||||
#endif
|
||||
|
||||
} // namespace class_sh_trampoline_self_life_support
|
||||
} // namespace pybind11_tests
|
||||
|
||||
using namespace pybind11_tests::class_sh_trampoline_self_life_support;
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Big5)
|
||||
|
||||
TEST_SUBMODULE(class_sh_trampoline_self_life_support, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
py::classh<Big5, Big5Trampoline>(m, "Big5")
|
||||
.def(py::init<std::string>())
|
||||
.def_readonly("history", &Big5::history);
|
||||
@ -94,5 +84,4 @@ TEST_SUBMODULE(class_sh_trampoline_self_life_support, m) {
|
||||
py::object o1 = py::cast(std::move(obj));
|
||||
return py::make_tuple(o1, o2);
|
||||
});
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
@ -4,9 +4,6 @@ import pytest
|
||||
|
||||
import pybind11_tests.class_sh_trampoline_self_life_support as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
class PyBig5(m.Big5):
|
||||
pass
|
||||
|
@ -71,11 +71,9 @@ struct SftSharedPtrStash {
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
struct SftTrampoline : Sft, py::trampoline_self_life_support {
|
||||
using Sft::Sft;
|
||||
};
|
||||
#endif
|
||||
|
||||
long use_count(const std::shared_ptr<Sft> &obj) { return obj.use_count(); }
|
||||
|
||||
@ -111,16 +109,7 @@ std::shared_ptr<Sft> pass_through_shd_ptr(const std::shared_ptr<Sft> &obj) { ret
|
||||
|
||||
using namespace pybind11_tests::class_sh_trampoline_shared_from_this;
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Sft)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(SftSharedPtrStash)
|
||||
|
||||
TEST_SUBMODULE(class_sh_trampoline_shared_from_this, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
py::classh<Sft, SftTrampoline>(m, "Sft")
|
||||
.def(py::init<const std::string &>())
|
||||
.def(py::init([](const std::string &history, int) {
|
||||
@ -146,5 +135,4 @@ 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_shd_ptr", make_pure_cpp_sft_shd_ptr);
|
||||
m.def("pass_through_shd_ptr", pass_through_shd_ptr);
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
@ -8,9 +8,6 @@ import pytest
|
||||
import env
|
||||
import pybind11_tests.class_sh_trampoline_shared_from_this as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
class PySft(m.Sft):
|
||||
pass
|
||||
|
@ -57,18 +57,7 @@ struct SpGoAwayTester {
|
||||
|
||||
using namespace pybind11_tests::class_sh_trampoline_shared_ptr_cpp_arg;
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpBase)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpBaseTester)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpGoAway)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(SpGoAwayTester)
|
||||
|
||||
TEST_SUBMODULE(class_sh_trampoline_shared_ptr_cpp_arg, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
// For testing whether a python subclass of a C++ object dies when the
|
||||
// last python reference is lost
|
||||
|
||||
@ -101,5 +90,4 @@ TEST_SUBMODULE(class_sh_trampoline_shared_ptr_cpp_arg, m) {
|
||||
py::classh<SpGoAwayTester>(m, "SpGoAwayTester")
|
||||
.def(py::init<>())
|
||||
.def_readwrite("obj", &SpGoAwayTester::m_obj);
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
@ -5,9 +5,6 @@ import pytest
|
||||
import env # noqa: F401
|
||||
import pybind11_tests.class_sh_trampoline_shared_ptr_cpp_arg as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC")
|
||||
def test_shared_ptr_cpp_arg():
|
||||
|
@ -34,12 +34,9 @@ private:
|
||||
} // namespace class_sh_trampoline_unique_ptr
|
||||
} // namespace pybind11_tests
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_trampoline_unique_ptr::Class)
|
||||
|
||||
namespace pybind11_tests {
|
||||
namespace class_sh_trampoline_unique_ptr {
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
class PyClass : public Class, public py::trampoline_self_life_support {
|
||||
public:
|
||||
std::unique_ptr<Class> clone() const override {
|
||||
@ -48,18 +45,11 @@ public:
|
||||
|
||||
int foo() const override { PYBIND11_OVERRIDE_PURE(int, Class, foo); }
|
||||
};
|
||||
#endif
|
||||
|
||||
} // namespace class_sh_trampoline_unique_ptr
|
||||
} // namespace pybind11_tests
|
||||
|
||||
TEST_SUBMODULE(class_sh_trampoline_unique_ptr, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
using namespace pybind11_tests::class_sh_trampoline_unique_ptr;
|
||||
|
||||
py::classh<Class, PyClass>(m, "Class")
|
||||
@ -71,5 +61,4 @@ TEST_SUBMODULE(class_sh_trampoline_unique_ptr, m) {
|
||||
|
||||
m.def("clone", [](const Class &obj) { return obj.clone(); });
|
||||
m.def("clone_and_foo", [](const Class &obj) { return obj.clone()->foo(); });
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
@ -1,12 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
import pybind11_tests.class_sh_trampoline_unique_ptr as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
class MyClass(m.Class):
|
||||
def foo(self):
|
||||
|
@ -22,25 +22,10 @@ private:
|
||||
explicit Pet(const std::string &name) : name(name) {}
|
||||
};
|
||||
|
||||
} // namespace class_sh_unique_ptr_custom_deleter
|
||||
} // namespace pybind11_tests
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_unique_ptr_custom_deleter::Pet)
|
||||
|
||||
namespace pybind11_tests {
|
||||
namespace class_sh_unique_ptr_custom_deleter {
|
||||
|
||||
TEST_SUBMODULE(class_sh_unique_ptr_custom_deleter, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
py::classh<Pet>(m, "Pet").def_readwrite("name", &Pet::name);
|
||||
|
||||
m.def("create", &Pet::New);
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
||||
} // namespace class_sh_unique_ptr_custom_deleter
|
||||
|
@ -1,12 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from pybind11_tests import class_sh_unique_ptr_custom_deleter as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
def test_create():
|
||||
pet = m.create("abc")
|
||||
|
@ -36,21 +36,7 @@ private:
|
||||
std::unique_ptr<pointee> ptr_;
|
||||
};
|
||||
|
||||
} // namespace class_sh_unique_ptr_member
|
||||
} // namespace pybind11_tests
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_unique_ptr_member::pointee)
|
||||
|
||||
namespace pybind11_tests {
|
||||
namespace class_sh_unique_ptr_member {
|
||||
|
||||
TEST_SUBMODULE(class_sh_unique_ptr_member, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
py::classh<pointee>(m, "pointee").def(py::init<>()).def("get_int", &pointee::get_int);
|
||||
|
||||
m.def("make_unique_pointee", make_unique_pointee);
|
||||
@ -60,7 +46,6 @@ TEST_SUBMODULE(class_sh_unique_ptr_member, m) {
|
||||
.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_shared_ptr", &ptr_owner::give_up_ownership_via_shared_ptr);
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
||||
} // namespace class_sh_unique_ptr_member
|
||||
|
@ -4,9 +4,6 @@ import pytest
|
||||
|
||||
from pybind11_tests import class_sh_unique_ptr_member as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
def test_make_unique_pointee():
|
||||
obj = m.make_unique_pointee()
|
||||
|
@ -31,8 +31,6 @@ int get_from_cpp_plainc_ptr(const Base *b) { return b->get() + 4000; }
|
||||
|
||||
int get_from_cpp_unique_ptr(std::unique_ptr<Base> b) { return b->get() + 5000; }
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
struct BaseVirtualOverrider : Base, py::trampoline_self_life_support {
|
||||
using Base::Base;
|
||||
|
||||
@ -45,24 +43,12 @@ struct CppDerivedVirtualOverrider : CppDerived, py::trampoline_self_life_support
|
||||
int get() const override { PYBIND11_OVERRIDE(int, CppDerived, get); }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace class_sh_virtual_py_cpp_mix
|
||||
} // namespace pybind11_tests
|
||||
|
||||
using namespace pybind11_tests::class_sh_virtual_py_cpp_mix;
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Base)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(CppDerivedPlain)
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(CppDerived)
|
||||
|
||||
TEST_SUBMODULE(class_sh_virtual_py_cpp_mix, m) {
|
||||
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
||||
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
||||
false;
|
||||
#else
|
||||
true;
|
||||
|
||||
py::classh<Base, BaseVirtualOverrider>(m, "Base").def(py::init<>()).def("get", &Base::get);
|
||||
|
||||
py::classh<CppDerivedPlain, Base>(m, "CppDerivedPlain").def(py::init<>());
|
||||
@ -71,5 +57,4 @@ 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_unique_ptr", get_from_cpp_unique_ptr, py::arg("b"));
|
||||
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
||||
}
|
||||
|
@ -4,9 +4,6 @@ import pytest
|
||||
|
||||
from pybind11_tests import class_sh_virtual_py_cpp_mix as m
|
||||
|
||||
if not m.defined_PYBIND11_SMART_HOLDER_ENABLED:
|
||||
pytest.skip("smart_holder not available.", allow_module_level=True)
|
||||
|
||||
|
||||
class PyBase(m.Base): # Avoiding name PyDerived, for more systematic naming.
|
||||
def __init__(self):
|
||||
|
@ -22,8 +22,6 @@ void wrap_number_bucket(py::module m, const char *class_name) {
|
||||
.def("add", &WrappedType::add, py::arg("other"));
|
||||
}
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
|
||||
template <typename T>
|
||||
class padded_unique_ptr {
|
||||
std::unique_ptr<T> ptr;
|
||||
@ -37,21 +35,15 @@ public:
|
||||
static_assert(sizeof(padded_unique_ptr<nb_pu>) == sizeof(py::smart_holder),
|
||||
"Unexpected sizeof mismatch.");
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace hc
|
||||
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
PYBIND11_DECLARE_HOLDER_TYPE(T, hc::padded_unique_ptr<T>);
|
||||
#endif
|
||||
|
||||
PYBIND11_MODULE(pybind11_ubench_holder_comparison, m) {
|
||||
using namespace hc;
|
||||
wrap_number_bucket<nb_up, std::unique_ptr<nb_up>>(m, "number_bucket_up");
|
||||
wrap_number_bucket<nb_sp, std::shared_ptr<nb_sp>>(m, "number_bucket_sp");
|
||||
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
||||
m.def("sizeof_smart_holder", []() { return sizeof(py::smart_holder); });
|
||||
wrap_number_bucket<nb_pu, padded_unique_ptr<nb_pu>>(m, "number_bucket_pu");
|
||||
wrap_number_bucket<nb_sh, py::smart_holder>(m, "number_bucket_sh");
|
||||
#endif
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user