diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 22d44c808..b6d380497 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -26,6 +26,7 @@ set(PYBIND11_EXAMPLES example-stl-binder-vector.cpp example-eval.cpp example-custom-exceptions.cpp + example20.cpp issues.cpp ) @@ -65,4 +66,3 @@ foreach(VALUE ${PYBIND11_EXAMPLES}) string(REGEX REPLACE "^(.+).cpp$" "\\1" EXAMPLE_NAME "${VALUE}") add_test(NAME ${EXAMPLE_NAME} COMMAND ${RUN_TEST} ${EXAMPLE_NAME}) endforeach() - diff --git a/example/example.cpp b/example/example.cpp index b831e3e84..819f69f44 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -29,6 +29,7 @@ void init_ex_inheritance(py::module &); void init_ex_stl_binder_vector(py::module &); void init_ex_eval(py::module &); void init_ex_custom_exceptions(py::module &); +void init_ex20(py::module &); void init_issues(py::module &); #if defined(PYBIND11_TEST_EIGEN) @@ -72,6 +73,7 @@ PYBIND11_PLUGIN(example) { init_ex_stl_binder_vector(m); init_ex_eval(m); init_ex_custom_exceptions(m); + init_ex20(m); init_issues(m); #if defined(PYBIND11_TEST_EIGEN) diff --git a/example/example20.cpp b/example/example20.cpp new file mode 100644 index 000000000..c22eb213e --- /dev/null +++ b/example/example20.cpp @@ -0,0 +1,57 @@ +/* + example/example20.cpp -- Usage of structured numpy dtypes + + Copyright (c) 2016 Ivan Smirnov + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "example.h" + +#include +#include +#include + +namespace py = pybind11; + +struct Struct { + bool x; + uint32_t y; + float z; +}; + +struct PackedStruct { + bool x; + uint32_t y; + float z; +} __attribute__((packed)); + +struct NestedStruct { + Struct a; + PackedStruct b; +}; + +template +py::array_t create_recarray(size_t n) { + auto arr = py::array(py::buffer_info(nullptr, sizeof(S), + py::format_descriptor::value(), + 1, { n }, { sizeof(S) })); + auto buf = arr.request(); + auto ptr = static_cast(buf.ptr); + for (size_t i = 0; i < n; i++) { + ptr[i].x = i % 2; + ptr[i].y = i; + ptr[i].z = i * 1.5; + } + return arr; +} + +void init_ex20(py::module &m) { + PYBIND11_DTYPE(Struct, x, y, z); + PYBIND11_DTYPE(PackedStruct, x, y, z); + PYBIND11_DTYPE(NestedStruct, a, b); + + m.def("create_rec_simple", &create_recarray); + m.def("create_rec_packed", &create_recarray); +} diff --git a/example/example20.py b/example/example20.py new file mode 100644 index 000000000..ed3454860 --- /dev/null +++ b/example/example20.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python +from __future__ import print_function + +import numpy as np +from example import create_rec_simple + + +def check_eq(arr, data, dtype): + np.testing.assert_equal(arr, np.array(data, dtype=dtype)) + +dtype = np.dtype({'names': ['x', 'y', 'z'], + 'formats': ['?', 'u4', 'f4'], + 'offsets': [0, 4, 8]}) +base_dtype = np.dtype([('x', '?'), ('y', 'u4'), ('z', 'f4')]) + +arr = create_rec_simple(3) +assert arr.dtype == dtype +check_eq(arr, [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)], dtype) +check_eq(arr, [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)], base_dtype) diff --git a/example/example20.ref b/example/example20.ref new file mode 100644 index 000000000..e69de29bb