summaryrefslogtreecommitdiff
path: root/ext/pybind11/tests/test_numpy_array.cpp
diff options
context:
space:
mode:
authorBobby R. Bruce <bbruce@ucdavis.edu>2019-09-23 13:52:58 -0700
committerBobby R. Bruce <bbruce@ucdavis.edu>2019-09-24 21:40:15 +0000
commitf97cf54db7a6f7642cc9fd122f23c4396c39bcf0 (patch)
tree17d2ed22a1114cb138500d46afddb3bafcc2b418 /ext/pybind11/tests/test_numpy_array.cpp
parent9235ae56c282d5a02ada3ed9b4e0fe2ee5738bde (diff)
downloadgem5-f97cf54db7a6f7642cc9fd122f23c4396c39bcf0.tar.xz
ext: Updated Pybind11 to version 2.4.1.
This updates Pybind11 from version 2.2.1 to version 2.4.1. This fixes warning/error received when "<experiment/optional>" is used when compiling using c++14 with clang. It should be noted that "ext/pybind11/include/pybind11/std.h" has been changed to include a fix added by commit ba42457254cc362eddc099f22b60d469cc6369e0. This is necessary to avoid build errors. Built: Linux (gcc, c++11) and MacOS (clang, c++14). Tested: Ran quick tests for X86, ARM, and RISC-V. Deprecates: https://gem5-review.googlesource.com/c/public/gem5/+/21019 Change-Id: Ie9783511cb6be50136076a55330e645f4f36d075 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/21119 Reviewed-by: Jason Lowe-Power <jason@lowepower.com> Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com> Maintainer: Jason Lowe-Power <jason@lowepower.com> Maintainer: Andreas Sandberg <andreas.sandberg@arm.com> Tested-by: kokoro <noreply+kokoro@google.com>
Diffstat (limited to 'ext/pybind11/tests/test_numpy_array.cpp')
-rw-r--r--ext/pybind11/tests/test_numpy_array.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/ext/pybind11/tests/test_numpy_array.cpp b/ext/pybind11/tests/test_numpy_array.cpp
index 2046c0e03..156a3bfa8 100644
--- a/ext/pybind11/tests/test_numpy_array.cpp
+++ b/ext/pybind11/tests/test_numpy_array.cpp
@@ -14,6 +14,67 @@
#include <cstdint>
+// Size / dtype checks.
+struct DtypeCheck {
+ py::dtype numpy{};
+ py::dtype pybind11{};
+};
+
+template <typename T>
+DtypeCheck get_dtype_check(const char* name) {
+ py::module np = py::module::import("numpy");
+ DtypeCheck check{};
+ check.numpy = np.attr("dtype")(np.attr(name));
+ check.pybind11 = py::dtype::of<T>();
+ return check;
+}
+
+std::vector<DtypeCheck> get_concrete_dtype_checks() {
+ return {
+ // Normalization
+ get_dtype_check<std::int8_t>("int8"),
+ get_dtype_check<std::uint8_t>("uint8"),
+ get_dtype_check<std::int16_t>("int16"),
+ get_dtype_check<std::uint16_t>("uint16"),
+ get_dtype_check<std::int32_t>("int32"),
+ get_dtype_check<std::uint32_t>("uint32"),
+ get_dtype_check<std::int64_t>("int64"),
+ get_dtype_check<std::uint64_t>("uint64")
+ };
+}
+
+struct DtypeSizeCheck {
+ std::string name{};
+ int size_cpp{};
+ int size_numpy{};
+ // For debugging.
+ py::dtype dtype{};
+};
+
+template <typename T>
+DtypeSizeCheck get_dtype_size_check() {
+ DtypeSizeCheck check{};
+ check.name = py::type_id<T>();
+ check.size_cpp = sizeof(T);
+ check.dtype = py::dtype::of<T>();
+ check.size_numpy = check.dtype.attr("itemsize").template cast<int>();
+ return check;
+}
+
+std::vector<DtypeSizeCheck> get_platform_dtype_size_checks() {
+ return {
+ get_dtype_size_check<short>(),
+ get_dtype_size_check<unsigned short>(),
+ get_dtype_size_check<int>(),
+ get_dtype_size_check<unsigned int>(),
+ get_dtype_size_check<long>(),
+ get_dtype_size_check<unsigned long>(),
+ get_dtype_size_check<long long>(),
+ get_dtype_size_check<unsigned long long>(),
+ };
+}
+
+// Arrays.
using arr = py::array;
using arr_t = py::array_t<uint16_t, 0>;
static_assert(std::is_same<arr_t::value_type, uint16_t>::value, "");
@@ -68,10 +129,33 @@ template <typename T, typename T2> py::handle auxiliaries(T &&r, T2 &&r2) {
return l.release();
}
+// note: declaration at local scope would create a dangling reference!
+static int data_i = 42;
+
TEST_SUBMODULE(numpy_array, sm) {
try { py::module::import("numpy"); }
catch (...) { return; }
+ // test_dtypes
+ py::class_<DtypeCheck>(sm, "DtypeCheck")
+ .def_readonly("numpy", &DtypeCheck::numpy)
+ .def_readonly("pybind11", &DtypeCheck::pybind11)
+ .def("__repr__", [](const DtypeCheck& self) {
+ return py::str("<DtypeCheck numpy={} pybind11={}>").format(
+ self.numpy, self.pybind11);
+ });
+ sm.def("get_concrete_dtype_checks", &get_concrete_dtype_checks);
+
+ py::class_<DtypeSizeCheck>(sm, "DtypeSizeCheck")
+ .def_readonly("name", &DtypeSizeCheck::name)
+ .def_readonly("size_cpp", &DtypeSizeCheck::size_cpp)
+ .def_readonly("size_numpy", &DtypeSizeCheck::size_numpy)
+ .def("__repr__", [](const DtypeSizeCheck& self) {
+ return py::str("<DtypeSizeCheck name='{}' size_cpp={} size_numpy={} dtype={}>").format(
+ self.name, self.size_cpp, self.size_numpy, self.dtype);
+ });
+ sm.def("get_platform_dtype_size_checks", &get_platform_dtype_size_checks);
+
// test_array_attributes
sm.def("ndim", [](const arr& a) { return a.ndim(); });
sm.def("shape", [](const arr& a) { return arr(a.ndim(), a.shape()); });
@@ -102,6 +186,11 @@ TEST_SUBMODULE(numpy_array, sm) {
sm.def("make_f_array", [] { return py::array_t<float>({ 2, 2 }, { 4, 8 }); });
sm.def("make_c_array", [] { return py::array_t<float>({ 2, 2 }, { 8, 4 }); });
+ // test_empty_shaped_array
+ sm.def("make_empty_shaped_array", [] { return py::array(py::dtype("f"), {}, {}); });
+ // test numpy scalars (empty shape, ndim==0)
+ sm.def("scalar_int", []() { return py::array(py::dtype("i"), {}, {}, &data_i); });
+
// test_wrap
sm.def("wrap", [](py::array a) {
return py::array(
@@ -292,4 +381,10 @@ TEST_SUBMODULE(numpy_array, sm) {
std::fill(a.mutable_data(), a.mutable_data() + a.size(), 42.);
return a;
});
+
+#if PY_MAJOR_VERSION >= 3
+ sm.def("index_using_ellipsis", [](py::array a) {
+ return a[py::make_tuple(0, py::ellipsis(), 0)];
+ });
+#endif
}