Skip to content

Commit

Permalink
Define PYBIND11_TYPING_H_HAS_STRING_LITERAL to avoid repeating a comp…
Browse files Browse the repository at this point in the history
…lex expression.
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Jun 28, 2024
1 parent 1008960 commit d530ce8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
11 changes: 4 additions & 7 deletions include/pybind11/typing.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,10 @@ class Never : public none {
using none::none;
};

// Define guard around specific GCC version
// https://github.com/pybind/pybind11/issues/5201
#if defined(__cpp_nontype_template_parameter_class) \
&& (!defined(__GNUC__) \
|| defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)))
&& (/* See #5201 */ !defined(__GNUC__) \
|| (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)))
# define PYBIND11_TYPING_H_HAS_STRING_LITERAL
template <size_t N>
struct StringLiteral {
constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); }
Expand Down Expand Up @@ -226,9 +225,7 @@ struct handle_type_name<typing::Never> {
static constexpr auto name = const_name("Never");
};

#if defined(__cpp_nontype_template_parameter_class) \
&& (!defined(__GNUC__) \
|| defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)))
#if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
template <typing::StringLiteral... Literals>
struct handle_type_name<typing::Literal<Literals...>> {
static constexpr auto name = const_name("Literal[")
Expand Down
12 changes: 4 additions & 8 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ void m_defs(py::module_ &m) {

} // namespace handle_from_move_only_type_with_operator_PyObject

#if defined(__cpp_nontype_template_parameter_class) \
&& (!defined(__GNUC__) \
|| defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)))
#if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
namespace literals {
enum Color { RED = 0, BLUE = 1 };

Expand Down Expand Up @@ -907,9 +905,7 @@ TEST_SUBMODULE(pytypes, m) {
m.def("annotate_optional_to_object",
[](py::typing::Optional<int> &o) -> py::object { return o; });

#if defined(__cpp_nontype_template_parameter_class) \
&& (!defined(__GNUC__) \
|| defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)))
#if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
py::enum_<literals::Color>(m, "Color")
.value("RED", literals::Color::RED)
.value("BLUE", literals::Color::BLUE);
Expand All @@ -923,8 +919,8 @@ TEST_SUBMODULE(pytypes, m) {
m.def("annotate_listT_to_T",
[](const py::typing::List<typevar::TypeVarT> &l) -> typevar::TypeVarT { return l[0]; });
m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; });
m.attr("if_defined__cpp_nontype_template_parameter_class") = true;
m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = true;
#else
m.attr("if_defined__cpp_nontype_template_parameter_class") = false;
m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = false;
#endif
}
4 changes: 2 additions & 2 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ def test_optional_object_annotations(doc):


@pytest.mark.skipif(
not m.if_defined__cpp_nontype_template_parameter_class,
not m.defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL,
reason="C++20 feature not available.",
)
def test_literal(doc):
Expand All @@ -1036,7 +1036,7 @@ def test_literal(doc):


@pytest.mark.skipif(
not m.if_defined__cpp_nontype_template_parameter_class,
not m.defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL,
reason="C++20 feature not available.",
)
def test_typevar(doc):
Expand Down

0 comments on commit d530ce8

Please sign in to comment.