Skip to content

Commit 3f04188

Browse files
committed
Account for np.float128, np.complex256 not being available on Windows, in a future-proof way.
1 parent 1593ebc commit 3f04188

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

tests/test_buffers.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
np = pytest.importorskip("numpy")
1212

13+
if env.WIN:
14+
# Windows does not have these (see e.g. #1908). But who knows, maybe later?
15+
np_float128_or_none = getattr(np, "float128", None)
16+
np_complex256_or_none = getattr(np, "complex256", None)
17+
else:
18+
np_float128_or_none = np.float128
19+
np_complex256_or_none = np.complex256
20+
1321

1422
@pytest.mark.parametrize(
1523
("cpp_name", "expected_fmts", "np_array_dtype"),
@@ -26,10 +34,10 @@
2634
("std::uint64_t", ["Q"], np.uint64),
2735
("float", ["f"], np.float32),
2836
("double", ["d"], np.float64),
29-
("long double", ["g", "d"], np.float128),
37+
("long double", ["g", "d"], np_float128_or_none),
3038
("std::complex<float>", ["Zf"], np.complex64),
3139
("std::complex<double>", ["Zd"], np.complex128),
32-
("std::complex<long double>", ["Zg", "Zd"], np.complex256),
40+
("std::complex<long double>", ["Zg", "Zd"], np_complex256_or_none),
3341
],
3442
)
3543
def test_format_descriptor_format(cpp_name, expected_fmts, np_array_dtype):
@@ -39,15 +47,16 @@ def test_format_descriptor_format(cpp_name, expected_fmts, np_array_dtype):
3947
# Everything below just documents long-standing inconsistencies.
4048
# See also: https://github.com/pybind/pybind11/issues/1908
4149

42-
# py::format_descriptor<> vs np.array:
43-
na = np.array([], dtype=np_array_dtype)
44-
bi = m.get_buffer_info(na)
45-
if fmt == "q":
46-
assert bi.format in ["q", "l"]
47-
elif fmt == "Q":
48-
assert bi.format in ["Q", "L"]
49-
else:
50-
assert bi.format == fmt
50+
if np_array_dtype is not None:
51+
# py::format_descriptor<> vs np.array:
52+
na = np.array([], dtype=np_array_dtype)
53+
bi = m.get_buffer_info(na)
54+
if fmt == "q":
55+
assert bi.format in ["q", "l"]
56+
elif fmt == "Q":
57+
assert bi.format in ["Q", "L"]
58+
else:
59+
assert bi.format == fmt
5160

5261
# py::format_descriptor<> vs np.format_parser():
5362
fmtp = fmt[1:] if fmt.startswith("Z") else fmt

0 commit comments

Comments
 (0)