From a4b923505965464c53e81391a3bd6c6e08e80b07 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 27 Jun 2024 18:10:56 -0400 Subject: [PATCH 01/11] Update CI --- .github/workflows/ci.yml | 2 ++ include/pybind11/typing.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3054d842a1..041e0dfb3f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -501,7 +501,9 @@ jobs: - { gcc: 7, std: 17 } - { gcc: 8, std: 14 } - { gcc: 8, std: 17 } + - { gcc: 9, std: 20 } - { gcc: 10, std: 17 } + - { gcc: 10, std: 20 } - { gcc: 11, std: 20 } - { gcc: 12, std: 20 } - { gcc: 13, std: 20 } diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 1442cdc7f1..50e1de6123 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -98,6 +98,8 @@ 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) template struct StringLiteral { From 0efe146dfc8f4f80d0fc8c46a47c94e84ddee33d Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 27 Jun 2024 18:17:57 -0400 Subject: [PATCH 02/11] update define guard --- include/pybind11/typing.h | 6 ++++-- tests/test_pytypes.cpp | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 50e1de6123..ce69e7c75b 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -100,7 +100,8 @@ class Never : public none { // Define guard around specific GCC version // https://github.com/pybind/pybind11/issues/5201 -#if defined(__cpp_nontype_template_parameter_class) +#if defined(__cpp_nontype_template_parameter_class) || define(__GNUG__) && (__GNUC__ >= 10 && __GNUC_MINOR__ < 2) +__gcc_major template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } @@ -224,7 +225,8 @@ struct handle_type_name { static constexpr auto name = const_name("Never"); }; -#if defined(__cpp_nontype_template_parameter_class) + +#if defined(__cpp_nontype_template_parameter_class) || define(__GNUG__) && (__GNUC__ >= 10 && __GNUC_MINOR__ < 2) template struct handle_type_name> { static constexpr auto name = const_name("Literal[") diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 7c30978cea..d36f2bb9d0 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -905,7 +905,8 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_optional_to_object", [](py::typing::Optional &o) -> py::object { return o; }); -#if defined(__cpp_nontype_template_parameter_class) + +#if defined(__cpp_nontype_template_parameter_class) || define(__GNUG__) && (__GNUC__ >= 10 && __GNUC_MINOR__ < 2) py::enum_(m, "Color") .value("RED", literals::Color::RED) .value("BLUE", literals::Color::BLUE); From 3fdba88a743fb78476b23e57f7a477fc8e99e9a9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 27 Jun 2024 22:18:19 +0000 Subject: [PATCH 03/11] style: pre-commit fixes --- include/pybind11/typing.h | 10 +++++----- tests/test_pytypes.cpp | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index ce69e7c75b..374c85f8b0 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -100,9 +100,9 @@ class Never : public none { // Define guard around specific GCC version // https://github.com/pybind/pybind11/issues/5201 -#if defined(__cpp_nontype_template_parameter_class) || define(__GNUG__) && (__GNUC__ >= 10 && __GNUC_MINOR__ < 2) -__gcc_major -template +#if defined(__cpp_nontype_template_parameter_class) \ + || define(__GNUG__) && (__GNUC__ >= 10 && __GNUC_MINOR__ < 2) +__gcc_major template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } char name[N]; @@ -225,8 +225,8 @@ struct handle_type_name { static constexpr auto name = const_name("Never"); }; - -#if defined(__cpp_nontype_template_parameter_class) || define(__GNUG__) && (__GNUC__ >= 10 && __GNUC_MINOR__ < 2) +#if defined(__cpp_nontype_template_parameter_class) \ + || define(__GNUG__) && (__GNUC__ >= 10 && __GNUC_MINOR__ < 2) template struct handle_type_name> { static constexpr auto name = const_name("Literal[") diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index d36f2bb9d0..3b3c8c47bb 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -905,8 +905,8 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_optional_to_object", [](py::typing::Optional &o) -> py::object { return o; }); - -#if defined(__cpp_nontype_template_parameter_class) || define(__GNUG__) && (__GNUC__ >= 10 && __GNUC_MINOR__ < 2) +#if defined(__cpp_nontype_template_parameter_class) \ + || define(__GNUG__) && (__GNUC__ >= 10 && __GNUC_MINOR__ < 2) py::enum_(m, "Color") .value("RED", literals::Color::RED) .value("BLUE", literals::Color::BLUE); From ecda9ed6969a025d15f7c36bf087b9a06531f84d Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 27 Jun 2024 18:26:57 -0400 Subject: [PATCH 04/11] updated define guard --- include/pybind11/typing.h | 9 ++++----- tests/test_pytypes.cpp | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 374c85f8b0..25e1a2d454 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -100,9 +100,8 @@ class Never : public none { // Define guard around specific GCC version // https://github.com/pybind/pybind11/issues/5201 -#if defined(__cpp_nontype_template_parameter_class) \ - || define(__GNUG__) && (__GNUC__ >= 10 && __GNUC_MINOR__ < 2) -__gcc_major template +#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) || defined(__cpp_nontype_template_parameter_class) && (__GNUC__ > 10 || __GNUC__ == 10 && __GNUC_MINOR__ >= 3) +template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } char name[N]; @@ -225,8 +224,8 @@ struct handle_type_name { static constexpr auto name = const_name("Never"); }; -#if defined(__cpp_nontype_template_parameter_class) \ - || define(__GNUG__) && (__GNUC__ >= 10 && __GNUC_MINOR__ < 2) + +#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) || defined(__cpp_nontype_template_parameter_class) && (__GNUC__ > 10 || __GNUC__ == 10 && __GNUC_MINOR__ >= 3) template struct handle_type_name> { static constexpr auto name = const_name("Literal[") diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 3b3c8c47bb..a9d632f67e 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -905,8 +905,8 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_optional_to_object", [](py::typing::Optional &o) -> py::object { return o; }); -#if defined(__cpp_nontype_template_parameter_class) \ - || define(__GNUG__) && (__GNUC__ >= 10 && __GNUC_MINOR__ < 2) + +#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) || defined(__cpp_nontype_template_parameter_class) && (__GNUC__ > 10 || __GNUC__ == 10 && __GNUC_MINOR__ >= 3) py::enum_(m, "Color") .value("RED", literals::Color::RED) .value("BLUE", literals::Color::BLUE); From 70ff7fb79ccec2ecbf2a2c7670fa340269e55996 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 27 Jun 2024 22:27:18 +0000 Subject: [PATCH 05/11] style: pre-commit fixes --- include/pybind11/typing.h | 9 ++++++--- tests/test_pytypes.cpp | 5 +++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 25e1a2d454..4a5327e040 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -100,7 +100,9 @@ class Never : public none { // Define guard around specific GCC version // https://github.com/pybind/pybind11/issues/5201 -#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) || defined(__cpp_nontype_template_parameter_class) && (__GNUC__ > 10 || __GNUC__ == 10 && __GNUC_MINOR__ >= 3) +#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) \ + || defined(__cpp_nontype_template_parameter_class) \ + && (__GNUC__ > 10 || __GNUC__ == 10 && __GNUC_MINOR__ >= 3) template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } @@ -224,8 +226,9 @@ struct handle_type_name { static constexpr auto name = const_name("Never"); }; - -#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) || defined(__cpp_nontype_template_parameter_class) && (__GNUC__ > 10 || __GNUC__ == 10 && __GNUC_MINOR__ >= 3) +#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) \ + || defined(__cpp_nontype_template_parameter_class) \ + && (__GNUC__ > 10 || __GNUC__ == 10 && __GNUC_MINOR__ >= 3) template struct handle_type_name> { static constexpr auto name = const_name("Literal[") diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index a9d632f67e..3b9466641c 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -905,8 +905,9 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_optional_to_object", [](py::typing::Optional &o) -> py::object { return o; }); - -#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) || defined(__cpp_nontype_template_parameter_class) && (__GNUC__ > 10 || __GNUC__ == 10 && __GNUC_MINOR__ >= 3) +#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) \ + || defined(__cpp_nontype_template_parameter_class) \ + && (__GNUC__ > 10 || __GNUC__ == 10 && __GNUC_MINOR__ >= 3) py::enum_(m, "Color") .value("RED", literals::Color::RED) .value("BLUE", literals::Color::BLUE); From 8aa5cf769e3d5a60aba15e585c97e8cae7c095be Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 27 Jun 2024 18:32:39 -0400 Subject: [PATCH 06/11] update guard --- include/pybind11/typing.h | 4 ++-- tests/test_pytypes.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 4a5327e040..97bd5e6add 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -102,7 +102,7 @@ class Never : public none { // https://github.com/pybind/pybind11/issues/5201 #if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) \ || defined(__cpp_nontype_template_parameter_class) \ - && (__GNUC__ > 10 || __GNUC__ == 10 && __GNUC_MINOR__ >= 3) + && (!(__GNUC__ < 10) && __GNUC__ == 10 && __GNUC_MINOR__ >= 3) template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } @@ -228,7 +228,7 @@ struct handle_type_name { #if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) \ || defined(__cpp_nontype_template_parameter_class) \ - && (__GNUC__ > 10 || __GNUC__ == 10 && __GNUC_MINOR__ >= 3) + && (!(__GNUC__ < 10) && __GNUC__ == 10 && __GNUC_MINOR__ >= 3) template struct handle_type_name> { static constexpr auto name = const_name("Literal[") diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 3b9466641c..f97a864ee2 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -907,7 +907,7 @@ TEST_SUBMODULE(pytypes, m) { #if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) \ || defined(__cpp_nontype_template_parameter_class) \ - && (__GNUC__ > 10 || __GNUC__ == 10 && __GNUC_MINOR__ >= 3) + && (!(__GNUC__ < 10) && __GNUC__ == 10 && __GNUC_MINOR__ >= 3) py::enum_(m, "Color") .value("RED", literals::Color::RED) .value("BLUE", literals::Color::BLUE); From 6c4c9e76488b4f11e6f34af3f8cdc6123090e02f Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 27 Jun 2024 18:39:53 -0400 Subject: [PATCH 07/11] testing new guards --- include/pybind11/typing.h | 4 ++-- tests/test_pytypes.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 97bd5e6add..f371b50178 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -102,7 +102,7 @@ class Never : public none { // https://github.com/pybind/pybind11/issues/5201 #if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) \ || defined(__cpp_nontype_template_parameter_class) \ - && (!(__GNUC__ < 10) && __GNUC__ == 10 && __GNUC_MINOR__ >= 3) + && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)) template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } @@ -228,7 +228,7 @@ struct handle_type_name { #if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) \ || defined(__cpp_nontype_template_parameter_class) \ - && (!(__GNUC__ < 10) && __GNUC__ == 10 && __GNUC_MINOR__ >= 3) + && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)) template struct handle_type_name> { static constexpr auto name = const_name("Literal[") diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index f97a864ee2..9c37ced217 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -907,7 +907,7 @@ TEST_SUBMODULE(pytypes, m) { #if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) \ || defined(__cpp_nontype_template_parameter_class) \ - && (!(__GNUC__ < 10) && __GNUC__ == 10 && __GNUC_MINOR__ >= 3) + && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)) py::enum_(m, "Color") .value("RED", literals::Color::RED) .value("BLUE", literals::Color::BLUE); From 4c708a7fd2947dff79a688fa499ad840447f84ec Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 27 Jun 2024 18:46:19 -0400 Subject: [PATCH 08/11] update guards --- include/pybind11/typing.h | 10 ++++------ tests/test_pytypes.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index f371b50178..0a025497af 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -100,9 +100,8 @@ class Never : public none { // Define guard around specific GCC version // https://github.com/pybind/pybind11/issues/5201 -#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) \ - || defined(__cpp_nontype_template_parameter_class) \ - && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)) +#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUC__)) \ + || defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)) template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } @@ -226,9 +225,8 @@ struct handle_type_name { static constexpr auto name = const_name("Never"); }; -#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) \ - || defined(__cpp_nontype_template_parameter_class) \ - && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)) +#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUC__)) \ + || defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)) template struct handle_type_name> { static constexpr auto name = const_name("Literal[") diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 9c37ced217..fad0f05b32 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -109,7 +109,8 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject -#if defined(__cpp_nontype_template_parameter_class) +#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUC__)) \ + || defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)) namespace literals { enum Color { RED = 0, BLUE = 1 }; @@ -905,9 +906,8 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_optional_to_object", [](py::typing::Optional &o) -> py::object { return o; }); -#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUG__)) \ - || defined(__cpp_nontype_template_parameter_class) \ - && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)) +#if (defined(__cpp_nontype_template_parameter_class) && !defined(__GNUC__)) \ + || defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3)) py::enum_(m, "Color") .value("RED", literals::Color::RED) .value("BLUE", literals::Color::BLUE); From 0fe84efab108ef6591428c6633ac0a287149b0f1 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 27 Jun 2024 18:50:19 -0400 Subject: [PATCH 09/11] surely this time --- include/pybind11/typing.h | 6 ++---- tests/test_pytypes.cpp | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 0a025497af..c96fe221fd 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -100,8 +100,7 @@ class Never : public 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)) +#if defined(__cpp_nontype_template_parameter_class) && (!defined(__GNUC__) || defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3))) template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } @@ -225,8 +224,7 @@ struct handle_type_name { 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(__cpp_nontype_template_parameter_class) && (!defined(__GNUC__) || defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3))) template struct handle_type_name> { static constexpr auto name = const_name("Literal[") diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index fad0f05b32..d098d6351c 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -109,8 +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(__cpp_nontype_template_parameter_class) && (!defined(__GNUC__) || defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3))) namespace literals { enum Color { RED = 0, BLUE = 1 }; @@ -906,8 +905,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_optional_to_object", [](py::typing::Optional &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(__cpp_nontype_template_parameter_class) && (!defined(__GNUC__) || defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3))) py::enum_(m, "Color") .value("RED", literals::Color::RED) .value("BLUE", literals::Color::BLUE); From 1008960d9ac55f3df341d0b436cf5c9aac3d6e16 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 27 Jun 2024 22:50:39 +0000 Subject: [PATCH 10/11] style: pre-commit fixes --- include/pybind11/typing.h | 8 ++++++-- tests/test_pytypes.cpp | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index c96fe221fd..972c1c4d9a 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -100,7 +100,9 @@ class Never : public 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))) +#if defined(__cpp_nontype_template_parameter_class) \ + && (!defined(__GNUC__) \ + || defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3))) template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } @@ -224,7 +226,9 @@ struct handle_type_name { 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(__cpp_nontype_template_parameter_class) \ + && (!defined(__GNUC__) \ + || defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3))) template struct handle_type_name> { static constexpr auto name = const_name("Literal[") diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index d098d6351c..d9fcf930cb 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -109,7 +109,9 @@ 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(__cpp_nontype_template_parameter_class) \ + && (!defined(__GNUC__) \ + || defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3))) namespace literals { enum Color { RED = 0, BLUE = 1 }; @@ -905,7 +907,9 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_optional_to_object", [](py::typing::Optional &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(__cpp_nontype_template_parameter_class) \ + && (!defined(__GNUC__) \ + || defined(__GNUC__) && (__GNUC__ > 10 || (__GNUC__ == 10 && __GNUC_MINOR__ >= 3))) py::enum_(m, "Color") .value("RED", literals::Color::RED) .value("BLUE", literals::Color::BLUE); From d530ce8761ec4a47b1f34b1101a2585d084036e5 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 27 Jun 2024 20:53:10 -0700 Subject: [PATCH 11/11] Define PYBIND11_TYPING_H_HAS_STRING_LITERAL to avoid repeating a complex expression. --- include/pybind11/typing.h | 11 ++++------- tests/test_pytypes.cpp | 12 ++++-------- tests/test_pytypes.py | 4 ++-- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 972c1c4d9a..c8ba18d499 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -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 struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } @@ -226,9 +225,7 @@ struct handle_type_name { 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 struct handle_type_name> { static constexpr auto name = const_name("Literal[") diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index d9fcf930cb..ecb44939aa 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -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 }; @@ -907,9 +905,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_optional_to_object", [](py::typing::Optional &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_(m, "Color") .value("RED", literals::Color::RED) .value("BLUE", literals::Color::BLUE); @@ -923,8 +919,8 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_listT_to_T", [](const py::typing::List &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 } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 30931e0b9a..218092b434 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -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): @@ -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):