5np = pytest.importorskip(
"numpy")
6eigen_tensor = pytest.importorskip(
"pybind11_tests.eigen_tensor")
7submodules = [eigen_tensor.c_style, eigen_tensor.f_style]
9 import eigen_tensor_avoid_stl_array
as avoid
11 submodules += [avoid.c_style, avoid.f_style]
12except ImportError
as e:
15 "import eigen_tensor_avoid_stl_array FAILED, while "
16 "import pybind11_tests.eigen_tensor succeeded. "
18 "test_eigen_tensor.cpp & "
19 "eigen_tensor_avoid_stl_array.cpp "
20 "are built together (or both are not built if Eigen is not available)."
23tensor_ref = np.empty((3, 5, 2), dtype=np.int64)
25for i
in range(tensor_ref.shape[0]):
26 for j
in range(tensor_ref.shape[1]):
27 for k
in range(tensor_ref.shape[2]):
28 tensor_ref[i, j, k] = i * (5 * 2) + j * 2 + k
33@pytest.fixture(autouse=True)
35 for module
in submodules:
40 for module
in submodules:
45 pytest.importorskip(
"eigen_tensor_avoid_stl_array")
46 assert len(submodules) == 4
50 assert mat.flags.writeable == writeable
52 copy = np.array(tensor_ref)
53 if modified
is not None:
54 copy[indices] = modified
56 np.testing.assert_array_equal(mat, copy)
59@pytest.mark.parametrize("m", submodules)
60@pytest.mark.parametrize("member_name", ["member", "member_view"])
62 if not hasattr(sys,
"getrefcount"):
63 pytest.skip(
"No reference counting")
64 foo = m.CustomExample()
65 counts = sys.getrefcount(foo)
68 new_counts = sys.getrefcount(foo)
69 assert new_counts == counts + 1
72 assert sys.getrefcount(foo) == counts
80 "move_fixed_tensor_copy",
84 "reference_tensor_v2",
85 "reference_fixed_tensor",
86 "reference_view_of_tensor",
87 "reference_view_of_tensor_v3",
88 "reference_view_of_tensor_v5",
89 "reference_view_of_fixed_tensor",
92assert_equal_const_funcs = [
93 "reference_view_of_tensor_v2",
94 "reference_view_of_tensor_v4",
95 "reference_view_of_tensor_v6",
96 "reference_const_tensor",
97 "reference_const_tensor_v2",
101@pytest.mark.parametrize("m", submodules)
102@pytest.mark.parametrize("func_name", assert_equal_funcs + assert_equal_const_funcs)
104 writeable = func_name
in assert_equal_funcs
108@pytest.mark.parametrize("m", submodules)
111 RuntimeError, match=
"Cannot use reference internal when there is no parent"
113 m.reference_tensor_internal()
115 with pytest.raises(RuntimeError, match=
"Cannot move from a constant reference"):
116 m.move_const_tensor()
119 RuntimeError, match=
"Cannot take ownership of a const reference"
121 m.take_const_tensor()
125 match=
"Invalid return_value_policy for Eigen Map type, must be either reference or reference_internal",
130@pytest.mark.parametrize("m", submodules)
133 TypeError, match=
r"^round_trip_tensor\(\): incompatible function arguments"
135 m.round_trip_tensor(np.zeros((2, 3)))
137 with pytest.raises(TypeError, match=
r"^Cannot cast array data from dtype"):
138 m.round_trip_tensor(np.zeros(dtype=np.str_, shape=(2, 3, 1)))
142 match=
r"^round_trip_tensor_noconvert\(\): incompatible function arguments",
144 m.round_trip_tensor_noconvert(tensor_ref)
147 m.round_trip_tensor_noconvert(tensor_ref.astype(np.float64))
150 if m.needed_options ==
"F":
156 TypeError, match=
r"^round_trip_view_tensor\(\): incompatible function arguments"
158 m.round_trip_view_tensor(
159 np.zeros((3, 5, 2), dtype=np.float64, order=bad_options)
163 TypeError, match=
r"^round_trip_view_tensor\(\): incompatible function arguments"
165 m.round_trip_view_tensor(
166 np.zeros((3, 5, 2), dtype=np.float32, order=m.needed_options)
170 TypeError, match=
r"^round_trip_view_tensor\(\): incompatible function arguments"
172 m.round_trip_view_tensor(
173 np.zeros((3, 5), dtype=np.float64, order=m.needed_options)
177 TypeError, match=
r"^round_trip_view_tensor\(\): incompatible function arguments"
179 temp = np.zeros((3, 5, 2), dtype=np.float64, order=m.needed_options)
180 m.round_trip_view_tensor(
185 TypeError, match=
r"^round_trip_view_tensor\(\): incompatible function arguments"
187 temp = np.zeros((3, 5, 2), dtype=np.float64, order=m.needed_options)
188 temp.setflags(write=
False)
189 m.round_trip_view_tensor(temp)
192@pytest.mark.parametrize("m", submodules)
194 a = m.reference_tensor()
201 a = m.reference_view_of_tensor()
208@pytest.mark.parametrize("m", submodules)
212 with pytest.raises(TypeError, match=
"^Cannot cast array data from"):
219 copy = np.array(tensor_ref, dtype=np.float64, order=m.needed_options)
223 copy.setflags(write=
False)
226 np.testing.assert_array_equal(
227 tensor_ref[:, ::-1, :], m.round_trip_tensor(tensor_ref[:, ::-1, :])
230 assert m.round_trip_rank_0(np.float64(3.5)) == 3.5
231 assert m.round_trip_rank_0(3.5) == 3.5
235 match=
r"^round_trip_rank_0_noconvert\(\): incompatible function arguments",
237 m.round_trip_rank_0_noconvert(np.float64(3.5))
241 match=
r"^round_trip_rank_0_noconvert\(\): incompatible function arguments",
243 m.round_trip_rank_0_noconvert(3.5)
246 TypeError, match=
r"^round_trip_rank_0_view\(\): incompatible function arguments"
248 m.round_trip_rank_0_view(np.float64(3.5))
251 TypeError, match=
r"^round_trip_rank_0_view\(\): incompatible function arguments"
253 m.round_trip_rank_0_view(3.5)
256@pytest.mark.parametrize("m", submodules)
259 copy = np.array(tensor_ref, dtype=np.float64, order=m.needed_options)
260 a = m.round_trip_view_tensor(copy)
268@pytest.mark.parametrize("m", submodules)
271 doc(m.copy_tensor) ==
"copy_tensor() -> numpy.ndarray[numpy.float64[?, ?, ?]]"
274 doc(m.copy_fixed_tensor)
275 ==
"copy_fixed_tensor() -> numpy.ndarray[numpy.float64[3, 5, 2]]"
278 doc(m.reference_const_tensor)
279 ==
"reference_const_tensor() -> numpy.ndarray[numpy.float64[?, ?, ?]]"
282 order_flag = f
"flags.{m.needed_options.lower()}_contiguous"
283 assert doc(m.round_trip_view_tensor) == (
284 f
"round_trip_view_tensor(arg0: numpy.ndarray[numpy.float64[?, ?, ?], flags.writeable, {order_flag}])"
285 + f
" -> numpy.ndarray[numpy.float64[?, ?, ?], flags.writeable, {order_flag}]"
287 assert doc(m.round_trip_const_view_tensor) == (
288 f
"round_trip_const_view_tensor(arg0: numpy.ndarray[numpy.float64[?, ?, ?], {order_flag}])"
289 +
" -> numpy.ndarray[numpy.float64[?, ?, ?]]"
object getattr(handle obj, handle name)
bool hasattr(handle obj, handle name)
size_t len(handle h)
Get the length of a Python object.
def test_import_avoid_stl_array()
def test_references_actually_refer(m)
def test_doc_string(m, doc)
def test_round_trip_references_actually_refer(m)
def test_reference_internal(m, member_name)
def assert_equal_tensor_ref(mat, writeable=True, modified=None)
def test_convert_tensor_to_py(m, func_name)
def test_bad_cpp_to_python_casts(m)
def test_bad_python_to_cpp_casts(m)
Annotation for documentation.