[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:
Ralf W. Grosse-Kunstleve 2025-02-22 10:22:20 -08:00 committed by GitHub
parent dc37cba85d
commit 5ab036bf08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
48 changed files with 33 additions and 438 deletions

View File

@ -102,20 +102,6 @@ jobs:
python: '3.12' python: '3.12'
args: > args: >
-DCMAKE_CXX_FLAGS="/DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT /GR /EHsc" -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: exclude:
# The setup-python action currently doesn't have graalpy for windows # The setup-python action currently doesn't have graalpy for windows
- python: 'graalpy-24.1' - python: 'graalpy-24.1'

View File

@ -339,9 +339,7 @@ struct type_record {
/// Solves pybind/pybind11#1446 /// Solves pybind/pybind11#1446
bool release_gil_before_calling_cpp_dtor : 1; 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; holder_enum_t holder_enum_v = holder_enum_t::undefined;
#endif
PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *) ) { PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *) ) {
auto *base_info = detail::get_type_info(base, false); auto *base_info = detail::get_type_info(base, false);

View File

@ -836,8 +836,6 @@ protected:
holder_type holder; holder_type holder;
}; };
#ifdef PYBIND11_SMART_HOLDER_ENABLED
template <typename, typename SFINAE = void> template <typename, typename SFINAE = void>
struct copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled : std::true_type {}; struct copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled : std::true_type {};
@ -928,13 +926,13 @@ protected:
return; return;
} }
throw cast_error("Unable to cast from non-held to held instance (T& to Holder<T>) " throw cast_error("Unable to cast from non-held to held instance (T& to Holder<T>) "
# if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
"(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for " "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for "
"type information)"); "type information)");
# else #else
"of type '" "of type '"
+ type_id<std::shared_ptr<type>>() + "''"); + type_id<std::shared_ptr<type>>() + "''");
# endif #endif
} }
template <typename T = std::shared_ptr<type>, template <typename T = std::shared_ptr<type>,
@ -968,8 +966,6 @@ protected:
std::shared_ptr<type> shared_ptr_storage; 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 /// Specialize for the common std::shared_ptr, so users don't need to
template <typename T> template <typename T>
class type_caster<std::shared_ptr<T>> : public copyable_holder_caster<T, std::shared_ptr<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; static constexpr auto name = type_caster_base<type>::name;
}; };
#ifdef PYBIND11_SMART_HOLDER_ENABLED
template <typename, typename SFINAE = void> template <typename, typename SFINAE = void>
struct move_only_holder_caster_unique_ptr_with_smart_holder_support_enabled : std::true_type {}; 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; std::shared_ptr<std::unique_ptr<type, deleter>> unique_ptr_storage;
}; };
#endif // PYBIND11_SMART_HOLDER_ENABLED
template <typename type, typename deleter> template <typename type, typename deleter>
class type_caster<std::unique_ptr<type, deleter>> class type_caster<std::unique_ptr<type, deleter>>
: public move_only_holder_caster<type, 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> template <typename base, typename deleter>
struct is_holder_type<base, std::unique_ptr<base, deleter>> : std::true_type {}; struct is_holder_type<base, std::unique_ptr<base, deleter>> : std::true_type {};
#ifdef PYBIND11_SMART_HOLDER_ENABLED
template <typename base> template <typename base>
struct is_holder_type<base, smart_holder> : std::true_type {}; struct is_holder_type<base, smart_holder> : std::true_type {};
#endif
#ifdef PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION // See PR #4888 #ifdef PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION // See PR #4888

View File

@ -605,11 +605,8 @@ struct instance {
bool simple_instance_registered : 1; bool simple_instance_registered : 1;
/// If true, get_internals().patients has an entry for this object /// If true, get_internals().patients has an entry for this object
bool has_patients : 1; 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. /// If true, this Python object needs to be kept alive for the lifetime of the C++ value.
bool is_alias : 1; bool is_alias : 1;
#endif
/// Initializes all of the above type/values/holders data (but not the instance values /// Initializes all of the above type/values/holders data (but not the instance values
/// themselves) /// themselves)

View File

@ -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)); v_h.value_ptr() = new Alias<Class>(std::move(result));
} }
#ifdef PYBIND11_SMART_HOLDER_ENABLED
template <typename T, typename D> template <typename T, typename D>
smart_holder init_smart_holder_from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr, smart_holder init_smart_holder_from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr,
bool void_cast_raw_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); v_h.type->init_instance(v_h.inst, &smhldr);
} }
#endif // PYBIND11_SMART_HOLDER_ENABLED
// Implementing class for py::init<...>() // Implementing class for py::init<...>()
template <typename... Args> template <typename... Args>
struct constructor { struct constructor {

View File

@ -37,15 +37,11 @@
/// further ABI-incompatible changes may be made before the ABI is officially /// further ABI-incompatible changes may be made before the ABI is officially
/// changed to the new version. /// changed to the new version.
#ifndef PYBIND11_INTERNALS_VERSION #ifndef PYBIND11_INTERNALS_VERSION
# if PYBIND11_VERSION_MAJOR >= 3
# define PYBIND11_INTERNALS_VERSION 106 # define PYBIND11_INTERNALS_VERSION 106
# else
# define PYBIND11_INTERNALS_VERSION 6
# endif
#endif #endif
#if PYBIND11_INTERNALS_VERSION < 6 #if PYBIND11_INTERNALS_VERSION < 106
# error "PYBIND11_INTERNALS_VERSION 6 is the minimum for all platforms for pybind11v3." # error "PYBIND11_INTERNALS_VERSION 106 is the minimum (SPECIAL SITUATION)."
#endif #endif
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) 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 { enum class holder_enum_t : uint8_t {
undefined, undefined,
std_unique_ptr, // Default, lacking interop with std::shared_ptr. std_unique_ptr, // Default, lacking interop with std::shared_ptr.
@ -227,13 +219,6 @@ enum class holder_enum_t : uint8_t {
custom_holder, 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. /// Additional type information which does not fit into the PyTypeObject.
/// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`. /// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`.
struct type_info { struct type_info {
@ -262,9 +247,7 @@ struct type_info {
bool default_holder : 1; bool default_holder : 1;
/* true if this is a type registered with py::module_local */ /* true if this is a type registered with py::module_local */
bool module_local : 1; bool module_local : 1;
#ifdef PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
holder_enum_t holder_enum_v = holder_enum_t::undefined; holder_enum_t holder_enum_v = holder_enum_t::undefined;
#endif
}; };
#define PYBIND11_INTERNALS_ID \ #define PYBIND11_INTERNALS_ID \

View File

@ -511,8 +511,6 @@ inline PyThreadState *get_thread_state_unchecked() {
void keep_alive_impl(handle nurse, handle patient); void keep_alive_impl(handle nurse, handle patient);
inline PyObject *make_new_instance(PyTypeObject *type); inline PyObject *make_new_instance(PyTypeObject *type);
#ifdef PYBIND11_SMART_HOLDER_ENABLED
// PYBIND11:REMINDER: Needs refactoring of existing pybind11 code. // PYBIND11:REMINDER: Needs refactoring of existing pybind11 code.
inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo); 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) PYBIND11_NAMESPACE_END(smart_holder_type_caster_support)
#endif // PYBIND11_SMART_HOLDER_ENABLED
class type_caster_generic { class type_caster_generic {
public: public:
PYBIND11_NOINLINE explicit type_caster_generic(const std::type_info &type_info) 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 // Base methods for generic caster; there are overridden in copyable_holder_caster
void load_value(value_and_holder &&v_h) { void load_value(value_and_holder &&v_h) {
#ifdef PYBIND11_SMART_HOLDER_ENABLED
if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) { if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) {
smart_holder_type_caster_support::value_and_holder_helper v_h_helper; smart_holder_type_caster_support::value_and_holder_helper v_h_helper;
v_h_helper.loaded_v_h = v_h; v_h_helper.loaded_v_h = v_h;
@ -984,7 +979,6 @@ public:
return; return;
} }
} }
#endif
auto *&vptr = v_h.value_ptr(); auto *&vptr = v_h.value_ptr();
// Lazy allocation for unallocated values: // Lazy allocation for unallocated values:
if (vptr == nullptr) { if (vptr == nullptr) {

View File

@ -5,29 +5,18 @@
#pragma once #pragma once
#include "common.h" #include "common.h"
#include "internals.h" #include "struct_smart_holder.h"
#include <type_traits> #include <type_traits>
#ifdef PYBIND11_SMART_HOLDER_ENABLED
# include "struct_smart_holder.h"
#endif
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
#ifdef PYBIND11_SMART_HOLDER_ENABLED
using pybindit::memory::smart_holder; using pybindit::memory::smart_holder;
#endif
PYBIND11_NAMESPACE_BEGIN(detail) PYBIND11_NAMESPACE_BEGIN(detail)
#ifdef PYBIND11_SMART_HOLDER_ENABLED
template <typename H> template <typename H>
using is_smart_holder = std::is_same<H, smart_holder>; 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(detail)
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

View File

@ -7,6 +7,7 @@
#include "common.h" #include "common.h"
#include <cstddef> #include <cstddef>
#include <cstdint>
#include <typeinfo> #include <typeinfo>
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

View File

@ -1448,9 +1448,7 @@ protected:
tinfo->simple_ancestors = true; tinfo->simple_ancestors = true;
tinfo->default_holder = rec.default_holder; tinfo->default_holder = rec.default_holder;
tinfo->module_local = rec.module_local; tinfo->module_local = rec.module_local;
#ifdef PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
tinfo->holder_enum_v = rec.holder_enum_v; tinfo->holder_enum_v = rec.holder_enum_v;
#endif
with_internals([&](internals &internals) { with_internals([&](internals &internals) {
auto tindex = std::type_index(*rec.type); auto tindex = std::type_index(*rec.type);
@ -1665,8 +1663,6 @@ PYBIND11_NAMESPACE_END(detail)
template <typename T, typename D, typename SFINAE = void> template <typename T, typename D, typename SFINAE = void>
struct property_cpp_function : detail::property_cpp_function_classic<T, D> {}; struct property_cpp_function : detail::property_cpp_function_classic<T, D> {};
#ifdef PYBIND11_SMART_HOLDER_ENABLED
PYBIND11_NAMESPACE_BEGIN(detail) PYBIND11_NAMESPACE_BEGIN(detail)
template <typename T, typename D, typename SFINAE = void> 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::both_t_and_d_use_type_caster_base<T, typename D::element_type>>::value>>
: detail::property_cpp_function_sh_unique_ptr_member<T, D> {}; : detail::property_cpp_function_sh_unique_ptr_member<T, D> {};
#endif // PYBIND11_SMART_HOLDER_ENABLED #if defined(PYBIND11_USE_SMART_HOLDER_AS_DEFAULT)
#if defined(PYBIND11_USE_SMART_HOLDER_AS_DEFAULT) && defined(PYBIND11_SMART_HOLDER_ENABLED)
// NOTE: THIS IS MEANT FOR STRESS-TESTING ONLY! // NOTE: THIS IS MEANT FOR STRESS-TESTING ONLY!
// As of PR #5257, for production use, there is no longer a strong reason to make // As of PR #5257, for production use, there is no longer a strong reason to make
// smart_holder the default holder: // 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 // 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 // that `py::smart_holder` / `py::classh` is backward-compatible with all pre-existing
// functionality. // functionality.
# define PYBIND11_ACTUALLY_USING_SMART_HOLDER_AS_DEFAULT
template <typename> template <typename>
using default_holder_type = smart_holder; using default_holder_type = smart_holder;
#else #else
@ -1913,7 +1906,6 @@ public:
// A more fitting name would be uses_unique_ptr_holder. // A more fitting name would be uses_unique_ptr_holder.
record.default_holder = detail::is_instantiation<std::unique_ptr, holder_type>::value; 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) { if (detail::is_instantiation<std::unique_ptr, holder_type>::value) {
record.holder_enum_v = detail::holder_enum_t::std_unique_ptr; record.holder_enum_v = detail::holder_enum_t::std_unique_ptr;
} else if (detail::is_instantiation<std::shared_ptr, holder_type>::value) { } else if (detail::is_instantiation<std::shared_ptr, holder_type>::value) {
@ -1923,7 +1915,6 @@ public:
} else { } else {
record.holder_enum_v = detail::holder_enum_t::custom_holder; record.holder_enum_v = detail::holder_enum_t::custom_holder;
} }
#endif
set_operator_new<type>(&record); 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>()); init_holder(inst, v_h, (const holder_type *) holder_ptr, v_h.value_ptr<type>());
} }
#ifdef PYBIND11_SMART_HOLDER_ENABLED
template <typename WrappedType> template <typename WrappedType>
static bool try_initialization_using_shared_from_this(holder_type *, WrappedType *, ...) { static bool try_initialization_using_shared_from_this(holder_type *, WrappedType *, ...) {
return false; return false;
@ -2326,8 +2315,6 @@ private:
v_h.set_holder_constructed(); v_h.set_holder_constructed();
} }
#endif // PYBIND11_SMART_HOLDER_ENABLED
// Deallocates an instance; via holder, if constructed; otherwise via operator delete. // Deallocates an instance; via holder, if constructed; otherwise via operator delete.
// NOTE: The Python error indicator needs to cleared BEFORE this function is called. // 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. // 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>: // 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. // users can simply replace the `_` in `class_` with `h` or vice versa.
template <typename type_, typename... options> template <typename type_, typename... options>
@ -2403,8 +2388,6 @@ public:
using class_<type_, smart_holder, options...>::class_; using class_<type_, smart_holder, options...>::class_;
}; };
#endif
/// Binds an existing constructor taking arguments Args... /// Binds an existing constructor taking arguments Args...
template <typename... Args> template <typename... Args>
detail::initimpl::constructor<Args...> init() { detail::initimpl::constructor<Args...> init() {

View File

@ -4,13 +4,9 @@
#pragma once #pragma once
#include "detail/internals.h" #include "detail/common.h"
#include "detail/using_smart_holder.h"
#ifdef PYBIND11_SMART_HOLDER_ENABLED #include "detail/value_and_holder.h"
# include "detail/common.h"
# include "detail/using_smart_holder.h"
# include "detail/value_and_holder.h"
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
@ -62,5 +58,3 @@ struct trampoline_self_life_support {
}; };
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
#endif // PYBIND11_SMART_HOLDER_ENABLED

View File

@ -109,11 +109,7 @@ TEST_SUBMODULE(class_, m) {
struct ToBeHeldByUniquePtr {}; struct ToBeHeldByUniquePtr {};
py::class_<ToBeHeldByUniquePtr, std::unique_ptr<ToBeHeldByUniquePtr>>(m, "ToBeHeldByUniquePtr") py::class_<ToBeHeldByUniquePtr, std::unique_ptr<ToBeHeldByUniquePtr>>(m, "ToBeHeldByUniquePtr")
.def(py::init<>()); .def(py::init<>());
#ifdef PYBIND11_SMART_HOLDER_ENABLED
m.def("pass_unique_ptr", [](std::unique_ptr<ToBeHeldByUniquePtr> &&) {}); m.def("pass_unique_ptr", [](std::unique_ptr<ToBeHeldByUniquePtr> &&) {});
#else
m.attr("pass_unique_ptr") = py::none();
#endif
// test_inheritance // test_inheritance
class Pet { class Pet {
@ -636,7 +632,7 @@ CHECK_NOALIAS(8);
CHECK_HOLDER(1, unique); CHECK_HOLDER(1, unique);
CHECK_HOLDER(2, unique); CHECK_HOLDER(2, unique);
CHECK_HOLDER(3, 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(4, unique);
CHECK_HOLDER(5, unique); CHECK_HOLDER(5, unique);
#endif #endif

View File

@ -51,8 +51,6 @@ def test_instance_new():
def test_pass_unique_ptr(): def test_pass_unique_ptr():
obj = m.ToBeHeldByUniquePtr() obj = m.ToBeHeldByUniquePtr()
if m.pass_unique_ptr is None:
pytest.skip("smart_holder not available.")
with pytest.raises(RuntimeError) as execinfo: with pytest.raises(RuntimeError) as execinfo:
m.pass_unique_ptr(obj) m.pass_unique_ptr(obj)
assert str(execinfo.value).startswith( assert str(execinfo.value).startswith(

View File

@ -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 CastUnusualOpRefConstRef(const LocalUnusualOpRef &cref) { return py::cast(cref); }
py::object CastUnusualOpRefMovable(LocalUnusualOpRef &&mvbl) { return py::cast(std::move(mvbl)); } 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) { TEST_SUBMODULE(class_sh_basic, m) {
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
#ifndef PYBIND11_SMART_HOLDER_ENABLED
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) {
@ -260,7 +243,6 @@ 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_SMART_HOLDER_ENABLED
} }
} // namespace class_sh_basic } // namespace class_sh_basic

View File

@ -7,9 +7,6 @@ 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_SMART_HOLDER_ENABLED:
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

@ -28,16 +28,7 @@ int overloaded(std::unique_ptr<Atype<2>> at2, int i) { return at2->get() * 40 +
} // namespace class_sh_disowning } // namespace class_sh_disowning
} // namespace pybind11_tests } // 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) { 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; 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);
@ -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<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_SMART_HOLDER_ENABLED
} }

View File

@ -4,9 +4,6 @@ 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_SMART_HOLDER_ENABLED:
pytest.skip("smart_holder not available.", allow_module_level=True)
def is_disowned(obj): def is_disowned(obj):
try: try:

View File

@ -48,21 +48,7 @@ int disown_base2(std::unique_ptr<Base2> b2) { return b2->j * 2000 + 2; }
} // namespace class_sh_disowning_mi } // namespace class_sh_disowning_mi
} // namespace pybind11_tests } // 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) { 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; using namespace pybind11_tests::class_sh_disowning_mi;
py::classh<B>(m, "B") 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); 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_SMART_HOLDER_ENABLED
} }

View File

@ -5,9 +5,6 @@ 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_SMART_HOLDER_ENABLED:
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

@ -72,27 +72,7 @@ struct sddwaa : std::default_delete<with_alias_alias> {};
} // namespace class_sh_factory_constructors } // namespace class_sh_factory_constructors
} // namespace pybind11_tests } // 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) { 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; using namespace pybind11_tests::class_sh_factory_constructors;
py::classh<atyp_valu>(m, "atyp_valu") py::classh<atyp_valu>(m, "atyp_valu")
@ -183,5 +163,4 @@ 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_SMART_HOLDER_ENABLED
} }

View File

@ -4,9 +4,6 @@ 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_SMART_HOLDER_ENABLED:
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

@ -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; } inline int pass_cptr_drvd2(drvd2 const *d) { return d->id() + 23; }
// clang-format on // 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) { 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<base>(m, "base");
py::classh<drvd, base>(m, "drvd"); 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_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_SMART_HOLDER_ENABLED
} }
} // namespace class_sh_inheritance } // namespace class_sh_inheritance

View File

@ -1,12 +1,7 @@
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_SMART_HOLDER_ENABLED:
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

@ -35,17 +35,7 @@ struct Derived : Base1, Base0 {
} // namespace test_class_sh_mi_thunks } // 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) { 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; using namespace test_class_sh_mi_thunks;
m.def("ptrdiff_drvd_base0", []() { m.def("ptrdiff_drvd_base0", []() {
@ -103,5 +93,4 @@ TEST_SUBMODULE(class_sh_mi_thunks, m) {
} }
return obj_der->vec.size(); return obj_der->vec.size();
}); });
#endif // PYBIND11_SMART_HOLDER_ENABLED
} }

View File

@ -4,9 +4,6 @@ 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_SMART_HOLDER_ENABLED:
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

@ -46,24 +46,7 @@ struct WithConstCharPtrMember {
} // namespace test_class_sh_property } // 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) { 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; using namespace test_class_sh_property;
py::class_<ClassicField, std::unique_ptr<ClassicField>>(m, "ClassicField") py::class_<ClassicField, std::unique_ptr<ClassicField>>(m, "ClassicField")
@ -109,5 +92,4 @@ TEST_SUBMODULE(class_sh_property, m) {
py::classh<WithConstCharPtrMember>(m, "WithConstCharPtrMember") py::classh<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_SMART_HOLDER_ENABLED
} }

View File

@ -7,9 +7,6 @@ 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_SMART_HOLDER_ENABLED:
pytest.skip("smart_holder not available.", allow_module_level=True)
@pytest.mark.skipif( @pytest.mark.skipif(
"env.PYPY or env.GRAALPY", reason="gc after `del field` is apparently deferred" "env.PYPY or env.GRAALPY", reason="gc after `del field` is apparently deferred"

View File

@ -46,17 +46,7 @@ public:
using namespace test_class_sh_property_non_owning; 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) { 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<CoreField>(m, "CoreField").def_readwrite("int_value", &CoreField::int_value);
py::classh<DataField>(m, "DataField") py::classh<DataField>(m, "DataField")
@ -71,5 +61,4 @@ 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_SMART_HOLDER_ENABLED
} }

View File

@ -4,9 +4,6 @@ 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_SMART_HOLDER_ENABLED:
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

@ -41,21 +41,8 @@ struct Outer {
}; };
} // namespace } // 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) { 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; namespace py = pybind11;
py::class_<FooShPtr, std::shared_ptr<FooShPtr>>(m, "FooShPtr") py::class_<FooShPtr, std::shared_ptr<FooShPtr>>(m, "FooShPtr")
@ -63,20 +50,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, \
@ -85,8 +72,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");
@ -113,7 +100,6 @@ 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_SMART_HOLDER_ENABLED
} }
} // namespace pybind11_tests } // namespace pybind11_tests

View File

@ -1,12 +1,7 @@
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_SMART_HOLDER_ENABLED:
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

@ -34,7 +34,6 @@ struct AbaseAlias : Abase<SerNo> {
} }
}; };
#ifdef PYBIND11_SMART_HOLDER_ENABLED
template <> template <>
struct AbaseAlias<1> : Abase<1>, py::trampoline_self_life_support { struct AbaseAlias<1> : Abase<1>, py::trampoline_self_life_support {
using Abase<1>::Abase; using Abase<1>::Abase;
@ -46,7 +45,6 @@ struct AbaseAlias<1> : Abase<1>, py::trampoline_self_life_support {
other_val); other_val);
} }
}; };
#endif // PYBIND11_SMART_HOLDER_ENABLED
template <int SerNo> template <int SerNo>
int AddInCppRawPtr(const Abase<SerNo> *obj, int other_val) { 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; return obj->Add(other_val) * 100 + 13;
} }
#ifdef PYBIND11_SMART_HOLDER_ENABLED
template <int SerNo> template <int SerNo>
void wrap(py::module_ m, const char *py_class_name) { void wrap(py::module_ m, const char *py_class_name) {
py::classh<Abase<SerNo>, AbaseAlias<SerNo>>(m, 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("AddInCppSharedPtr", AddInCppSharedPtr<SerNo>, py::arg("obj"), py::arg("other_val"));
m.def("AddInCppUniquePtr", AddInCppUniquePtr<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 class_sh_trampoline_basic
} // namespace pybind11_tests } // namespace pybind11_tests
using namespace pybind11_tests::class_sh_trampoline_basic; 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) { 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<0>(m, "Abase0");
wrap<1>(m, "Abase1"); wrap<1>(m, "Abase1");
#endif // PYBIND11_SMART_HOLDER_ENABLED
} }

View File

@ -4,9 +4,6 @@ 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_SMART_HOLDER_ENABLED:
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

@ -38,26 +38,16 @@ protected:
Big5() : history{"DefaultConstructor"} {} Big5() : history{"DefaultConstructor"} {}
}; };
#ifdef PYBIND11_SMART_HOLDER_ENABLED
struct Big5Trampoline : Big5, py::trampoline_self_life_support { struct Big5Trampoline : Big5, py::trampoline_self_life_support {
using Big5::Big5; using Big5::Big5;
}; };
#endif
} // namespace class_sh_trampoline_self_life_support } // namespace class_sh_trampoline_self_life_support
} // namespace pybind11_tests } // namespace pybind11_tests
using namespace pybind11_tests::class_sh_trampoline_self_life_support; 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) { 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") 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);
@ -94,5 +84,4 @@ 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_SMART_HOLDER_ENABLED
} }

View File

@ -4,9 +4,6 @@ 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_SMART_HOLDER_ENABLED:
pytest.skip("smart_holder not available.", allow_module_level=True)
class PyBig5(m.Big5): class PyBig5(m.Big5):
pass pass

View File

@ -71,11 +71,9 @@ struct SftSharedPtrStash {
} }
}; };
#ifdef PYBIND11_SMART_HOLDER_ENABLED
struct SftTrampoline : Sft, py::trampoline_self_life_support { struct SftTrampoline : Sft, py::trampoline_self_life_support {
using Sft::Sft; using Sft::Sft;
}; };
#endif
long use_count(const std::shared_ptr<Sft> &obj) { return obj.use_count(); } 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; 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) { 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") 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) {
@ -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_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_SMART_HOLDER_ENABLED
} }

View File

@ -8,9 +8,6 @@ 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_SMART_HOLDER_ENABLED:
pytest.skip("smart_holder not available.", allow_module_level=True)
class PySft(m.Sft): class PySft(m.Sft):
pass pass

View File

@ -57,18 +57,7 @@ struct SpGoAwayTester {
using namespace pybind11_tests::class_sh_trampoline_shared_ptr_cpp_arg; 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) { 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 // For testing whether a python subclass of a C++ object dies when the
// last python reference is lost // last python reference is lost
@ -101,5 +90,4 @@ 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_SMART_HOLDER_ENABLED
} }

View File

@ -5,9 +5,6 @@ import pytest
import env # noqa: F401 import env # noqa: F401
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_SMART_HOLDER_ENABLED:
pytest.skip("smart_holder not available.", allow_module_level=True)
@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") @pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC")
def test_shared_ptr_cpp_arg(): def test_shared_ptr_cpp_arg():

View File

@ -34,12 +34,9 @@ private:
} // namespace class_sh_trampoline_unique_ptr } // namespace class_sh_trampoline_unique_ptr
} // namespace pybind11_tests } // namespace pybind11_tests
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_trampoline_unique_ptr::Class)
namespace pybind11_tests { namespace pybind11_tests {
namespace class_sh_trampoline_unique_ptr { namespace class_sh_trampoline_unique_ptr {
#ifdef PYBIND11_SMART_HOLDER_ENABLED
class PyClass : public Class, public py::trampoline_self_life_support { class PyClass : public Class, public py::trampoline_self_life_support {
public: public:
std::unique_ptr<Class> clone() const override { std::unique_ptr<Class> clone() const override {
@ -48,18 +45,11 @@ public:
int foo() const override { PYBIND11_OVERRIDE_PURE(int, Class, foo); } int foo() const override { PYBIND11_OVERRIDE_PURE(int, Class, foo); }
}; };
#endif
} // namespace class_sh_trampoline_unique_ptr } // namespace class_sh_trampoline_unique_ptr
} // namespace pybind11_tests } // namespace pybind11_tests
TEST_SUBMODULE(class_sh_trampoline_unique_ptr, m) { 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; using namespace pybind11_tests::class_sh_trampoline_unique_ptr;
py::classh<Class, PyClass>(m, "Class") 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", [](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_SMART_HOLDER_ENABLED
} }

View File

@ -1,12 +1,7 @@
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_SMART_HOLDER_ENABLED:
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

@ -22,25 +22,10 @@ private:
explicit Pet(const std::string &name) : name(name) {} 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) { 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); py::classh<Pet>(m, "Pet").def_readwrite("name", &Pet::name);
m.def("create", &Pet::New); m.def("create", &Pet::New);
#endif // PYBIND11_SMART_HOLDER_ENABLED
} }
} // namespace class_sh_unique_ptr_custom_deleter } // namespace class_sh_unique_ptr_custom_deleter

View File

@ -1,12 +1,7 @@
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_SMART_HOLDER_ENABLED:
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

@ -36,21 +36,7 @@ private:
std::unique_ptr<pointee> ptr_; 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) { 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); 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);
@ -60,7 +46,6 @@ 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_SMART_HOLDER_ENABLED
} }
} // namespace class_sh_unique_ptr_member } // namespace class_sh_unique_ptr_member

View File

@ -4,9 +4,6 @@ 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_SMART_HOLDER_ENABLED:
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

@ -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; } 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 { struct BaseVirtualOverrider : Base, py::trampoline_self_life_support {
using Base::Base; using Base::Base;
@ -45,24 +43,12 @@ struct CppDerivedVirtualOverrider : CppDerived, py::trampoline_self_life_support
int get() const override { PYBIND11_OVERRIDE(int, CppDerived, get); } int get() const override { PYBIND11_OVERRIDE(int, CppDerived, get); }
}; };
#endif
} // namespace class_sh_virtual_py_cpp_mix } // namespace class_sh_virtual_py_cpp_mix
} // namespace pybind11_tests } // namespace pybind11_tests
using namespace pybind11_tests::class_sh_virtual_py_cpp_mix; 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) { 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<Base, BaseVirtualOverrider>(m, "Base").def(py::init<>()).def("get", &Base::get);
py::classh<CppDerivedPlain, Base>(m, "CppDerivedPlain").def(py::init<>()); 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_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_SMART_HOLDER_ENABLED
} }

View File

@ -4,9 +4,6 @@ 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_SMART_HOLDER_ENABLED:
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):

View File

@ -22,8 +22,6 @@ void wrap_number_bucket(py::module m, const char *class_name) {
.def("add", &WrappedType::add, py::arg("other")); .def("add", &WrappedType::add, py::arg("other"));
} }
#ifdef PYBIND11_SMART_HOLDER_ENABLED
template <typename T> template <typename T>
class padded_unique_ptr { class padded_unique_ptr {
std::unique_ptr<T> ptr; std::unique_ptr<T> ptr;
@ -37,21 +35,15 @@ public:
static_assert(sizeof(padded_unique_ptr<nb_pu>) == sizeof(py::smart_holder), static_assert(sizeof(padded_unique_ptr<nb_pu>) == sizeof(py::smart_holder),
"Unexpected sizeof mismatch."); "Unexpected sizeof mismatch.");
#endif
} // namespace hc } // namespace hc
#ifdef PYBIND11_SMART_HOLDER_ENABLED
PYBIND11_DECLARE_HOLDER_TYPE(T, hc::padded_unique_ptr<T>); PYBIND11_DECLARE_HOLDER_TYPE(T, hc::padded_unique_ptr<T>);
#endif
PYBIND11_MODULE(pybind11_ubench_holder_comparison, m) { PYBIND11_MODULE(pybind11_ubench_holder_comparison, m) {
using namespace hc; using namespace hc;
wrap_number_bucket<nb_up, std::unique_ptr<nb_up>>(m, "number_bucket_up"); 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"); 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); }); 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_pu, padded_unique_ptr<nb_pu>>(m, "number_bucket_pu");
wrap_number_bucket<nb_sh, py::smart_holder>(m, "number_bucket_sh"); wrap_number_bucket<nb_sh, py::smart_holder>(m, "number_bucket_sh");
#endif
} }