From a28ea8ccc5b98af73a2306ca0e43bcf0c82d39d8 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 8 Apr 2025 20:46:24 -0700 Subject: [PATCH] Eliminate `pybindit` namespace (#5607) * Factor out pybind11/detail/pybind11_namespace_macros.h from pybind11/detail/common.h * Eliminate `pybindit` namespace, use pybind11/detail/pybind11_namespace_macros.h instead. * Fix oversight: add pybind11_namespace_macros.h in tests/extra_python_package/test_files.py * [skip ci] Add copyright notice. --- include/pybind11/detail/common.h | 75 +---------------- .../detail/pybind11_namespace_macros.h | 82 +++++++++++++++++++ include/pybind11/detail/struct_smart_holder.h | 13 ++- include/pybind11/detail/type_caster_base.h | 7 +- include/pybind11/detail/using_smart_holder.h | 2 +- tests/extra_python_package/test_files.py | 1 + tests/pure_cpp/smart_holder_poc.h | 14 ++-- tests/pure_cpp/smart_holder_poc_test.cpp | 4 +- 8 files changed, 103 insertions(+), 95 deletions(-) create mode 100644 include/pybind11/detail/pybind11_namespace_macros.h diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 92b2af538..d1cf8bd95 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -22,80 +22,7 @@ // Additional convention: 0xD = dev #define PYBIND11_VERSION_HEX 0x030000D1 -// Define some generic pybind11 helper macros for warning management. -// -// Note that compiler-specific push/pop pairs are baked into the -// PYBIND11_NAMESPACE_BEGIN/PYBIND11_NAMESPACE_END pair of macros. Therefore manual -// PYBIND11_WARNING_PUSH/PYBIND11_WARNING_POP are usually only needed in `#include` sections. -// -// If you find you need to suppress a warning, please try to make the suppression as local as -// possible using these macros. Please also be sure to push/pop with the pybind11 macros. Please -// only use compiler specifics if you need to check specific versions, e.g. Apple Clang vs. vanilla -// Clang. -#if defined(_MSC_VER) -# define PYBIND11_COMPILER_MSVC -# define PYBIND11_PRAGMA(...) __pragma(__VA_ARGS__) -# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning(push)) -# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning(pop)) -#elif defined(__INTEL_COMPILER) -# define PYBIND11_COMPILER_INTEL -# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__) -# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning push) -# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning pop) -#elif defined(__clang__) -# define PYBIND11_COMPILER_CLANG -# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__) -# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(clang diagnostic push) -# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(clang diagnostic pop) -#elif defined(__GNUC__) -# define PYBIND11_COMPILER_GCC -# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__) -# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(GCC diagnostic push) -# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(GCC diagnostic pop) -#endif - -#ifdef PYBIND11_COMPILER_MSVC -# define PYBIND11_WARNING_DISABLE_MSVC(name) PYBIND11_PRAGMA(warning(disable : name)) -#else -# define PYBIND11_WARNING_DISABLE_MSVC(name) -#endif - -#ifdef PYBIND11_COMPILER_CLANG -# define PYBIND11_WARNING_DISABLE_CLANG(name) PYBIND11_PRAGMA(clang diagnostic ignored name) -#else -# define PYBIND11_WARNING_DISABLE_CLANG(name) -#endif - -#ifdef PYBIND11_COMPILER_GCC -# define PYBIND11_WARNING_DISABLE_GCC(name) PYBIND11_PRAGMA(GCC diagnostic ignored name) -#else -# define PYBIND11_WARNING_DISABLE_GCC(name) -#endif - -#ifdef PYBIND11_COMPILER_INTEL -# define PYBIND11_WARNING_DISABLE_INTEL(name) PYBIND11_PRAGMA(warning disable name) -#else -# define PYBIND11_WARNING_DISABLE_INTEL(name) -#endif - -#define PYBIND11_NAMESPACE_BEGIN(name) \ - namespace name { \ - PYBIND11_WARNING_PUSH - -#define PYBIND11_NAMESPACE_END(name) \ - PYBIND11_WARNING_POP \ - } - -// Robust support for some features and loading modules compiled against different pybind versions -// requires forcing hidden visibility on pybind code, so we enforce this by setting the attribute -// on the main `pybind11` namespace. -#if !defined(PYBIND11_NAMESPACE) -# ifdef __GNUG__ -# define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden"))) -# else -# define PYBIND11_NAMESPACE pybind11 -# endif -#endif +#include "pybind11_namespace_macros.h" #if !(defined(_MSC_VER) && __cplusplus == 199711L) # if __cplusplus >= 201402L diff --git a/include/pybind11/detail/pybind11_namespace_macros.h b/include/pybind11/detail/pybind11_namespace_macros.h new file mode 100644 index 000000000..7137fb5f7 --- /dev/null +++ b/include/pybind11/detail/pybind11_namespace_macros.h @@ -0,0 +1,82 @@ +// Copyright (c) 2016-2025 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +// PLEASE DO NOT ADD ANY INCLUDES HERE + +// Define some generic pybind11 helper macros for warning management. +// +// Note that compiler-specific push/pop pairs are baked into the +// PYBIND11_NAMESPACE_BEGIN/PYBIND11_NAMESPACE_END pair of macros. Therefore manual +// PYBIND11_WARNING_PUSH/PYBIND11_WARNING_POP are usually only needed in `#include` sections. +// +// If you find you need to suppress a warning, please try to make the suppression as local as +// possible using these macros. Please also be sure to push/pop with the pybind11 macros. Please +// only use compiler specifics if you need to check specific versions, e.g. Apple Clang vs. vanilla +// Clang. +#if defined(_MSC_VER) +# define PYBIND11_COMPILER_MSVC +# define PYBIND11_PRAGMA(...) __pragma(__VA_ARGS__) +# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning(push)) +# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning(pop)) +#elif defined(__INTEL_COMPILER) +# define PYBIND11_COMPILER_INTEL +# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__) +# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning push) +# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning pop) +#elif defined(__clang__) +# define PYBIND11_COMPILER_CLANG +# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__) +# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(clang diagnostic push) +# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(clang diagnostic pop) +#elif defined(__GNUC__) +# define PYBIND11_COMPILER_GCC +# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__) +# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(GCC diagnostic push) +# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(GCC diagnostic pop) +#endif + +#ifdef PYBIND11_COMPILER_MSVC +# define PYBIND11_WARNING_DISABLE_MSVC(name) PYBIND11_PRAGMA(warning(disable : name)) +#else +# define PYBIND11_WARNING_DISABLE_MSVC(name) +#endif + +#ifdef PYBIND11_COMPILER_CLANG +# define PYBIND11_WARNING_DISABLE_CLANG(name) PYBIND11_PRAGMA(clang diagnostic ignored name) +#else +# define PYBIND11_WARNING_DISABLE_CLANG(name) +#endif + +#ifdef PYBIND11_COMPILER_GCC +# define PYBIND11_WARNING_DISABLE_GCC(name) PYBIND11_PRAGMA(GCC diagnostic ignored name) +#else +# define PYBIND11_WARNING_DISABLE_GCC(name) +#endif + +#ifdef PYBIND11_COMPILER_INTEL +# define PYBIND11_WARNING_DISABLE_INTEL(name) PYBIND11_PRAGMA(warning disable name) +#else +# define PYBIND11_WARNING_DISABLE_INTEL(name) +#endif + +#define PYBIND11_NAMESPACE_BEGIN(name) \ + namespace name { \ + PYBIND11_WARNING_PUSH + +#define PYBIND11_NAMESPACE_END(name) \ + PYBIND11_WARNING_POP \ + } + +// Robust support for some features and loading modules compiled against different pybind versions +// requires forcing hidden visibility on pybind code, so we enforce this by setting the attribute +// on the main `pybind11` namespace. +#if !defined(PYBIND11_NAMESPACE) +# ifdef __GNUG__ +# define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden"))) +# else +# define PYBIND11_NAMESPACE pybind11 +# endif +#endif diff --git a/include/pybind11/detail/struct_smart_holder.h b/include/pybind11/detail/struct_smart_holder.h index 84ad8e00f..5466f8668 100644 --- a/include/pybind11/detail/struct_smart_holder.h +++ b/include/pybind11/detail/struct_smart_holder.h @@ -48,6 +48,8 @@ Details: #pragma once +#include "pybind11_namespace_macros.h" + #include #include #include @@ -56,11 +58,8 @@ Details: #include #include -// pybindit = Python Bindings Innovation Track. -// Currently not in pybind11 namespace to signal that this POC does not depend -// on any existing pybind11 functionality. -namespace pybindit { -namespace memory { +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(memory) // Default fallback. static constexpr bool type_has_shared_from_this(...) { return false; } @@ -355,5 +354,5 @@ struct smart_holder { } }; -} // namespace memory -} // namespace pybindit +PYBIND11_NAMESPACE_END(memory) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 9618b2181..d4f9a41e0 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -543,7 +543,7 @@ struct value_and_holder_helper { // have_holder() must be true or this function will fail. void throw_if_instance_is_currently_owned_by_shared_ptr() const { - auto *vptr_gd_ptr = std::get_deleter(holder().vptr); + auto *vptr_gd_ptr = std::get_deleter(holder().vptr); if (vptr_gd_ptr != nullptr && !vptr_gd_ptr->released_ptr.expired()) { throw value_error("Python instance is currently owned by a std::shared_ptr."); } @@ -770,7 +770,7 @@ struct load_helper : value_and_holder_helper { } auto *type_raw_ptr = static_cast(void_raw_ptr); if (python_instance_is_alias) { - 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) { @@ -790,8 +790,7 @@ struct load_helper : value_and_holder_helper { "loaded_v_h.inst == sptsls_ptr->self"); } } - if (sptsls_ptr != nullptr - || !pybindit::memory::type_has_shared_from_this(type_raw_ptr)) { + if (sptsls_ptr != nullptr || !memory::type_has_shared_from_this(type_raw_ptr)) { return std::shared_ptr( type_raw_ptr, shared_ptr_trampoline_self_life_support(loaded_v_h.inst)); } diff --git a/include/pybind11/detail/using_smart_holder.h b/include/pybind11/detail/using_smart_holder.h index 57f99b95f..903148962 100644 --- a/include/pybind11/detail/using_smart_holder.h +++ b/include/pybind11/detail/using_smart_holder.h @@ -11,7 +11,7 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -using pybindit::memory::smart_holder; +using pybind11::memory::smart_holder; PYBIND11_NAMESPACE_BEGIN(detail) diff --git a/tests/extra_python_package/test_files.py b/tests/extra_python_package/test_files.py index 86c37089a..9aca4d6d1 100644 --- a/tests/extra_python_package/test_files.py +++ b/tests/extra_python_package/test_files.py @@ -69,6 +69,7 @@ detail_headers = { "include/pybind11/detail/init.h", "include/pybind11/detail/internals.h", "include/pybind11/detail/native_enum_data.h", + "include/pybind11/detail/pybind11_namespace_macros.h", "include/pybind11/detail/struct_smart_holder.h", "include/pybind11/detail/type_caster_base.h", "include/pybind11/detail/typeid.h", diff --git a/tests/pure_cpp/smart_holder_poc.h b/tests/pure_cpp/smart_holder_poc.h index 20067b43e..e57ec13f4 100644 --- a/tests/pure_cpp/smart_holder_poc.h +++ b/tests/pure_cpp/smart_holder_poc.h @@ -6,12 +6,12 @@ #include "pybind11/detail/struct_smart_holder.h" -namespace pybindit { -namespace memory { -namespace smart_holder_poc { // Proof-of-Concept implementations. +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(memory) +PYBIND11_NAMESPACE_BEGIN(smart_holder_poc) // Proof-of-Concept implementations. struct PrivateESFT : private std::enable_shared_from_this {}; -static_assert(!pybindit::memory::type_has_shared_from_this(static_cast(nullptr)), +static_assert(!type_has_shared_from_this(static_cast(nullptr)), "should detect inaccessible base"); template @@ -50,6 +50,6 @@ std::unique_ptr as_unique_ptr(smart_holder &hld) { return std::unique_ptr(raw_ptr); } -} // namespace smart_holder_poc -} // namespace memory -} // namespace pybindit +PYBIND11_NAMESPACE_END(smart_holder_poc) +PYBIND11_NAMESPACE_END(memory) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/tests/pure_cpp/smart_holder_poc_test.cpp b/tests/pure_cpp/smart_holder_poc_test.cpp index 24ab643ee..0d3057b31 100644 --- a/tests/pure_cpp/smart_holder_poc_test.cpp +++ b/tests/pure_cpp/smart_holder_poc_test.cpp @@ -13,8 +13,8 @@ #define CATCH_CONFIG_MAIN #include "catch.hpp" -using pybindit::memory::smart_holder; -namespace poc = pybindit::memory::smart_holder_poc; +using pybind11::memory::smart_holder; +namespace poc = pybind11::memory::smart_holder_poc; namespace helpers {