aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/pybind11/tests/test_numpy_dtypes.cpp
diff options
context:
space:
mode:
authorMiodrag Milanovic <mmicko@gmail.com>2021-01-02 10:15:39 +0100
committerMiodrag Milanovic <mmicko@gmail.com>2021-01-02 10:15:39 +0100
commite76cdab6dd77bad411e6ac9372ee527aff89ef17 (patch)
treee9868f05cf455336d75f33b1312d71034f8fb334 /3rdparty/pybind11/tests/test_numpy_dtypes.cpp
parentc6cdf30501dcb2da01361229dd66a05dad73a132 (diff)
downloadnextpnr-e76cdab6dd77bad411e6ac9372ee527aff89ef17.tar.gz
nextpnr-e76cdab6dd77bad411e6ac9372ee527aff89ef17.tar.bz2
nextpnr-e76cdab6dd77bad411e6ac9372ee527aff89ef17.zip
Update pybind11 to version 2.6.1
Diffstat (limited to '3rdparty/pybind11/tests/test_numpy_dtypes.cpp')
-rw-r--r--3rdparty/pybind11/tests/test_numpy_dtypes.cpp39
1 files changed, 32 insertions, 7 deletions
diff --git a/3rdparty/pybind11/tests/test_numpy_dtypes.cpp b/3rdparty/pybind11/tests/test_numpy_dtypes.cpp
index 467e0253..b2e5e607 100644
--- a/3rdparty/pybind11/tests/test_numpy_dtypes.cpp
+++ b/3rdparty/pybind11/tests/test_numpy_dtypes.cpp
@@ -168,7 +168,7 @@ py::list print_recarray(py::array_t<S, 0> arr) {
const auto req = arr.request();
const auto ptr = static_cast<S*>(req.ptr);
auto l = py::list();
- for (ssize_t i = 0; i < req.size; i++) {
+ for (py::ssize_t i = 0; i < req.size; i++) {
std::stringstream ss;
ss << ptr[i];
l.append(py::str(ss.str()));
@@ -180,8 +180,8 @@ py::array_t<int32_t, 0> test_array_ctors(int i) {
using arr_t = py::array_t<int32_t, 0>;
std::vector<int32_t> data { 1, 2, 3, 4, 5, 6 };
- std::vector<ssize_t> shape { 3, 2 };
- std::vector<ssize_t> strides { 8, 4 };
+ std::vector<py::ssize_t> shape { 3, 2 };
+ std::vector<py::ssize_t> strides { 8, 4 };
auto ptr = data.data();
auto vptr = (void *) ptr;
@@ -255,11 +255,30 @@ struct A {};
struct B {};
TEST_SUBMODULE(numpy_dtypes, m) {
- try { py::module::import("numpy"); }
+ try { py::module_::import("numpy"); }
catch (...) { return; }
// typeinfo may be registered before the dtype descriptor for scalar casts to work...
- py::class_<SimpleStruct>(m, "SimpleStruct");
+ py::class_<SimpleStruct>(m, "SimpleStruct")
+ // Explicit construct to ensure zero-valued initialization.
+ .def(py::init([]() { return SimpleStruct(); }))
+ .def_readwrite("bool_", &SimpleStruct::bool_)
+ .def_readwrite("uint_", &SimpleStruct::uint_)
+ .def_readwrite("float_", &SimpleStruct::float_)
+ .def_readwrite("ldbl_", &SimpleStruct::ldbl_)
+ .def("astuple", [](const SimpleStruct& self) {
+ return py::make_tuple(self.bool_, self.uint_, self.float_, self.ldbl_);
+ })
+ .def_static("fromtuple", [](const py::tuple tup) {
+ if (py::len(tup) != 4) {
+ throw py::cast_error("Invalid size");
+ }
+ return SimpleStruct{
+ tup[0].cast<bool>(),
+ tup[1].cast<uint32_t>(),
+ tup[2].cast<float>(),
+ tup[3].cast<long double>()};
+ });
PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_);
PYBIND11_NUMPY_DTYPE(SimpleStructReordered, bool_, uint_, float_, ldbl_);
@@ -379,7 +398,7 @@ TEST_SUBMODULE(numpy_dtypes, m) {
if (non_empty) {
auto req = arr.request();
auto ptr = static_cast<StringStruct*>(req.ptr);
- for (ssize_t i = 0; i < req.size * req.itemsize; i++)
+ for (py::ssize_t i = 0; i < req.size * req.itemsize; i++)
static_cast<char*>(req.ptr)[i] = 0;
ptr[1].a[0] = 'a'; ptr[1].b[0] = 'a';
ptr[2].a[0] = 'a'; ptr[2].b[0] = 'a';
@@ -462,10 +481,16 @@ TEST_SUBMODULE(numpy_dtypes, m) {
m.def("buffer_to_dtype", [](py::buffer& buf) { return py::dtype(buf.request()); });
// test_scalar_conversion
- m.def("f_simple", [](SimpleStruct s) { return s.uint_ * 10; });
+ auto f_simple = [](SimpleStruct s) { return s.uint_ * 10; };
+ m.def("f_simple", f_simple);
m.def("f_packed", [](PackedStruct s) { return s.uint_ * 10; });
m.def("f_nested", [](NestedStruct s) { return s.a.uint_ * 10; });
+ // test_vectorize
+ m.def("f_simple_vectorized", py::vectorize(f_simple));
+ auto f_simple_pass_thru = [](SimpleStruct s) { return s; };
+ m.def("f_simple_pass_thru_vectorized", py::vectorize(f_simple_pass_thru));
+
// test_register_dtype
m.def("register_dtype", []() { PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_); });