Skip to content

chore: collect all #define PYBIND11_HAS_... macros in pybind11/detail/common.h #5647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions include/pybind11/detail/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@
# endif
#endif

// These PYBIND11_HAS_... macros are consolidated in pybind11/detail/common.h
// to simplify backward compatibility handling for users (e.g., via #ifdef checks):
#define PYBIND11_HAS_TYPE_CASTER_STD_FUNCTION_SPECIALIZATIONS 1
#define PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT 1
#define PYBIND11_HAS_CPP_CONDUIT 1
#define PYBIND11_HAS_NATIVE_ENUM 1

#if defined(PYBIND11_CPP17) && defined(__has_include)
# if __has_include(<filesystem>)
# define PYBIND11_HAS_FILESYSTEM 1
# elif __has_include(<experimental/filesystem>)
# define PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM 1
# endif
#endif

#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914))
# define PYBIND11_STD_LAUNDER std::launder
# define PYBIND11_HAS_STD_LAUNDER 1
Expand Down Expand Up @@ -168,14 +183,9 @@
# define PYBIND11_HAS_VARIANT 1
#endif

#if defined(PYBIND11_CPP17)
# if defined(__has_include)
# if __has_include(<string_view>)
# define PYBIND11_HAS_STRING_VIEW
# endif
# elif defined(_MSC_VER)
# define PYBIND11_HAS_STRING_VIEW
# endif
#if defined(PYBIND11_CPP17) \
&& ((defined(__has_include) && __has_include(<string_view>)) || defined(_MSC_VER))
# define PYBIND11_HAS_STRING_VIEW 1
#endif

#if (defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
Expand Down Expand Up @@ -213,7 +223,7 @@

// Must be after including <version> or one of the other headers specified by the standard
#if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
# define PYBIND11_HAS_U8STRING
# define PYBIND11_HAS_U8STRING 1
#endif

// See description of PR #4246:
Expand Down
2 changes: 0 additions & 2 deletions include/pybind11/detail/cpp_conduit.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,5 @@ inline void *try_raw_pointer_ephemeral_from_cpp_conduit(handle src,
return nullptr;
}

#define PYBIND11_HAS_CPP_CONDUIT 1

PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
3 changes: 0 additions & 3 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,6 @@ struct internals {
}
};

// For backwards compatibility (i.e. #ifdef guards):
#define PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT

enum class holder_enum_t : uint8_t {
undefined,
std_unique_ptr, // Default, lacking interop with std::shared_ptr.
Expand Down
2 changes: 0 additions & 2 deletions include/pybind11/detail/native_enum_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#pragma once

#define PYBIND11_HAS_NATIVE_ENUM

#include "../pytypes.h"
#include "common.h"
#include "internals.h"
Expand Down
2 changes: 0 additions & 2 deletions include/pybind11/functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#pragma once

#define PYBIND11_HAS_TYPE_CASTER_STD_FUNCTION_SPECIALIZATIONS

#include "pybind11.h"

#include <functional>
Expand Down
22 changes: 6 additions & 16 deletions include/pybind11/stl/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,12 @@

#include <string>

#ifdef __has_include
# if defined(PYBIND11_CPP17)
# if __has_include(<filesystem>)
# include <filesystem>
# define PYBIND11_HAS_FILESYSTEM 1
# elif __has_include(<experimental/filesystem>)
# include <experimental/filesystem>
# define PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM 1
# endif
# endif
#endif

#if !defined(PYBIND11_HAS_FILESYSTEM) && !defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM) \
&& !defined(PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL)
# error \
"Neither #include <filesystem> nor #include <experimental/filesystem is available. (Use -DPYBIND11_HAS_FILESYSTEM_IS_OPTIONAL to ignore.)"
#if defined(PYBIND11_HAS_FILESYSTEM)
Copy link
Preview

Copilot AI May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removal of the PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL branch changes the behavior for environments where neither nor <experimental/filesystem> is available. Consider updating the documentation or migration notes to inform users of this breaking change.

Copilot uses AI. Check for mistakes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reasoned this out with the help of Chat:

https://chatgpt.com/share/681da436-a5fc-8008-8d13-402e8bcbaa8e

✅ Conclusion
You're preserving — and even clarifying — expected behavior by:

Removing a test-only macro,

Consolidating detection macros in a single header,

Ensuring that #include <pybind11/stl/filesystem.h> fails clearly if the compiler lacks support.

So: you’re not overlooking anything, and the PR is correct as is.

# include <filesystem>
#elif defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
# include <experimental/filesystem>
#else
# error "Neither #include <filesystem> nor #include <experimental/filesystem is available."
#endif

PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#include "constructor_stats.h"
#include "pybind11_tests.h"

#ifndef PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL
# define PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL
#if defined(PYBIND11_HAS_FILESYSTEM) || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
# include <pybind11/stl/filesystem.h>
#endif
#include <pybind11/stl/filesystem.h>

#include <pybind11/typing.h>

#include <string>
Expand Down
Loading