mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-13 01:36:21 +00:00
redesigned format_descriptor<> and npy_format_descriptor<>
This somewhat heavyweight solution will avoid size_t/long long/long/int mismatches on various platforms once and for all. The previous template overloads could e.g. not handle size_t on Darwin. One gotcha: the 'format_descriptor<T>::value()' syntax changed to just 'format_descriptor<T>::value'
This commit is contained in:
@@ -80,7 +80,7 @@ void init_ex7(py::module &m) {
|
||||
/// Construct from a buffer
|
||||
.def("__init__", [](Matrix &v, py::buffer b) {
|
||||
py::buffer_info info = b.request();
|
||||
if (info.format != py::format_descriptor<float>::value() || info.ndim != 2)
|
||||
if (info.format != py::format_descriptor<float>::value || info.ndim != 2)
|
||||
throw std::runtime_error("Incompatible buffer format!");
|
||||
new (&v) Matrix(info.shape[0], info.shape[1]);
|
||||
memcpy(v.data(), info.ptr, sizeof(float) * v.rows() * v.cols());
|
||||
@@ -103,12 +103,12 @@ void init_ex7(py::module &m) {
|
||||
/// Provide buffer access
|
||||
.def_buffer([](Matrix &m) -> py::buffer_info {
|
||||
return py::buffer_info(
|
||||
m.data(), /* Pointer to buffer */
|
||||
sizeof(float), /* Size of one scalar */
|
||||
py::format_descriptor<float>::value(), /* Python struct-style format descriptor */
|
||||
2, /* Number of dimensions */
|
||||
{ m.rows(), m.cols() }, /* Buffer dimensions */
|
||||
{ sizeof(float) * m.rows(), /* Strides (in bytes) for each index */
|
||||
m.data(), /* Pointer to buffer */
|
||||
sizeof(float), /* Size of one scalar */
|
||||
py::format_descriptor<float>::value, /* Python struct-style format descriptor */
|
||||
2, /* Number of dimensions */
|
||||
{ m.rows(), m.cols() }, /* Buffer dimensions */
|
||||
{ sizeof(float) * m.rows(), /* Strides (in bytes) for each index */
|
||||
sizeof(float) }
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user