Test for __cpp_inline_variables and use static_assert()

This commit is contained in:
Ralf W. Grosse-Kunstleve 2024-12-17 12:19:20 -08:00
parent 28d2a66350
commit 3cbaafdb8a
No known key found for this signature in database
4 changed files with 15 additions and 13 deletions

View File

@ -1366,16 +1366,19 @@ object object_or_cast(T &&o) {
return pybind11::cast(std::forward<T>(o)); return pybind11::cast(std::forward<T>(o));
} }
#if defined(PYBIND11_CPP17)
// Declared in pytypes.h: // Declared in pytypes.h:
// Written here so make_caster<T> can be used // Written here so make_caster<T> can be used
template <typename D> template <typename D>
template <typename T> template <typename T>
str_attr_accessor object_api<D>::attr_with_type_hint(const char *key) const { str_attr_accessor object_api<D>::attr_with_type_hint(const char *key) const {
#if !defined(__cpp_inline_variables)
static_assert(false,
"C++17 feature __cpp_inline_variables not available: "
"https://en.cppreference.com/w/cpp/language/static#Static_data_members");
#endif
annotations()[key] = make_caster<T>::name.text; annotations()[key] = make_caster<T>::name.text;
return {derived(), key}; return {derived(), key};
} }
#endif
// Placeholder type for the unneeded (and dead code) static variable in the // Placeholder type for the unneeded (and dead code) static variable in the
// PYBIND11_OVERRIDE_OVERRIDE macro // PYBIND11_OVERRIDE_OVERRIDE macro

View File

@ -113,11 +113,10 @@ public:
/// See above (the only difference is that the key is provided as a string literal) /// See above (the only difference is that the key is provided as a string literal)
str_attr_accessor attr(const char *key) const; str_attr_accessor attr(const char *key) const;
#if defined(PYBIND11_CPP17)
// attr_with_type_hint is implemented in cast.h: // attr_with_type_hint is implemented in cast.h:
template <typename T> template <typename T>
str_attr_accessor attr_with_type_hint(const char *key) const; str_attr_accessor attr_with_type_hint(const char *key) const;
#endif
/** \rst /** \rst
Matches * unpacking in Python, e.g. to unpack arguments out of a ``tuple`` Matches * unpacking in Python, e.g. to unpack arguments out of a ``tuple``
or ``list`` for a function call. Applying another * to the result yields or ``list`` for a function call. Applying another * to the result yields

View File

@ -1038,7 +1038,7 @@ TEST_SUBMODULE(pytypes, m) {
m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = false; m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = false;
#endif #endif
#if defined(PYBIND11_CPP17) #if defined(__cpp_inline_variables)
m.attr_with_type_hint<py::typing::List<int>>("list_int") = py::list(); m.attr_with_type_hint<py::typing::List<int>>("list_int") = py::list();
m.attr_with_type_hint<py::typing::Set<py::str>>("set_str") = py::set(); m.attr_with_type_hint<py::typing::Set<py::str>>("set_str") = py::set();
@ -1059,9 +1059,9 @@ TEST_SUBMODULE(pytypes, m) {
instance.attr_with_type_hint<float>("y"); instance.attr_with_type_hint<float>("y");
m.attr_with_type_hint<py::typing::Final<int>>("CONST_INT") = 3; m.attr_with_type_hint<py::typing::Final<int>>("CONST_INT") = 3;
m.attr("defined_PYBIND11_CPP17") = true; m.attr("defined___cpp_inline_variables") = true;
#else #else
m.attr("defined_PYBIND11_CPP17") = false; m.attr("defined___cpp_inline_variables") = false;
#endif #endif
m.def("half_of_number", [](const RealNumber &x) { return RealNumber{x.value / 2}; }); m.def("half_of_number", [](const RealNumber &x) { return RealNumber{x.value / 2}; });
// std::vector<T> // std::vector<T>

View File

@ -1111,8 +1111,8 @@ def get_annotations_helper(o):
@pytest.mark.skipif( @pytest.mark.skipif(
not m.defined_PYBIND11_CPP17, not m.defined___cpp_inline_variables,
reason="C++17 Position Independent Code not available", reason="C++17 feature __cpp_inline_variables not available.",
) )
def test_module_attribute_types() -> None: def test_module_attribute_types() -> None:
module_annotations = get_annotations_helper(m) module_annotations = get_annotations_helper(m)
@ -1122,8 +1122,8 @@ def test_module_attribute_types() -> None:
@pytest.mark.skipif( @pytest.mark.skipif(
not m.defined_PYBIND11_CPP17, not m.defined___cpp_inline_variables,
reason="C++17 Position Independent Code not available", reason="C++17 feature __cpp_inline_variables not available.",
) )
def test_class_attribute_types() -> None: def test_class_attribute_types() -> None:
empty_annotations = get_annotations_helper(m.EmptyAnnotationClass) empty_annotations = get_annotations_helper(m.EmptyAnnotationClass)
@ -1154,8 +1154,8 @@ def test_class_attribute_types() -> None:
@pytest.mark.skipif( @pytest.mark.skipif(
not m.defined_PYBIND11_CPP17, not m.defined___cpp_inline_variables,
reason="C++17 Position Independent Code not available", reason="C++17 feature __cpp_inline_variables not available.",
) )
def test_final_annotation() -> None: def test_final_annotation() -> None:
module_annotations = get_annotations_helper(m) module_annotations = get_annotations_helper(m)