Skip to content

Commit ee4b9f5

Browse files
Fix ODR violations in our Eigen Tensor tests (#4412)
* First * Fix centos 7 again :( * Fix minor nits
1 parent a97c4d2 commit ee4b9f5

File tree

5 files changed

+19
-26
lines changed

5 files changed

+19
-26
lines changed

tests/CMakeLists.txt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ set(PYBIND11_TEST_FILES
130130
test_docstring_options
131131
test_eigen_matrix
132132
test_eigen_tensor
133-
test_eigen_tensor_avoid_stl_array.cpp
134133
test_enum
135134
test_eval
136135
test_exceptions
@@ -293,6 +292,11 @@ if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
293292
set(EIGEN3_VERSION ${EIGEN3_VERSION_STRING})
294293
endif()
295294
message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
295+
296+
if(NOT (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0))
297+
tests_extra_targets("test_eigen_tensor.py" "eigen_tensor_avoid_stl_array")
298+
endif()
299+
296300
else()
297301
list(FIND PYBIND11_TEST_FILES test_eigen_matrix.cpp PYBIND11_TEST_FILES_EIGEN_I)
298302
if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
@@ -303,11 +307,6 @@ if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
303307
if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
304308
list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
305309
endif()
306-
list(FIND PYBIND11_TEST_FILES test_eigen_tensor_avoid_stl_array.cpp
307-
PYBIND11_TEST_FILES_EIGEN_I)
308-
if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
309-
list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
310-
endif()
311310
message(
312311
STATUS "Building tests WITHOUT Eigen, use -DDOWNLOAD_EIGEN=ON on CMake 3.11+ to download")
313312
endif()
@@ -319,10 +318,6 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_L
319318
if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
320319
list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
321320
endif()
322-
list(FIND PYBIND11_TEST_FILES test_eigen_tensor_avoid_stl_array.cpp PYBIND11_TEST_FILES_EIGEN_I)
323-
if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
324-
list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
325-
endif()
326321
endif()
327322

328323
# Optional dependency for some tests (boost::variant is only supported with version >= 1.56)

tests/test_eigen_tensor_avoid_stl_array.cpp renamed to tests/eigen_tensor_avoid_stl_array.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
BSD-style license that can be found in the LICENSE file.
66
*/
77

8-
constexpr const char *test_eigen_tensor_module_name = "eigen_tensor_avoid_stl_array";
9-
108
#ifndef EIGEN_AVOID_STL_ARRAY
119
# define EIGEN_AVOID_STL_ARRAY
1210
#endif
1311

14-
#define PYBIND11_TEST_EIGEN_TENSOR_NAMESPACE eigen_tensor_avoid_stl_array
15-
1612
#include "test_eigen_tensor.inl"
13+
14+
PYBIND11_MODULE(eigen_tensor_avoid_stl_array, m) { eigen_tensor_test::test_module(m); }

tests/test_eigen_tensor.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
BSD-style license that can be found in the LICENSE file.
66
*/
77

8-
constexpr const char *test_eigen_tensor_module_name = "eigen_tensor";
9-
108
#define PYBIND11_TEST_EIGEN_TENSOR_NAMESPACE eigen_tensor
119

1210
#ifdef EIGEN_AVOID_STL_ARRAY
1311
# undef EIGEN_AVOID_STL_ARRAY
1412
#endif
1513

1614
#include "test_eigen_tensor.inl"
15+
16+
#include "pybind11_tests.h"
17+
18+
test_initializer egien_tensor("eigen_tensor", eigen_tensor_test::test_module);

tests/test_eigen_tensor.inl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
#include <pybind11/eigen/tensor.h>
99

10-
#include "pybind11_tests.h"
10+
PYBIND11_NAMESPACE_BEGIN(eigen_tensor_test)
1111

12-
PYBIND11_NAMESPACE_BEGIN(PYBIND11_TEST_EIGEN_TENSOR_NAMESPACE)
12+
namespace py = pybind11;
1313

1414
PYBIND11_WARNING_DISABLE_MSVC(4127)
1515

@@ -108,7 +108,7 @@ void init_tensor_module(pybind11::module &m) {
108108
return check_tensor(get_tensor<Options>()) && check_tensor(get_fixed_tensor<Options>());
109109
});
110110

111-
py::class_<CustomExample<Options>>(m, "CustomExample")
111+
py::class_<CustomExample<Options>>(m, "CustomExample", py::module_local())
112112
.def(py::init<>())
113113
.def_readonly(
114114
"member", &CustomExample<Options>::member, py::return_value_policy::reference_internal)
@@ -322,8 +322,6 @@ void init_tensor_module(pybind11::module &m) {
322322
py::return_value_policy::reference);
323323
}
324324

325-
void test_module(py::module_ &);
326-
test_initializer name(test_eigen_tensor_module_name, test_module);
327325
void test_module(py::module_ &m) {
328326
auto f_style = m.def_submodule("f_style");
329327
auto c_style = m.def_submodule("c_style");
@@ -332,4 +330,4 @@ void test_module(py::module_ &m) {
332330
init_tensor_module<Eigen::RowMajor>(c_style);
333331
}
334332

335-
PYBIND11_NAMESPACE_END(PYBIND11_TEST_EIGEN_TENSOR_NAMESPACE)
333+
PYBIND11_NAMESPACE_END(eigen_tensor_test)

tests/test_eigen_tensor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
eigen_tensor = pytest.importorskip("pybind11_tests.eigen_tensor")
77
submodules = [eigen_tensor.c_style, eigen_tensor.f_style]
88
try:
9-
from pybind11_tests import eigen_tensor_avoid_stl_array as avoid
9+
import eigen_tensor_avoid_stl_array as avoid
1010

1111
submodules += [avoid.c_style, avoid.f_style]
1212
except ImportError as e:
1313
# Ensure config, build, toolchain, etc. issues are not masked here:
1414
raise RuntimeError(
15-
"import pybind11_tests.eigen_tensor_avoid_stl_array FAILED, while "
15+
"import eigen_tensor_avoid_stl_array FAILED, while "
1616
"import pybind11_tests.eigen_tensor succeeded. "
1717
"Please ensure that "
1818
"test_eigen_tensor.cpp & "
19-
"test_eigen_tensor_avoid_stl_array.cpp "
19+
"eigen_tensor_avoid_stl_array.cpp "
2020
"are built together (or both are not built if Eigen is not available)."
2121
) from e
2222

@@ -42,7 +42,7 @@ def cleanup():
4242

4343

4444
def test_import_avoid_stl_array():
45-
pytest.importorskip("pybind11_tests.eigen_tensor_avoid_stl_array")
45+
pytest.importorskip("eigen_tensor_avoid_stl_array")
4646
assert len(submodules) == 4
4747

4848

0 commit comments

Comments
 (0)