From 9b6511cd55b74f42ba1c8db4a9cf15a5211550b7 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 6 Jul 2022 15:49:09 -0700 Subject: [PATCH 1/2] Report `C++ Info:` from `pytest_configure()` --- include/pybind11/detail/common.h | 1 + tests/conftest.py | 12 +++++++++++- tests/pybind11_tests.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 9355ecfda0..e52507a9e8 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -38,6 +38,7 @@ # define PYBIND11_CPP17 # if __cplusplus >= 202002L # define PYBIND11_CPP20 +// Please update tests/pybind11_tests.cpp `cpp_std()` when adding a macro here. # endif # endif # endif diff --git a/tests/conftest.py b/tests/conftest.py index e72ec0ef81..d648997345 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,7 +13,7 @@ import pytest # Early diagnostic for failed imports -import pybind11_tests # noqa: F401 +import pybind11_tests _long_marker = re.compile(r"([0-9])L") _hexadecimal = re.compile(r"0x[0-9a-fA-F]+") @@ -196,5 +196,15 @@ def gc_collect(): def pytest_configure(): + print( + "C++ Info:", + pybind11_tests.compiler_info, + pybind11_tests.cpp_std, + pybind11_tests.PYBIND11_INTERNALS_ID, + flush=True, + ) + assert ( + pybind11_tests.compiler_info is not None + ), "Please update pybind11_tests.cpp if this assert fails." pytest.suppress = suppress pytest.gc_collect = gc_collect diff --git a/tests/pybind11_tests.cpp b/tests/pybind11_tests.cpp index 3c04699157..aa3095594b 100644 --- a/tests/pybind11_tests.cpp +++ b/tests/pybind11_tests.cpp @@ -62,9 +62,34 @@ void bind_ConstructorStats(py::module_ &m) { }); } +const char *cpp_std() { + return +#if defined(PYBIND11_CPP20) + "C++20"; +#elif defined(PYBIND11_CPP17) + "C++17"; +#elif defined(PYBIND11_CPP14) + "C++14"; +#else + "C++11"; +#endif +} + PYBIND11_MODULE(pybind11_tests, m) { m.doc() = "pybind11 test module"; + // Intentionally kept minimal to not create a maintenance chore + // ("just enough" to be conclusive). +#if defined(_MSC_FULL_VER) + m.attr("compiler_info") = "MSVC " PYBIND11_TOSTRING(_MSC_FULL_VER); +#elif defined(__VERSION__) + m.attr("compiler_info") = __VERSION__; +#else + m.attr("compiler_info") = py::none(); +#endif + m.attr("cpp_std") = cpp_std(); + m.attr("PYBIND11_INTERNALS_ID") = PYBIND11_INTERNALS_ID; + bind_ConstructorStats(m); #if defined(PYBIND11_DETAILED_ERROR_MESSAGES) From a934f8e5ddbce0d944ddb08329e8edae137b43e0 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 7 Jul 2022 11:11:15 -0700 Subject: [PATCH 2/2] Use pytest_report_header() as suggested by @skylion007 --- tests/conftest.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index d648997345..02ce263afc 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -196,15 +196,18 @@ def gc_collect(): def pytest_configure(): - print( - "C++ Info:", - pybind11_tests.compiler_info, - pybind11_tests.cpp_std, - pybind11_tests.PYBIND11_INTERNALS_ID, - flush=True, - ) + pytest.suppress = suppress + pytest.gc_collect = gc_collect + + +def pytest_report_header(config): + del config # Unused. assert ( pybind11_tests.compiler_info is not None ), "Please update pybind11_tests.cpp if this assert fails." - pytest.suppress = suppress - pytest.gc_collect = gc_collect + return ( + "C++ Info:" + f" {pybind11_tests.compiler_info}" + f" {pybind11_tests.cpp_std}" + f" {pybind11_tests.PYBIND11_INTERNALS_ID}" + )