From 05c8b9b7a8b8b72c8af7d94ac4a726d45607457b Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 14 Feb 2022 19:00:40 -0800 Subject: [PATCH] Automatic `pre-commit run --all-files` and clang-tidy changes (NO manual changes). --- include/pybind11/detail/init.h | 6 +- include/pybind11/detail/smart_holder_poc.h | 23 +- .../detail/smart_holder_type_casters.h | 224 ++++++++++-------- include/pybind11/smart_holder.h | 2 +- tests/test_class_sh_basic.cpp | 4 +- tests/test_class_sh_basic.py | 1 - tests/test_class_sh_disowning.cpp | 4 +- tests/test_class_sh_disowning.py | 3 - tests/test_class_sh_disowning_mi.cpp | 4 +- tests/test_class_sh_disowning_mi.py | 5 +- tests/test_class_sh_factory_constructors.cpp | 4 +- tests/test_class_sh_factory_constructors.py | 1 - tests/test_class_sh_inheritance.cpp | 4 +- tests/test_class_sh_inheritance.py | 2 - tests/test_class_sh_module_local.py | 1 - tests/test_class_sh_shared_ptr_copy_move.cpp | 4 +- tests/test_class_sh_shared_ptr_copy_move.py | 6 +- tests/test_class_sh_trampoline_basic.cpp | 4 +- tests/test_class_sh_trampoline_basic.py | 5 +- ..._class_sh_trampoline_self_life_support.cpp | 2 +- ...t_class_sh_trampoline_self_life_support.py | 1 - ...t_class_sh_trampoline_shared_from_this.cpp | 6 +- ...st_class_sh_trampoline_shared_from_this.py | 3 +- ...class_sh_trampoline_shared_ptr_cpp_arg.cpp | 6 +- ..._class_sh_trampoline_shared_ptr_cpp_arg.py | 3 +- tests/test_class_sh_trampoline_unique_ptr.py | 2 - tests/test_class_sh_unique_ptr_member.cpp | 4 +- tests/test_class_sh_unique_ptr_member.py | 1 - tests/test_class_sh_virtual_py_cpp_mix.cpp | 4 +- tests/test_class_sh_virtual_py_cpp_mix.py | 1 - tests/test_class_sh_void_ptr_capsule.cpp | 106 ++++----- tests/test_class_sh_void_ptr_capsule.py | 1 - tests/test_classh_mock.py | 2 - tests/test_smart_ptr.cpp | 10 +- ubench/holder_comparison.py | 4 +- .../holder_comparison_extract_sheet_data.py | 2 - 36 files changed, 237 insertions(+), 228 deletions(-) diff --git a/include/pybind11/detail/init.h b/include/pybind11/detail/init.h index 1ff2abd09..18adc40b1 100644 --- a/include/pybind11/detail/init.h +++ b/include/pybind11/detail/init.h @@ -206,9 +206,10 @@ void construct(value_and_holder &v_h, std::unique_ptr, D> &&unq_ptr, PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); auto *ptr = unq_ptr.get(); no_nullptr(ptr); - if (PYBIND11_SILENCE_MSVC_C4127(Class::has_alias) && need_alias && !is_alias(ptr)) + if (PYBIND11_SILENCE_MSVC_C4127(Class::has_alias) && need_alias && !is_alias(ptr)) { throw type_error("pybind11::init(): construction failed: returned std::unique_ptr pointee " "is not an alias instance"); + } // Here and below: if the new object is a trampoline, the shared_from_this mechanism needs // to be prevented from accessing the smart_holder vptr, because it does not keep the // trampoline Python object alive. For types that don't inherit from enable_shared_from_this @@ -242,9 +243,10 @@ void construct(value_and_holder &v_h, std::shared_ptr> &&shd_ptr, boo PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); auto *ptr = shd_ptr.get(); no_nullptr(ptr); - if (PYBIND11_SILENCE_MSVC_C4127(Class::has_alias) && need_alias && !is_alias(ptr)) + if (PYBIND11_SILENCE_MSVC_C4127(Class::has_alias) && need_alias && !is_alias(ptr)) { throw type_error("pybind11::init(): construction failed: returned std::shared_ptr pointee " "is not an alias instance"); + } auto smhldr = type_caster>::template smart_holder_from_shared_ptr(shd_ptr); v_h.value_ptr() = ptr; v_h.type->init_instance(v_h.inst, &smhldr); diff --git a/include/pybind11/detail/smart_holder_poc.h b/include/pybind11/detail/smart_holder_poc.h index 4a2dc034c..6825e6451 100644 --- a/include/pybind11/detail/smart_holder_poc.h +++ b/include/pybind11/detail/smart_holder_poc.h @@ -74,8 +74,9 @@ struct guarded_delete { guarded_delete(void (*del_ptr)(void *), bool armed_flag) : del_ptr{del_ptr}, armed_flag{armed_flag} {} void operator()(void *raw_ptr) const { - if (armed_flag) + if (armed_flag) { (*del_ptr)(raw_ptr); + } } }; @@ -138,9 +139,10 @@ struct smart_holder { template static void ensure_pointee_is_destructible(const char *context) { - if (!std::is_destructible::value) + if (!std::is_destructible::value) { throw std::invalid_argument(std::string("Pointee is not destructible (") + context + ")."); + } } void ensure_is_populated(const char *context) const { @@ -207,7 +209,7 @@ struct smart_holder { } void reset_vptr_deleter_armed_flag(bool armed_flag) const { - auto vptr_del_ptr = std::get_deleter(vptr); + auto *vptr_del_ptr = std::get_deleter(vptr); if (vptr_del_ptr == nullptr) { throw std::runtime_error( "smart_holder::reset_vptr_deleter_armed_flag() called in an invalid context."); @@ -249,10 +251,11 @@ struct smart_holder { ensure_pointee_is_destructible("from_raw_ptr_take_ownership"); smart_holder hld; auto gd = make_guarded_builtin_delete(true); - if (void_cast_raw_ptr) + if (void_cast_raw_ptr) { hld.vptr.reset(static_cast(raw_ptr), std::move(gd)); - else + } else { hld.vptr.reset(raw_ptr, std::move(gd)); + } hld.vptr_is_using_builtin_delete = true; hld.is_populated = true; return hld; @@ -301,14 +304,16 @@ struct smart_holder { hld.rtti_uqp_del = &typeid(D); hld.vptr_is_using_builtin_delete = is_std_default_delete(*hld.rtti_uqp_del); guarded_delete gd{nullptr, false}; - if (hld.vptr_is_using_builtin_delete) + if (hld.vptr_is_using_builtin_delete) { gd = make_guarded_builtin_delete(true); - else + } else { gd = make_guarded_custom_deleter(true); - if (void_cast_raw_ptr) + } + if (void_cast_raw_ptr) { hld.vptr.reset(static_cast(unq_ptr.get()), std::move(gd)); - else + } else { hld.vptr.reset(unq_ptr.get(), std::move(gd)); + } (void) unq_ptr.release(); hld.is_populated = true; return hld; diff --git a/include/pybind11/detail/smart_holder_type_casters.h b/include/pybind11/detail/smart_holder_type_casters.h index 482090e39..afec27bd8 100644 --- a/include/pybind11/detail/smart_holder_type_casters.h +++ b/include/pybind11/detail/smart_holder_type_casters.h @@ -38,7 +38,7 @@ inline void register_instance(instance *self, void *valptr, const type_info *tin inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo); // Replace all occurrences of a character in string. -inline void replace_all(std::string& str, const std::string& from, char to) { +inline void replace_all(std::string &str, const std::string &from, char to) { size_t pos = str.find(from); while (pos != std::string::npos) { str.replace(pos, from.length(), 1, to); @@ -51,11 +51,12 @@ inline void replace_all(std::string& str, const std::string& from, char to) { // held value, while the modified implementation propagates value_and_holder. class modified_type_caster_generic_load_impl { public: - PYBIND11_NOINLINE explicit modified_type_caster_generic_load_impl(const std::type_info &type_info) - : typeinfo(get_type_info(type_info)), cpptype(&type_info) { } + PYBIND11_NOINLINE explicit modified_type_caster_generic_load_impl( + const std::type_info &type_info) + : typeinfo(get_type_info(type_info)), cpptype(&type_info) {} explicit modified_type_caster_generic_load_impl(const type_info *typeinfo = nullptr) - : typeinfo(typeinfo), cpptype(typeinfo ? typeinfo->cpptype : nullptr) { } + : typeinfo(typeinfo), cpptype(typeinfo ? typeinfo->cpptype : nullptr) {} bool load(handle src, bool convert) { return load_impl(src, convert); @@ -70,20 +71,19 @@ public: // Lazy allocation for unallocated values: if (vptr == nullptr) { // Lazy allocation for unallocated values: - auto *type = v_h.type ? v_h.type : typeinfo; + const auto *type = v_h.type ? v_h.type : typeinfo; if (type->operator_new) { vptr = type->operator_new(type->type_size); } else { - #if defined(__cpp_aligned_new) && (!defined(_MSC_VER) || _MSC_VER >= 1912) - if (type->type_align > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { - vptr = ::operator new(type->type_size, - std::align_val_t(type->type_align)); - } else { - vptr = ::operator new(type->type_size); - } - #else +#if defined(__cpp_aligned_new) && (!defined(_MSC_VER) || _MSC_VER >= 1912) + if (type->type_align > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { + vptr = ::operator new(type->type_size, std::align_val_t(type->type_align)); + } else { vptr = ::operator new(type->type_size); - #endif + } +#else + vptr = ::operator new(type->type_size); +#endif } } // type_caster_generic::load_value END @@ -93,7 +93,7 @@ public: } bool try_implicit_casts(handle src, bool convert) { - for (auto &cast : typeinfo->implicit_casts) { + for (const auto &cast : typeinfo->implicit_casts) { modified_type_caster_generic_load_impl sub_caster(*cast.first); if (sub_caster.load(src, convert)) { if (loaded_v_h_cpptype != nullptr) { @@ -129,14 +129,13 @@ public: std::string as_void_ptr_function_name("as_"); as_void_ptr_function_name += type_name; if (hasattr(src, as_void_ptr_function_name.c_str())) { - auto as_void_ptr_function = function( - src.attr(as_void_ptr_function_name.c_str())); - auto void_ptr_capsule = as_void_ptr_function(); - if (isinstance(void_ptr_capsule)) { - unowned_void_ptr_from_void_ptr_capsule = reinterpret_borrow( - void_ptr_capsule).get_pointer(); - return true; - } + auto as_void_ptr_function = function(src.attr(as_void_ptr_function_name.c_str())); + auto void_ptr_capsule = as_void_ptr_function(); + if (isinstance(void_ptr_capsule)) { + unowned_void_ptr_from_void_ptr_capsule + = reinterpret_borrow(void_ptr_capsule).get_pointer(); + return true; + } } return false; } @@ -159,25 +158,27 @@ public: PYBIND11_NOINLINE bool try_load_foreign_module_local(handle src) { constexpr auto *local_key = PYBIND11_MODULE_LOCAL_ID; const auto pytype = type::handle_of(src); - if (!hasattr(pytype, local_key)) + if (!hasattr(pytype, local_key)) { return false; + } type_info *foreign_typeinfo = reinterpret_borrow(getattr(pytype, local_key)); - // Only consider this foreign loader if actually foreign and is a loader of the correct cpp type + // Only consider this foreign loader if actually foreign and is a loader of the correct cpp + // type if (foreign_typeinfo->module_local_load == &local_load - || (cpptype && !same_type(*cpptype, *foreign_typeinfo->cpptype))) + || (cpptype && !same_type(*cpptype, *foreign_typeinfo->cpptype))) { return false; + } - void* foreign_loader_void_ptr = - foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo); + void *foreign_loader_void_ptr + = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo); if (foreign_loader_void_ptr != nullptr) { auto foreign_loader = std::unique_ptr( static_cast(foreign_loader_void_ptr)); // Magic number intentionally hard-coded for simplicity and maximum robustness. if (foreign_loader->local_load_safety_guard != 1887406645) { - pybind11_fail( - "smart_holder_type_casters: Unexpected local_load_safety_guard," - " possibly due to py::class_ holder mixup."); + pybind11_fail("smart_holder_type_casters: Unexpected local_load_safety_guard," + " possibly due to py::class_ holder mixup."); } if (loaded_v_h_cpptype != nullptr) { pybind11_fail("smart_holder_type_casters: try_load_foreign_module_local failure."); @@ -196,11 +197,15 @@ public: // logic (without having to resort to virtual inheritance). template PYBIND11_NOINLINE bool load_impl(handle src, bool convert) { - if (!src) return false; - if (cpptype && try_as_void_ptr_capsule(src)) { - return true; + if (!src) { + return false; + } + if (cpptype && try_as_void_ptr_capsule(src)) { + return true; + } + if (!typeinfo) { + return try_load_foreign_module_local(src); } - if (!typeinfo) return try_load_foreign_module_local(src); auto &this_ = static_cast(*this); @@ -209,12 +214,13 @@ public: // Case 1: If src is an exact type match for the target type then we can reinterpret_cast // the instance's value pointer to the target type: if (srctype == typeinfo->type) { - this_.load_value_and_holder(reinterpret_cast(src.ptr())->get_value_and_holder()); + this_.load_value_and_holder( + reinterpret_cast(src.ptr())->get_value_and_holder()); return true; } // Case 2: We have a derived class if (PyType_IsSubtype(srctype, typeinfo->type)) { - auto &bases = all_type_info(srctype); // subtype bases + const auto &bases = all_type_info(srctype); // subtype bases bool no_cpp_mi = typeinfo->simple_type; // Case 2a: the python type is a Python-inherited derived class that inherits from just @@ -224,18 +230,21 @@ public: // is extremely common, we handle it specially to avoid the loop iterator and type // pointer lookup overhead) if (bases.size() == 1 && (no_cpp_mi || bases.front()->type == typeinfo->type)) { - this_.load_value_and_holder(reinterpret_cast(src.ptr())->get_value_and_holder()); + this_.load_value_and_holder( + reinterpret_cast(src.ptr())->get_value_and_holder()); loaded_v_h_cpptype = bases.front()->cpptype; reinterpret_cast_deemed_ok = true; return true; } - // Case 2b: the python type inherits from multiple C++ bases. Check the bases to see if - // we can find an exact match (or, for a simple C++ type, an inherited match); if so, we - // can safely reinterpret_cast to the relevant pointer. + // Case 2b: the python type inherits from multiple C++ bases. Check the bases to see + // if we can find an exact match (or, for a simple C++ type, an inherited match); if + // so, we can safely reinterpret_cast to the relevant pointer. if (bases.size() > 1) { - for (auto base : bases) { - if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type) : base->type == typeinfo->type) { - this_.load_value_and_holder(reinterpret_cast(src.ptr())->get_value_and_holder(base)); + for (auto *base : bases) { + if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type) + : base->type == typeinfo->type) { + this_.load_value_and_holder( + reinterpret_cast(src.ptr())->get_value_and_holder(base)); loaded_v_h_cpptype = base->cpptype; reinterpret_cast_deemed_ok = true; return true; @@ -243,9 +252,9 @@ public: } } - // Case 2c: C++ multiple inheritance is involved and we couldn't find an exact type match - // in the registered bases, above, so try implicit casting (needed for proper C++ casting - // when MI is involved). + // Case 2c: C++ multiple inheritance is involved and we couldn't find an exact type + // match in the registered bases, above, so try implicit casting (needed for proper C++ + // casting when MI is involved). if (this_.try_implicit_casts(src, convert)) { return true; } @@ -253,32 +262,36 @@ public: // Perform an implicit conversion if (convert) { - for (auto &converter : typeinfo->implicit_conversions) { + for (const auto &converter : typeinfo->implicit_conversions) { auto temp = reinterpret_steal(converter(src.ptr(), typeinfo->type)); if (load_impl(temp, false)) { loader_life_support::add_patient(temp); return true; } } - if (this_.try_direct_conversions(src)) + if (this_.try_direct_conversions(src)) { return true; + } } // Failed to match local typeinfo. Try again with global. if (typeinfo->module_local) { - if (auto gtype = get_global_type_info(*typeinfo->cpptype)) { + if (auto *gtype = get_global_type_info(*typeinfo->cpptype)) { typeinfo = gtype; return load(src, false); } } // Global typeinfo has precedence over foreign module_local - if (try_load_foreign_module_local(src)) + if (try_load_foreign_module_local(src)) { return true; + } if (src.is_none()) { // Defer accepting None to other overloads (if we aren't in convert mode): - if (!convert) return false; + if (!convert) { + return false; + } loaded_v_h = value_and_holder(); return true; } @@ -291,7 +304,7 @@ public: void *unowned_void_ptr_from_direct_conversion = nullptr; void *unowned_void_ptr_from_void_ptr_capsule = nullptr; const std::type_info *loaded_v_h_cpptype = nullptr; - std::vector implicit_casts; + std::vector implicit_casts; value_and_holder loaded_v_h; bool reinterpret_cast_deemed_ok = false; // Magic number intentionally hard-coded, to guard against class_ holder mixups. @@ -326,8 +339,9 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag const std::enable_shared_from_this *) { auto shd_ptr = std::dynamic_pointer_cast( detail::try_get_shared_from_this(value_ptr_w_t)); - if (!shd_ptr) + if (!shd_ptr) { return false; + } // Note: inst->owned ignored. new (uninitialized_location) holder_type(holder_type::from_shared_ptr(shd_ptr)); return true; @@ -337,20 +351,20 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag static void init_instance_for_type(detail::instance *inst, const void *holder_const_void_ptr) { // Need for const_cast is a consequence of the type_info::init_instance type: // void (*init_instance)(instance *, const void *); - auto holder_void_ptr = const_cast(holder_const_void_ptr); + auto *holder_void_ptr = const_cast(holder_const_void_ptr); auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(WrappedType))); if (!v_h.instance_registered()) { register_instance(inst, v_h.value_ptr(), v_h.type); v_h.set_instance_registered(); } - auto uninitialized_location = std::addressof(v_h.holder()); - auto value_ptr_w_t = v_h.value_ptr(); + auto *uninitialized_location = std::addressof(v_h.holder()); + auto *value_ptr_w_t = v_h.value_ptr(); bool pointee_depends_on_holder_owner = dynamic_raw_ptr_cast_if_possible(value_ptr_w_t) != nullptr; if (holder_void_ptr) { // Note: inst->owned ignored. - auto holder_ptr = static_cast(holder_void_ptr); + auto *holder_ptr = static_cast(holder_void_ptr); new (uninitialized_location) holder_type(std::move(*holder_ptr)); } else if (!try_initialization_using_shared_from_this( uninitialized_location, value_ptr_w_t, value_ptr_w_t)) { @@ -399,32 +413,36 @@ struct smart_holder_type_caster_load { bool load(handle src, bool convert) { static_assert(type_uses_smart_holder_type_caster::value, "Internal consistency error."); load_impl = modified_type_caster_generic_load_impl(typeid(T)); - if (!load_impl.load(src, convert)) + if (!load_impl.load(src, convert)) { return false; + } return true; } T *loaded_as_raw_ptr_unowned() const { void *void_ptr = load_impl.unowned_void_ptr_from_void_ptr_capsule; if (void_ptr == nullptr) { - void_ptr = load_impl.unowned_void_ptr_from_direct_conversion; + void_ptr = load_impl.unowned_void_ptr_from_direct_conversion; } if (void_ptr == nullptr) { if (have_holder()) { throw_if_uninitialized_or_disowned_holder(); void_ptr = holder().template as_raw_ptr_unowned(); - } else if (load_impl.loaded_v_h.vh != nullptr) + } else if (load_impl.loaded_v_h.vh != nullptr) { void_ptr = load_impl.loaded_v_h.value_ptr(); - if (void_ptr == nullptr) + } + if (void_ptr == nullptr) { return nullptr; + } } return convert_type(void_ptr); } T &loaded_as_lvalue_ref() const { T *raw_ptr = loaded_as_raw_ptr_unowned(); - if (raw_ptr == nullptr) + if (raw_ptr == nullptr) { throw reference_cast_error(); + } return *raw_ptr; } @@ -433,32 +451,35 @@ struct smart_holder_type_caster_load { throw cast_error("Unowned pointer from a void pointer capsule cannot be converted to a" " std::shared_ptr."); } - if (load_impl.unowned_void_ptr_from_direct_conversion != nullptr) + if (load_impl.unowned_void_ptr_from_direct_conversion != nullptr) { throw cast_error("Unowned pointer from direct conversion cannot be converted to a" " std::shared_ptr."); - if (!have_holder()) + } + if (!have_holder()) { return nullptr; + } throw_if_uninitialized_or_disowned_holder(); holder_type &hld = holder(); hld.ensure_is_not_disowned("loaded_as_shared_ptr"); if (hld.vptr_is_using_noop_deleter) { throw std::runtime_error("Non-owning holder (loaded_as_shared_ptr)."); } - auto void_raw_ptr = hld.template as_raw_ptr_unowned(); - auto type_raw_ptr = convert_type(void_raw_ptr); + auto *void_raw_ptr = hld.template as_raw_ptr_unowned(); + auto *type_raw_ptr = convert_type(void_raw_ptr); if (hld.pointee_depends_on_holder_owner) { - auto vptr_gd_ptr = std::get_deleter(hld.vptr); + auto *vptr_gd_ptr = std::get_deleter(hld.vptr); if (vptr_gd_ptr != nullptr) { std::shared_ptr released_ptr = vptr_gd_ptr->released_ptr.lock(); - if (released_ptr) + if (released_ptr) { return std::shared_ptr(released_ptr, type_raw_ptr); + } std::shared_ptr to_be_released( type_raw_ptr, shared_ptr_trampoline_self_life_support(load_impl.loaded_v_h.inst)); vptr_gd_ptr->released_ptr = to_be_released; return to_be_released; } - auto sptsls_ptr = std::get_deleter(hld.vptr); + auto *sptsls_ptr = std::get_deleter(hld.vptr); if (sptsls_ptr != nullptr) { // This code is reachable only if there are multiple registered_instances for the // same pointee. @@ -491,11 +512,13 @@ struct smart_holder_type_caster_load { throw cast_error("Unowned pointer from a void pointer capsule cannot be converted to a" " std::unique_ptr."); } - if (load_impl.unowned_void_ptr_from_direct_conversion != nullptr) + if (load_impl.unowned_void_ptr_from_direct_conversion != nullptr) { throw cast_error("Unowned pointer from direct conversion cannot be converted to a" " std::unique_ptr."); - if (!have_holder()) + } + if (!have_holder()) { return nullptr; + } throw_if_uninitialized_or_disowned_holder(); throw_if_instance_is_currently_owned_by_shared_ptr(); holder().ensure_is_not_disowned(context); @@ -572,7 +595,7 @@ private: T *convert_type(void *void_ptr) const { if (void_ptr != nullptr && load_impl.loaded_v_h_cpptype != nullptr && !load_impl.reinterpret_cast_deemed_ok && !load_impl.implicit_casts.empty()) { - for (auto implicit_cast: load_impl.implicit_casts) { + for (auto implicit_cast : load_impl.implicit_casts) { void_ptr = implicit_cast(void_ptr); } } @@ -603,8 +626,10 @@ struct smart_holder_type_caster : smart_holder_type_caster_load, static handle cast(T const &src, return_value_policy policy, handle parent) { // type_caster_base BEGIN - if (policy == return_value_policy::automatic || policy == return_value_policy::automatic_reference) + if (policy == return_value_policy::automatic + || policy == return_value_policy::automatic_reference) { policy = return_value_policy::copy; + } return cast(&src, policy, parent); // type_caster_base END } @@ -660,18 +685,21 @@ struct smart_holder_type_caster : smart_holder_type_caster_load, void *(*copy_constructor)(const void *), void *(*move_constructor)(const void *), const void *existing_holder = nullptr) { - if (!tinfo) // no type info: error will be set already + if (!tinfo) { // no type info: error will be set already return handle(); + } void *src = const_cast(_src); - if (src == nullptr) + if (src == nullptr) { return none().release(); + } - if (handle existing_inst = find_registered_python_instance(src, tinfo)) + if (handle existing_inst = find_registered_python_instance(src, tinfo)) { return existing_inst; + } auto inst = reinterpret_steal(make_new_instance(tinfo->type)); - auto wrapper = reinterpret_cast(inst.ptr()); + auto *wrapper = reinterpret_cast(inst.ptr()); wrapper->owned = false; void *&valueptr = values_and_holders(wrapper).begin()->value_ptr(); @@ -689,9 +717,9 @@ struct smart_holder_type_caster : smart_holder_type_caster_load, break; case return_value_policy::copy: - if (copy_constructor) + if (copy_constructor) { valueptr = copy_constructor(src); - else { + } else { #if defined(NDEBUG) throw cast_error("return_value_policy = copy, but type is " "non-copyable! (compile in debug mode for details)"); @@ -706,11 +734,11 @@ struct smart_holder_type_caster : smart_holder_type_caster_load, break; case return_value_policy::move: - if (move_constructor) + if (move_constructor) { valueptr = move_constructor(src); - else if (copy_constructor) + } else if (copy_constructor) { valueptr = copy_constructor(src); - else { + } else { #if defined(NDEBUG) throw cast_error("return_value_policy = move, but type is neither " "movable nor copyable! " @@ -761,20 +789,23 @@ struct smart_holder_type_caster> : smart_holder_type_caster_l case return_value_policy::reference_internal: break; } - if (!src) + if (!src) { return none().release(); + } auto src_raw_ptr = src.get(); auto st = type_caster_base::src_and_type(src_raw_ptr); - if (st.second == nullptr) + if (st.second == nullptr) { return handle(); // no type info: error will be set already + } void *src_raw_void_ptr = static_cast(src_raw_ptr); const detail::type_info *tinfo = st.second; - if (handle existing_inst = find_registered_python_instance(src_raw_void_ptr, tinfo)) + if (handle existing_inst = find_registered_python_instance(src_raw_void_ptr, tinfo)) { // SMART_HOLDER_WIP: MISSING: Enforcement of consistency with existing smart_holder. // SMART_HOLDER_WIP: MISSING: keep_alive. return existing_inst; + } auto inst = reinterpret_steal(make_new_instance(tinfo->type)); auto *inst_raw_ptr = reinterpret_cast(inst.ptr()); @@ -785,8 +816,9 @@ struct smart_holder_type_caster> : smart_holder_type_caster_l auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(src); tinfo->init_instance(inst_raw_ptr, static_cast(&smhldr)); - if (policy == return_value_policy::reference_internal) + if (policy == return_value_policy::reference_internal) { keep_alive_impl(inst, parent); + } return inst.release(); } @@ -830,13 +862,15 @@ struct smart_holder_type_caster> : smart_holder_type_caste // SMART_HOLDER_WIP: IMPROVABLE: Error message. throw cast_error("Invalid return_value_policy for unique_ptr."); } - if (!src) + if (!src) { return none().release(); + } auto src_raw_ptr = src.get(); auto st = type_caster_base::src_and_type(src_raw_ptr); - if (st.second == nullptr) + if (st.second == nullptr) { return handle(); // no type info: error will be set already + } void *src_raw_void_ptr = static_cast(src_raw_ptr); const detail::type_info *tinfo = st.second; @@ -872,19 +906,23 @@ struct smart_holder_type_caster> : smart_holder_type_caste /*void_cast_raw_ptr*/ false); tinfo->init_instance(inst_raw_ptr, static_cast(&smhldr)); - if (policy == return_value_policy::reference_internal) + if (policy == return_value_policy::reference_internal) { keep_alive_impl(inst, parent); + } return inst.release(); } static handle cast(const std::unique_ptr &src, return_value_policy policy, handle parent) { - if (!src) + if (!src) { return none().release(); - if (policy == return_value_policy::automatic) + } + if (policy == return_value_policy::automatic) { policy = return_value_policy::reference_internal; - if (policy != return_value_policy::reference_internal) + } + if (policy != return_value_policy::reference_internal) { throw cast_error("Invalid return_value_policy for unique_ptr&"); + } return smart_holder_type_caster::cast(src.get(), policy, parent); } diff --git a/include/pybind11/smart_holder.h b/include/pybind11/smart_holder.h index f852f77e2..03ec4bb82 100644 --- a/include/pybind11/smart_holder.h +++ b/include/pybind11/smart_holder.h @@ -4,9 +4,9 @@ #pragma once +#include "pybind11.h" #include "detail/common.h" #include "detail/smart_holder_type_casters.h" -#include "pybind11.h" #undef PYBIND11_SH_AVL // Undoing #define in pybind11.h diff --git a/tests/test_class_sh_basic.cpp b/tests/test_class_sh_basic.cpp index 74dad47ad..502f48a2c 100644 --- a/tests/test_class_sh_basic.cpp +++ b/tests/test_class_sh_basic.cpp @@ -1,7 +1,7 @@ -#include "pybind11_tests.h" - #include +#include "pybind11_tests.h" + #include #include #include diff --git a/tests/test_class_sh_basic.py b/tests/test_class_sh_basic.py index ba407a46a..2b62d0242 100644 --- a/tests/test_class_sh_basic.py +++ b/tests/test_class_sh_basic.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Importing re before pytest after observing a PyPy CI flake when importing pytest first. import re diff --git a/tests/test_class_sh_disowning.cpp b/tests/test_class_sh_disowning.cpp index a84571c60..f934852f6 100644 --- a/tests/test_class_sh_disowning.cpp +++ b/tests/test_class_sh_disowning.cpp @@ -1,7 +1,7 @@ -#include "pybind11_tests.h" - #include +#include "pybind11_tests.h" + #include namespace pybind11_tests { diff --git a/tests/test_class_sh_disowning.py b/tests/test_class_sh_disowning.py index f98157138..f4d247181 100644 --- a/tests/test_class_sh_disowning.py +++ b/tests/test_class_sh_disowning.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import print_function - import pytest from pybind11_tests import class_sh_disowning as m diff --git a/tests/test_class_sh_disowning_mi.cpp b/tests/test_class_sh_disowning_mi.cpp index 9c7ef6041..86333e864 100644 --- a/tests/test_class_sh_disowning_mi.cpp +++ b/tests/test_class_sh_disowning_mi.cpp @@ -1,7 +1,7 @@ -#include "pybind11_tests.h" - #include +#include "pybind11_tests.h" + #include namespace pybind11_tests { diff --git a/tests/test_class_sh_disowning_mi.py b/tests/test_class_sh_disowning_mi.py index dcc100673..3579a5c0b 100644 --- a/tests/test_class_sh_disowning_mi.py +++ b/tests/test_class_sh_disowning_mi.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import pytest import env # noqa: F401 @@ -77,7 +76,7 @@ class MI1(m.Base1, m.Base2): m.Base2.__init__(self, j) -class B1(object): +class B1: def v(self): return 1 @@ -119,7 +118,7 @@ class B2(B1): return 2 -class B3(object): +class B3: def v(self): return 3 diff --git a/tests/test_class_sh_factory_constructors.cpp b/tests/test_class_sh_factory_constructors.cpp index dae8aa037..937672cbd 100644 --- a/tests/test_class_sh_factory_constructors.cpp +++ b/tests/test_class_sh_factory_constructors.cpp @@ -1,7 +1,7 @@ -#include "pybind11_tests.h" - #include +#include "pybind11_tests.h" + #include #include diff --git a/tests/test_class_sh_factory_constructors.py b/tests/test_class_sh_factory_constructors.py index c5227b40d..ad602cd9b 100644 --- a/tests/test_class_sh_factory_constructors.py +++ b/tests/test_class_sh_factory_constructors.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import pytest from pybind11_tests import class_sh_factory_constructors as m diff --git a/tests/test_class_sh_inheritance.cpp b/tests/test_class_sh_inheritance.cpp index d5b05cd8c..d8f7da0e2 100644 --- a/tests/test_class_sh_inheritance.cpp +++ b/tests/test_class_sh_inheritance.cpp @@ -1,7 +1,7 @@ -#include "pybind11_tests.h" - #include +#include "pybind11_tests.h" + #include namespace pybind11_tests { diff --git a/tests/test_class_sh_inheritance.py b/tests/test_class_sh_inheritance.py index 69f9131d7..eaf53a5c6 100644 --- a/tests/test_class_sh_inheritance.py +++ b/tests/test_class_sh_inheritance.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from pybind11_tests import class_sh_inheritance as m diff --git a/tests/test_class_sh_module_local.py b/tests/test_class_sh_module_local.py index 98b161d6c..a88e3fc1c 100644 --- a/tests/test_class_sh_module_local.py +++ b/tests/test_class_sh_module_local.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import class_sh_module_local_0 as m0 import class_sh_module_local_1 as m1 import class_sh_module_local_2 as m2 diff --git a/tests/test_class_sh_shared_ptr_copy_move.cpp b/tests/test_class_sh_shared_ptr_copy_move.cpp index 2828b6710..a803d49f8 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.cpp +++ b/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -1,7 +1,7 @@ -#include "pybind11_tests.h" - #include +#include "pybind11_tests.h" + #include #include #include diff --git a/tests/test_class_sh_shared_ptr_copy_move.py b/tests/test_class_sh_shared_ptr_copy_move.py index 4ea47c9a6..bc7198f2e 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.py +++ b/tests/test_class_sh_shared_ptr_copy_move.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from pybind11_tests import class_sh_shared_ptr_copy_move as m @@ -25,8 +23,8 @@ def test_smhld_move(): def _check_property(foo_typ, prop_typ, policy): o = m.Outer() - name = "{}_{}_{}".format(foo_typ, prop_typ, policy) - history = "Foo{}_Outer".format(foo_typ) + name = f"{foo_typ}_{prop_typ}_{policy}" + history = f"Foo{foo_typ}_Outer" f = getattr(o, name) assert f.get_history() == history # and try again to check that o did not get changed diff --git a/tests/test_class_sh_trampoline_basic.cpp b/tests/test_class_sh_trampoline_basic.cpp index a3de5344b..e31bcf719 100644 --- a/tests/test_class_sh_trampoline_basic.cpp +++ b/tests/test_class_sh_trampoline_basic.cpp @@ -1,7 +1,7 @@ -#include "pybind11_tests.h" - #include +#include "pybind11_tests.h" + #include namespace pybind11_tests { diff --git a/tests/test_class_sh_trampoline_basic.py b/tests/test_class_sh_trampoline_basic.py index 8c82decf8..e87cc8b4d 100644 --- a/tests/test_class_sh_trampoline_basic.py +++ b/tests/test_class_sh_trampoline_basic.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import pytest from pybind11_tests import class_sh_trampoline_basic as m @@ -6,7 +5,7 @@ from pybind11_tests import class_sh_trampoline_basic as m class PyDrvd0(m.Abase0): def __init__(self, val): - super(PyDrvd0, self).__init__(val) + super().__init__(val) def Add(self, other_val): # noqa: N802 return self.Get() * 100 + other_val @@ -14,7 +13,7 @@ class PyDrvd0(m.Abase0): class PyDrvd1(m.Abase1): def __init__(self, val): - super(PyDrvd1, self).__init__(val) + super().__init__(val) def Add(self, other_val): # noqa: N802 return self.Get() * 200 + other_val diff --git a/tests/test_class_sh_trampoline_self_life_support.cpp b/tests/test_class_sh_trampoline_self_life_support.cpp index 444a1ab0a..c1eb03543 100644 --- a/tests/test_class_sh_trampoline_self_life_support.cpp +++ b/tests/test_class_sh_trampoline_self_life_support.cpp @@ -54,7 +54,7 @@ TEST_SUBMODULE(class_sh_trampoline_self_life_support, m) { py::object o2 = py::none(); // This is very unusual, but needed to directly exercise the trampoline_self_life_support // CpCtor, MvCtor, operator= lvalue, operator= rvalue. - auto obj_trampoline = dynamic_cast(obj.get()); + auto *obj_trampoline = dynamic_cast(obj.get()); if (obj_trampoline != nullptr) { switch (action_id) { case 0: { // CpCtor diff --git a/tests/test_class_sh_trampoline_self_life_support.py b/tests/test_class_sh_trampoline_self_life_support.py index f159e06a6..f86d4b212 100644 --- a/tests/test_class_sh_trampoline_self_life_support.py +++ b/tests/test_class_sh_trampoline_self_life_support.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import pytest import pybind11_tests.class_sh_trampoline_self_life_support as m diff --git a/tests/test_class_sh_trampoline_shared_from_this.cpp b/tests/test_class_sh_trampoline_shared_from_this.cpp index 2703c60b5..ae9f27465 100644 --- a/tests/test_class_sh_trampoline_shared_from_this.cpp +++ b/tests/test_class_sh_trampoline_shared_from_this.cpp @@ -57,13 +57,15 @@ struct SftSharedPtrStash { stash.push_back(sft); } std::string history(unsigned i) { - if (i < stash.size()) + if (i < stash.size()) { return stash[i]->history; + } return "OutOfRange"; } long use_count(unsigned i) { - if (i < stash.size()) + if (i < stash.size()) { return stash[i].use_count(); + } return -1; } }; diff --git a/tests/test_class_sh_trampoline_shared_from_this.py b/tests/test_class_sh_trampoline_shared_from_this.py index e64468e08..22c407290 100644 --- a/tests/test_class_sh_trampoline_shared_from_this.py +++ b/tests/test_class_sh_trampoline_shared_from_this.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import sys import weakref @@ -220,7 +219,7 @@ WORKAROUND_ENABLING_ROLLBACK_OF_PR3068 = env.LINUX and sys.version_info == (3, 9 def test_std_make_shared_factory(): class PySftMakeShared(m.Sft): def __init__(self, history): - super(PySftMakeShared, self).__init__(history, 0) + super().__init__(history, 0) obj = PySftMakeShared("PySftMakeShared") assert obj.history == "PySftMakeShared" diff --git a/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.cpp b/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.cpp index 33b67c291..5773f08e8 100644 --- a/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.cpp +++ b/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.cpp @@ -2,11 +2,11 @@ // All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -#include - #include "pybind11/smart_holder.h" #include "pybind11_tests.h" +#include + namespace { // For testing whether a python subclass of a C++ object dies when the @@ -17,7 +17,7 @@ struct SpBase { // returns true if there's an associated python instance bool has_python_instance() { - auto tinfo = py::detail::get_type_info(typeid(SpBase)); + auto *tinfo = py::detail::get_type_info(typeid(SpBase)); return (bool) py::detail::get_object_handle(this, tinfo); } diff --git a/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.py b/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.py index ec72800f3..912d22cbe 100644 --- a/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.py +++ b/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import pytest import pybind11_tests.class_sh_trampoline_shared_ptr_cpp_arg as m @@ -136,7 +135,7 @@ def test_infinite(): def test_std_make_shared_factory(): class PyChild(m.SpBase): def __init__(self): - super(PyChild, self).__init__(0) + super().__init__(0) obj = PyChild() while True: diff --git a/tests/test_class_sh_trampoline_unique_ptr.py b/tests/test_class_sh_trampoline_unique_ptr.py index 932db659e..c598079e4 100644 --- a/tests/test_class_sh_trampoline_unique_ptr.py +++ b/tests/test_class_sh_trampoline_unique_ptr.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - import pybind11_tests.class_sh_trampoline_unique_ptr as m diff --git a/tests/test_class_sh_unique_ptr_member.cpp b/tests/test_class_sh_unique_ptr_member.cpp index 89e5885ea..3de12fe62 100644 --- a/tests/test_class_sh_unique_ptr_member.cpp +++ b/tests/test_class_sh_unique_ptr_member.cpp @@ -1,7 +1,7 @@ -#include "pybind11_tests.h" - #include +#include "pybind11_tests.h" + #include namespace pybind11_tests { diff --git a/tests/test_class_sh_unique_ptr_member.py b/tests/test_class_sh_unique_ptr_member.py index f598e4ee5..e369213e0 100644 --- a/tests/test_class_sh_unique_ptr_member.py +++ b/tests/test_class_sh_unique_ptr_member.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import pytest from pybind11_tests import class_sh_unique_ptr_member as m diff --git a/tests/test_class_sh_virtual_py_cpp_mix.cpp b/tests/test_class_sh_virtual_py_cpp_mix.cpp index e8c17f107..2fa9990a2 100644 --- a/tests/test_class_sh_virtual_py_cpp_mix.cpp +++ b/tests/test_class_sh_virtual_py_cpp_mix.cpp @@ -1,7 +1,7 @@ -#include "pybind11_tests.h" - #include +#include "pybind11_tests.h" + #include namespace pybind11_tests { diff --git a/tests/test_class_sh_virtual_py_cpp_mix.py b/tests/test_class_sh_virtual_py_cpp_mix.py index e85be05fe..be1d516b1 100644 --- a/tests/test_class_sh_virtual_py_cpp_mix.py +++ b/tests/test_class_sh_virtual_py_cpp_mix.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import pytest from pybind11_tests import class_sh_virtual_py_cpp_mix as m diff --git a/tests/test_class_sh_void_ptr_capsule.cpp b/tests/test_class_sh_void_ptr_capsule.cpp index f8e312dcf..f6597d568 100644 --- a/tests/test_class_sh_void_ptr_capsule.cpp +++ b/tests/test_class_sh_void_ptr_capsule.cpp @@ -1,7 +1,7 @@ -#include "pybind11_tests.h" - #include +#include "pybind11_tests.h" + #include namespace pybind11_tests { @@ -27,67 +27,53 @@ struct HelperBase { virtual int get() const { return 100; } }; -struct Valid: public HelperBase { +struct Valid : public HelperBase { int get() const override { return 101; } - PyObject* as_pybind11_tests_class_sh_void_ptr_capsule_Valid() { - void* vptr = dynamic_cast(this); - capsule_generated = true; - // We assume vptr out lives the capsule, so we use nullptr for the - // destructor. - return PyCapsule_New( - vptr, "::pybind11_tests::class_sh_void_ptr_capsule::Valid", - nullptr); + PyObject *as_pybind11_tests_class_sh_void_ptr_capsule_Valid() { + void *vptr = dynamic_cast(this); + capsule_generated = true; + // We assume vptr out lives the capsule, so we use nullptr for the + // destructor. + return PyCapsule_New(vptr, "::pybind11_tests::class_sh_void_ptr_capsule::Valid", nullptr); } }; -struct NoConversion: public HelperBase { +struct NoConversion : public HelperBase { int get() const override { return 102; } }; -struct NoCapsuleReturned: public HelperBase { +struct NoCapsuleReturned : public HelperBase { int get() const override { return 103; } - PyObject* as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned() { - capsule_generated = true; - Py_XINCREF(Py_None); - return Py_None; + PyObject *as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned() { + capsule_generated = true; + Py_XINCREF(Py_None); + return Py_None; } }; -struct AsAnotherObject: public HelperBase { +struct AsAnotherObject : public HelperBase { int get() const override { return 104; } - PyObject* as_pybind11_tests_class_sh_void_ptr_capsule_Valid() { - void* vptr = dynamic_cast(this); - capsule_generated = true; - // We assume vptr out lives the capsule, so we use nullptr for the - // destructor. - return PyCapsule_New( - vptr, "::pybind11_tests::class_sh_void_ptr_capsule::Valid", - nullptr); + PyObject *as_pybind11_tests_class_sh_void_ptr_capsule_Valid() { + void *vptr = dynamic_cast(this); + capsule_generated = true; + // We assume vptr out lives the capsule, so we use nullptr for the + // destructor. + return PyCapsule_New(vptr, "::pybind11_tests::class_sh_void_ptr_capsule::Valid", nullptr); } }; -int get_from_valid_capsule(const Valid* c) { - return c->get(); -} +int get_from_valid_capsule(const Valid *c) { return c->get(); } -int get_from_shared_ptr_valid_capsule(const std::shared_ptr &c) { - return c->get(); -} +int get_from_shared_ptr_valid_capsule(const std::shared_ptr &c) { return c->get(); } -int get_from_unique_ptr_valid_capsule(std::unique_ptr c) { - return c->get(); -} +int get_from_unique_ptr_valid_capsule(std::unique_ptr c) { return c->get(); } -int get_from_no_conversion_capsule(const NoConversion* c) { - return c->get(); -} +int get_from_no_conversion_capsule(const NoConversion *c) { return c->get(); } -int get_from_no_capsule_returned(const NoCapsuleReturned* c) { - return c->get(); -} +int get_from_no_capsule_returned(const NoCapsuleReturned *c) { return c->get(); } } // namespace class_sh_void_ptr_capsule } // namespace pybind11_tests @@ -108,35 +94,33 @@ TEST_SUBMODULE(class_sh_void_ptr_capsule, m) { py::classh(m, "Valid") .def(py::init<>()) - .def("as_pybind11_tests_class_sh_void_ptr_capsule_Valid", - [](HelperBase* self) { - auto obj = dynamic_cast(self); - assert(obj != nullptr); - PyObject* capsule = obj->as_pybind11_tests_class_sh_void_ptr_capsule_Valid(); - return pybind11::reinterpret_steal(capsule); + .def("as_pybind11_tests_class_sh_void_ptr_capsule_Valid", [](HelperBase *self) { + auto *obj = dynamic_cast(self); + assert(obj != nullptr); + PyObject *capsule = obj->as_pybind11_tests_class_sh_void_ptr_capsule_Valid(); + return pybind11::reinterpret_steal(capsule); }); - py::classh(m, "NoConversion") - .def(py::init<>()); + py::classh(m, "NoConversion").def(py::init<>()); py::classh(m, "NoCapsuleReturned") .def(py::init<>()) .def("as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned", - [](HelperBase* self) { - auto obj = dynamic_cast(self); - assert(obj != nullptr); - PyObject* capsule = obj->as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned(); - return pybind11::reinterpret_steal(capsule); - }); + [](HelperBase *self) { + auto *obj = dynamic_cast(self); + assert(obj != nullptr); + PyObject *capsule + = obj->as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned(); + return pybind11::reinterpret_steal(capsule); + }); py::classh(m, "AsAnotherObject") .def(py::init<>()) - .def("as_pybind11_tests_class_sh_void_ptr_capsule_Valid", - [](HelperBase* self) { - auto obj = dynamic_cast(self); - assert(obj != nullptr); - PyObject* capsule = obj->as_pybind11_tests_class_sh_void_ptr_capsule_Valid(); - return pybind11::reinterpret_steal(capsule); + .def("as_pybind11_tests_class_sh_void_ptr_capsule_Valid", [](HelperBase *self) { + auto *obj = dynamic_cast(self); + assert(obj != nullptr); + PyObject *capsule = obj->as_pybind11_tests_class_sh_void_ptr_capsule_Valid(); + return pybind11::reinterpret_steal(capsule); }); m.def("get_from_valid_capsule", &get_from_valid_capsule); diff --git a/tests/test_class_sh_void_ptr_capsule.py b/tests/test_class_sh_void_ptr_capsule.py index 97794f17d..d5bf44961 100644 --- a/tests/test_class_sh_void_ptr_capsule.py +++ b/tests/test_class_sh_void_ptr_capsule.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import pytest from pybind11_tests import class_sh_void_ptr_capsule as m diff --git a/tests/test_classh_mock.py b/tests/test_classh_mock.py index 03d323eae..7d0ffbe8b 100644 --- a/tests/test_classh_mock.py +++ b/tests/test_classh_mock.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from pybind11_tests import classh_mock as m diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp index de38401ca..1f769ef78 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -298,8 +298,11 @@ PYBIND11_TYPE_CASTER_BASE_HOLDER(SharedFromThisRef::B, std::shared_ptr) PYBIND11_TYPE_CASTER_BASE_HOLDER(SharedFromThisVirt, std::shared_ptr) PYBIND11_TYPE_CASTER_BASE_HOLDER(C, custom_unique_ptr) -PYBIND11_TYPE_CASTER_BASE_HOLDER(TypeForHolderWithAddressOf, shared_ptr_with_addressof_operator) -PYBIND11_TYPE_CASTER_BASE_HOLDER(TypeForMoveOnlyHolderWithAddressOf, unique_ptr_with_addressof_operator) +PYBIND11_TYPE_CASTER_BASE_HOLDER(TypeForHolderWithAddressOf, + shared_ptr_with_addressof_operator) +PYBIND11_TYPE_CASTER_BASE_HOLDER( + TypeForMoveOnlyHolderWithAddressOf, + unique_ptr_with_addressof_operator) PYBIND11_TYPE_CASTER_BASE_HOLDER(HeldByDefaultHolder, std::unique_ptr) PYBIND11_TYPE_CASTER_BASE_HOLDER(ElementBase, std::shared_ptr) PYBIND11_TYPE_CASTER_BASE_HOLDER(ElementA, std::shared_ptr) @@ -308,7 +311,8 @@ PYBIND11_TYPE_CASTER_BASE_HOLDER(ElementList, std::shared_ptr) #ifdef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT // To prevent triggering a static_assert in the smart_holder code. // This is a very special case, because the associated test exercises a holder mismatch. -namespace pybind11 { namespace detail { +namespace pybind11 { +namespace detail { template <> class type_caster> : public copyable_holder_caster> {}; diff --git a/ubench/holder_comparison.py b/ubench/holder_comparison.py index 10f80469c..5e231ef06 100644 --- a/ubench/holder_comparison.py +++ b/ubench/holder_comparison.py @@ -1,7 +1,5 @@ -# -*- coding: utf-8 -*- """Simple comparison of holder performances, relative to unique_ptr holder.""" -from __future__ import absolute_import, division, print_function import collections import sys import time @@ -74,7 +72,7 @@ def run(args: List[str]) -> None: for size_exponent in range( size_exponent_min, size_exponent_max + 1, size_exponent_step ): - data_size = 2 ** size_exponent + data_size = 2**size_exponent pflush(data_size, "data_size") ratios: Dict[str, List[float]] = collections.defaultdict(list) call_repetitions = None diff --git a/ubench/holder_comparison_extract_sheet_data.py b/ubench/holder_comparison_extract_sheet_data.py index 83601fee0..7bcac9d9c 100644 --- a/ubench/holder_comparison_extract_sheet_data.py +++ b/ubench/holder_comparison_extract_sheet_data.py @@ -1,7 +1,5 @@ -# -*- coding: utf-8 -*- """Extract mean ratios from holder_comparison.py output.""" -from __future__ import absolute_import, division, print_function import sys from typing import List, Optional