Skip to content

Commit 85bc088

Browse files
authored
Report C++ Info: via pytest_report_header() (#4046)
* Report `C++ Info:` from `pytest_configure()` * Use pytest_report_header() as suggested by @Skylion007
1 parent cd08869 commit 85bc088

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

include/pybind11/detail/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
# define PYBIND11_CPP17
3939
# if __cplusplus >= 202002L
4040
# define PYBIND11_CPP20
41+
// Please update tests/pybind11_tests.cpp `cpp_std()` when adding a macro here.
4142
# endif
4243
# endif
4344
# endif

tests/conftest.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import pytest
1414

1515
# Early diagnostic for failed imports
16-
import pybind11_tests # noqa: F401
16+
import pybind11_tests
1717

1818
_long_marker = re.compile(r"([0-9])L")
1919
_hexadecimal = re.compile(r"0x[0-9a-fA-F]+")
@@ -198,3 +198,16 @@ def gc_collect():
198198
def pytest_configure():
199199
pytest.suppress = suppress
200200
pytest.gc_collect = gc_collect
201+
202+
203+
def pytest_report_header(config):
204+
del config # Unused.
205+
assert (
206+
pybind11_tests.compiler_info is not None
207+
), "Please update pybind11_tests.cpp if this assert fails."
208+
return (
209+
"C++ Info:"
210+
f" {pybind11_tests.compiler_info}"
211+
f" {pybind11_tests.cpp_std}"
212+
f" {pybind11_tests.PYBIND11_INTERNALS_ID}"
213+
)

tests/pybind11_tests.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,34 @@ void bind_ConstructorStats(py::module_ &m) {
6262
});
6363
}
6464

65+
const char *cpp_std() {
66+
return
67+
#if defined(PYBIND11_CPP20)
68+
"C++20";
69+
#elif defined(PYBIND11_CPP17)
70+
"C++17";
71+
#elif defined(PYBIND11_CPP14)
72+
"C++14";
73+
#else
74+
"C++11";
75+
#endif
76+
}
77+
6578
PYBIND11_MODULE(pybind11_tests, m) {
6679
m.doc() = "pybind11 test module";
6780

81+
// Intentionally kept minimal to not create a maintenance chore
82+
// ("just enough" to be conclusive).
83+
#if defined(_MSC_FULL_VER)
84+
m.attr("compiler_info") = "MSVC " PYBIND11_TOSTRING(_MSC_FULL_VER);
85+
#elif defined(__VERSION__)
86+
m.attr("compiler_info") = __VERSION__;
87+
#else
88+
m.attr("compiler_info") = py::none();
89+
#endif
90+
m.attr("cpp_std") = cpp_std();
91+
m.attr("PYBIND11_INTERNALS_ID") = PYBIND11_INTERNALS_ID;
92+
6893
bind_ConstructorStats(m);
6994

7095
#if defined(PYBIND11_DETAILED_ERROR_MESSAGES)

0 commit comments

Comments
 (0)