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.
This commit is contained in:
Ralf W. Grosse-Kunstleve
2025-04-08 20:46:24 -07:00
committed by GitHub
parent 6d1f28fe2b
commit a28ea8ccc5
8 changed files with 103 additions and 95 deletions

View File

@@ -22,80 +22,7 @@
// Additional convention: 0xD = dev // Additional convention: 0xD = dev
#define PYBIND11_VERSION_HEX 0x030000D1 #define PYBIND11_VERSION_HEX 0x030000D1
// Define some generic pybind11 helper macros for warning management. #include "pybind11_namespace_macros.h"
//
// 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
#if !(defined(_MSC_VER) && __cplusplus == 199711L) #if !(defined(_MSC_VER) && __cplusplus == 199711L)
# if __cplusplus >= 201402L # if __cplusplus >= 201402L

View File

@@ -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

View File

@@ -48,6 +48,8 @@ Details:
#pragma once #pragma once
#include "pybind11_namespace_macros.h"
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
@@ -56,11 +58,8 @@ Details:
#include <typeinfo> #include <typeinfo>
#include <utility> #include <utility>
// pybindit = Python Bindings Innovation Track. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
// Currently not in pybind11 namespace to signal that this POC does not depend PYBIND11_NAMESPACE_BEGIN(memory)
// on any existing pybind11 functionality.
namespace pybindit {
namespace memory {
// Default fallback. // Default fallback.
static constexpr bool type_has_shared_from_this(...) { return false; } static constexpr bool type_has_shared_from_this(...) { return false; }
@@ -355,5 +354,5 @@ struct smart_holder {
} }
}; };
} // namespace memory PYBIND11_NAMESPACE_END(memory)
} // namespace pybindit PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

View File

@@ -543,7 +543,7 @@ struct value_and_holder_helper {
// have_holder() must be true or this function will fail. // have_holder() must be true or this function will fail.
void throw_if_instance_is_currently_owned_by_shared_ptr() const { void throw_if_instance_is_currently_owned_by_shared_ptr() const {
auto *vptr_gd_ptr = std::get_deleter<pybindit::memory::guarded_delete>(holder().vptr); auto *vptr_gd_ptr = std::get_deleter<memory::guarded_delete>(holder().vptr);
if (vptr_gd_ptr != nullptr && !vptr_gd_ptr->released_ptr.expired()) { if (vptr_gd_ptr != nullptr && !vptr_gd_ptr->released_ptr.expired()) {
throw value_error("Python instance is currently owned by a std::shared_ptr."); 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<T *>(void_raw_ptr); auto *type_raw_ptr = static_cast<T *>(void_raw_ptr);
if (python_instance_is_alias) { if (python_instance_is_alias) {
auto *vptr_gd_ptr = std::get_deleter<pybindit::memory::guarded_delete>(hld.vptr); auto *vptr_gd_ptr = std::get_deleter<memory::guarded_delete>(hld.vptr);
if (vptr_gd_ptr != nullptr) { if (vptr_gd_ptr != nullptr) {
std::shared_ptr<void> released_ptr = vptr_gd_ptr->released_ptr.lock(); std::shared_ptr<void> released_ptr = vptr_gd_ptr->released_ptr.lock();
if (released_ptr) { if (released_ptr) {
@@ -790,8 +790,7 @@ struct load_helper : value_and_holder_helper {
"loaded_v_h.inst == sptsls_ptr->self"); "loaded_v_h.inst == sptsls_ptr->self");
} }
} }
if (sptsls_ptr != nullptr if (sptsls_ptr != nullptr || !memory::type_has_shared_from_this(type_raw_ptr)) {
|| !pybindit::memory::type_has_shared_from_this(type_raw_ptr)) {
return std::shared_ptr<T>( return std::shared_ptr<T>(
type_raw_ptr, shared_ptr_trampoline_self_life_support(loaded_v_h.inst)); type_raw_ptr, shared_ptr_trampoline_self_life_support(loaded_v_h.inst));
} }

View File

@@ -11,7 +11,7 @@
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
using pybindit::memory::smart_holder; using pybind11::memory::smart_holder;
PYBIND11_NAMESPACE_BEGIN(detail) PYBIND11_NAMESPACE_BEGIN(detail)

View File

@@ -69,6 +69,7 @@ detail_headers = {
"include/pybind11/detail/init.h", "include/pybind11/detail/init.h",
"include/pybind11/detail/internals.h", "include/pybind11/detail/internals.h",
"include/pybind11/detail/native_enum_data.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/struct_smart_holder.h",
"include/pybind11/detail/type_caster_base.h", "include/pybind11/detail/type_caster_base.h",
"include/pybind11/detail/typeid.h", "include/pybind11/detail/typeid.h",

View File

@@ -6,12 +6,12 @@
#include "pybind11/detail/struct_smart_holder.h" #include "pybind11/detail/struct_smart_holder.h"
namespace pybindit { PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
namespace memory { PYBIND11_NAMESPACE_BEGIN(memory)
namespace smart_holder_poc { // Proof-of-Concept implementations. PYBIND11_NAMESPACE_BEGIN(smart_holder_poc) // Proof-of-Concept implementations.
struct PrivateESFT : private std::enable_shared_from_this<PrivateESFT> {}; struct PrivateESFT : private std::enable_shared_from_this<PrivateESFT> {};
static_assert(!pybindit::memory::type_has_shared_from_this(static_cast<PrivateESFT *>(nullptr)), static_assert(!type_has_shared_from_this(static_cast<PrivateESFT *>(nullptr)),
"should detect inaccessible base"); "should detect inaccessible base");
template <typename T> template <typename T>
@@ -50,6 +50,6 @@ std::unique_ptr<T, D> as_unique_ptr(smart_holder &hld) {
return std::unique_ptr<T, D>(raw_ptr); return std::unique_ptr<T, D>(raw_ptr);
} }
} // namespace smart_holder_poc PYBIND11_NAMESPACE_END(smart_holder_poc)
} // namespace memory PYBIND11_NAMESPACE_END(memory)
} // namespace pybindit PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

View File

@@ -13,8 +13,8 @@
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_MAIN
#include "catch.hpp" #include "catch.hpp"
using pybindit::memory::smart_holder; using pybind11::memory::smart_holder;
namespace poc = pybindit::memory::smart_holder_poc; namespace poc = pybind11::memory::smart_holder_poc;
namespace helpers { namespace helpers {