Skip to content

Commit a28ea8c

Browse files
authored
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.
1 parent 6d1f28f commit a28ea8c

File tree

8 files changed

+103
-95
lines changed

8 files changed

+103
-95
lines changed

include/pybind11/detail/common.h

+1-74
Original file line numberDiff line numberDiff line change
@@ -22,80 +22,7 @@
2222
// Additional convention: 0xD = dev
2323
#define PYBIND11_VERSION_HEX 0x030000D1
2424

25-
// Define some generic pybind11 helper macros for warning management.
26-
//
27-
// Note that compiler-specific push/pop pairs are baked into the
28-
// PYBIND11_NAMESPACE_BEGIN/PYBIND11_NAMESPACE_END pair of macros. Therefore manual
29-
// PYBIND11_WARNING_PUSH/PYBIND11_WARNING_POP are usually only needed in `#include` sections.
30-
//
31-
// If you find you need to suppress a warning, please try to make the suppression as local as
32-
// possible using these macros. Please also be sure to push/pop with the pybind11 macros. Please
33-
// only use compiler specifics if you need to check specific versions, e.g. Apple Clang vs. vanilla
34-
// Clang.
35-
#if defined(_MSC_VER)
36-
# define PYBIND11_COMPILER_MSVC
37-
# define PYBIND11_PRAGMA(...) __pragma(__VA_ARGS__)
38-
# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning(push))
39-
# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning(pop))
40-
#elif defined(__INTEL_COMPILER)
41-
# define PYBIND11_COMPILER_INTEL
42-
# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__)
43-
# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning push)
44-
# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning pop)
45-
#elif defined(__clang__)
46-
# define PYBIND11_COMPILER_CLANG
47-
# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__)
48-
# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(clang diagnostic push)
49-
# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(clang diagnostic pop)
50-
#elif defined(__GNUC__)
51-
# define PYBIND11_COMPILER_GCC
52-
# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__)
53-
# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(GCC diagnostic push)
54-
# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(GCC diagnostic pop)
55-
#endif
56-
57-
#ifdef PYBIND11_COMPILER_MSVC
58-
# define PYBIND11_WARNING_DISABLE_MSVC(name) PYBIND11_PRAGMA(warning(disable : name))
59-
#else
60-
# define PYBIND11_WARNING_DISABLE_MSVC(name)
61-
#endif
62-
63-
#ifdef PYBIND11_COMPILER_CLANG
64-
# define PYBIND11_WARNING_DISABLE_CLANG(name) PYBIND11_PRAGMA(clang diagnostic ignored name)
65-
#else
66-
# define PYBIND11_WARNING_DISABLE_CLANG(name)
67-
#endif
68-
69-
#ifdef PYBIND11_COMPILER_GCC
70-
# define PYBIND11_WARNING_DISABLE_GCC(name) PYBIND11_PRAGMA(GCC diagnostic ignored name)
71-
#else
72-
# define PYBIND11_WARNING_DISABLE_GCC(name)
73-
#endif
74-
75-
#ifdef PYBIND11_COMPILER_INTEL
76-
# define PYBIND11_WARNING_DISABLE_INTEL(name) PYBIND11_PRAGMA(warning disable name)
77-
#else
78-
# define PYBIND11_WARNING_DISABLE_INTEL(name)
79-
#endif
80-
81-
#define PYBIND11_NAMESPACE_BEGIN(name) \
82-
namespace name { \
83-
PYBIND11_WARNING_PUSH
84-
85-
#define PYBIND11_NAMESPACE_END(name) \
86-
PYBIND11_WARNING_POP \
87-
}
88-
89-
// Robust support for some features and loading modules compiled against different pybind versions
90-
// requires forcing hidden visibility on pybind code, so we enforce this by setting the attribute
91-
// on the main `pybind11` namespace.
92-
#if !defined(PYBIND11_NAMESPACE)
93-
# ifdef __GNUG__
94-
# define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden")))
95-
# else
96-
# define PYBIND11_NAMESPACE pybind11
97-
# endif
98-
#endif
25+
#include "pybind11_namespace_macros.h"
9926

10027
#if !(defined(_MSC_VER) && __cplusplus == 199711L)
10128
# if __cplusplus >= 201402L
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) 2016-2025 The Pybind Development Team.
2+
// All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
#pragma once
6+
7+
// PLEASE DO NOT ADD ANY INCLUDES HERE
8+
9+
// Define some generic pybind11 helper macros for warning management.
10+
//
11+
// Note that compiler-specific push/pop pairs are baked into the
12+
// PYBIND11_NAMESPACE_BEGIN/PYBIND11_NAMESPACE_END pair of macros. Therefore manual
13+
// PYBIND11_WARNING_PUSH/PYBIND11_WARNING_POP are usually only needed in `#include` sections.
14+
//
15+
// If you find you need to suppress a warning, please try to make the suppression as local as
16+
// possible using these macros. Please also be sure to push/pop with the pybind11 macros. Please
17+
// only use compiler specifics if you need to check specific versions, e.g. Apple Clang vs. vanilla
18+
// Clang.
19+
#if defined(_MSC_VER)
20+
# define PYBIND11_COMPILER_MSVC
21+
# define PYBIND11_PRAGMA(...) __pragma(__VA_ARGS__)
22+
# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning(push))
23+
# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning(pop))
24+
#elif defined(__INTEL_COMPILER)
25+
# define PYBIND11_COMPILER_INTEL
26+
# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__)
27+
# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning push)
28+
# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning pop)
29+
#elif defined(__clang__)
30+
# define PYBIND11_COMPILER_CLANG
31+
# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__)
32+
# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(clang diagnostic push)
33+
# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(clang diagnostic pop)
34+
#elif defined(__GNUC__)
35+
# define PYBIND11_COMPILER_GCC
36+
# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__)
37+
# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(GCC diagnostic push)
38+
# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(GCC diagnostic pop)
39+
#endif
40+
41+
#ifdef PYBIND11_COMPILER_MSVC
42+
# define PYBIND11_WARNING_DISABLE_MSVC(name) PYBIND11_PRAGMA(warning(disable : name))
43+
#else
44+
# define PYBIND11_WARNING_DISABLE_MSVC(name)
45+
#endif
46+
47+
#ifdef PYBIND11_COMPILER_CLANG
48+
# define PYBIND11_WARNING_DISABLE_CLANG(name) PYBIND11_PRAGMA(clang diagnostic ignored name)
49+
#else
50+
# define PYBIND11_WARNING_DISABLE_CLANG(name)
51+
#endif
52+
53+
#ifdef PYBIND11_COMPILER_GCC
54+
# define PYBIND11_WARNING_DISABLE_GCC(name) PYBIND11_PRAGMA(GCC diagnostic ignored name)
55+
#else
56+
# define PYBIND11_WARNING_DISABLE_GCC(name)
57+
#endif
58+
59+
#ifdef PYBIND11_COMPILER_INTEL
60+
# define PYBIND11_WARNING_DISABLE_INTEL(name) PYBIND11_PRAGMA(warning disable name)
61+
#else
62+
# define PYBIND11_WARNING_DISABLE_INTEL(name)
63+
#endif
64+
65+
#define PYBIND11_NAMESPACE_BEGIN(name) \
66+
namespace name { \
67+
PYBIND11_WARNING_PUSH
68+
69+
#define PYBIND11_NAMESPACE_END(name) \
70+
PYBIND11_WARNING_POP \
71+
}
72+
73+
// Robust support for some features and loading modules compiled against different pybind versions
74+
// requires forcing hidden visibility on pybind code, so we enforce this by setting the attribute
75+
// on the main `pybind11` namespace.
76+
#if !defined(PYBIND11_NAMESPACE)
77+
# ifdef __GNUG__
78+
# define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden")))
79+
# else
80+
# define PYBIND11_NAMESPACE pybind11
81+
# endif
82+
#endif

include/pybind11/detail/struct_smart_holder.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ High-level aspects:
4848

4949
#pragma once
5050

51+
#include "pybind11_namespace_macros.h"
52+
5153
#include <functional>
5254
#include <memory>
5355
#include <stdexcept>
@@ -56,11 +58,8 @@ High-level aspects:
5658
#include <typeinfo>
5759
#include <utility>
5860

59-
// pybindit = Python Bindings Innovation Track.
60-
// Currently not in pybind11 namespace to signal that this POC does not depend
61-
// on any existing pybind11 functionality.
62-
namespace pybindit {
63-
namespace memory {
61+
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
62+
PYBIND11_NAMESPACE_BEGIN(memory)
6463

6564
// Default fallback.
6665
static constexpr bool type_has_shared_from_this(...) { return false; }
@@ -355,5 +354,5 @@ struct smart_holder {
355354
}
356355
};
357356

358-
} // namespace memory
359-
} // namespace pybindit
357+
PYBIND11_NAMESPACE_END(memory)
358+
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

include/pybind11/detail/type_caster_base.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ struct value_and_holder_helper {
543543

544544
// have_holder() must be true or this function will fail.
545545
void throw_if_instance_is_currently_owned_by_shared_ptr() const {
546-
auto *vptr_gd_ptr = std::get_deleter<pybindit::memory::guarded_delete>(holder().vptr);
546+
auto *vptr_gd_ptr = std::get_deleter<memory::guarded_delete>(holder().vptr);
547547
if (vptr_gd_ptr != nullptr && !vptr_gd_ptr->released_ptr.expired()) {
548548
throw value_error("Python instance is currently owned by a std::shared_ptr.");
549549
}
@@ -770,7 +770,7 @@ struct load_helper : value_and_holder_helper {
770770
}
771771
auto *type_raw_ptr = static_cast<T *>(void_raw_ptr);
772772
if (python_instance_is_alias) {
773-
auto *vptr_gd_ptr = std::get_deleter<pybindit::memory::guarded_delete>(hld.vptr);
773+
auto *vptr_gd_ptr = std::get_deleter<memory::guarded_delete>(hld.vptr);
774774
if (vptr_gd_ptr != nullptr) {
775775
std::shared_ptr<void> released_ptr = vptr_gd_ptr->released_ptr.lock();
776776
if (released_ptr) {
@@ -790,8 +790,7 @@ struct load_helper : value_and_holder_helper {
790790
"loaded_v_h.inst == sptsls_ptr->self");
791791
}
792792
}
793-
if (sptsls_ptr != nullptr
794-
|| !pybindit::memory::type_has_shared_from_this(type_raw_ptr)) {
793+
if (sptsls_ptr != nullptr || !memory::type_has_shared_from_this(type_raw_ptr)) {
795794
return std::shared_ptr<T>(
796795
type_raw_ptr, shared_ptr_trampoline_self_life_support(loaded_v_h.inst));
797796
}

include/pybind11/detail/using_smart_holder.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
1313

14-
using pybindit::memory::smart_holder;
14+
using pybind11::memory::smart_holder;
1515

1616
PYBIND11_NAMESPACE_BEGIN(detail)
1717

tests/extra_python_package/test_files.py

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"include/pybind11/detail/init.h",
7070
"include/pybind11/detail/internals.h",
7171
"include/pybind11/detail/native_enum_data.h",
72+
"include/pybind11/detail/pybind11_namespace_macros.h",
7273
"include/pybind11/detail/struct_smart_holder.h",
7374
"include/pybind11/detail/type_caster_base.h",
7475
"include/pybind11/detail/typeid.h",

tests/pure_cpp/smart_holder_poc.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
#include "pybind11/detail/struct_smart_holder.h"
88

9-
namespace pybindit {
10-
namespace memory {
11-
namespace smart_holder_poc { // Proof-of-Concept implementations.
9+
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
10+
PYBIND11_NAMESPACE_BEGIN(memory)
11+
PYBIND11_NAMESPACE_BEGIN(smart_holder_poc) // Proof-of-Concept implementations.
1212

1313
struct PrivateESFT : private std::enable_shared_from_this<PrivateESFT> {};
14-
static_assert(!pybindit::memory::type_has_shared_from_this(static_cast<PrivateESFT *>(nullptr)),
14+
static_assert(!type_has_shared_from_this(static_cast<PrivateESFT *>(nullptr)),
1515
"should detect inaccessible base");
1616

1717
template <typename T>
@@ -50,6 +50,6 @@ std::unique_ptr<T, D> as_unique_ptr(smart_holder &hld) {
5050
return std::unique_ptr<T, D>(raw_ptr);
5151
}
5252

53-
} // namespace smart_holder_poc
54-
} // namespace memory
55-
} // namespace pybindit
53+
PYBIND11_NAMESPACE_END(smart_holder_poc)
54+
PYBIND11_NAMESPACE_END(memory)
55+
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

tests/pure_cpp/smart_holder_poc_test.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#define CATCH_CONFIG_MAIN
1414
#include "catch.hpp"
1515

16-
using pybindit::memory::smart_holder;
17-
namespace poc = pybindit::memory::smart_holder_poc;
16+
using pybind11::memory::smart_holder;
17+
namespace poc = pybind11::memory::smart_holder_poc;
1818

1919
namespace helpers {
2020

0 commit comments

Comments
 (0)