mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-11 17:00:34 +00:00
Make changes to format_descriptor backwards-compat
The format strings that are known at compile time are now accessible via both ::value and ::format(), and format strings for everything else is accessible via ::format(). This makes it backwards compatible.
This commit is contained in:
@@ -81,7 +81,7 @@ void init_ex_buffers(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>::format() || 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());
|
||||
@@ -104,12 +104,12 @@ void init_ex_buffers(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>::format(), /* 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) }
|
||||
);
|
||||
})
|
||||
|
||||
@@ -47,7 +47,7 @@ std::ostream& operator<<(std::ostream& os, const NestedStruct& v) {
|
||||
template <typename T>
|
||||
py::array mkarray_via_buffer(size_t n) {
|
||||
return py::array(py::buffer_info(nullptr, sizeof(T),
|
||||
py::format_descriptor<T>::value(),
|
||||
py::format_descriptor<T>::format(),
|
||||
1, { n }, { sizeof(T) }));
|
||||
}
|
||||
|
||||
@@ -80,9 +80,9 @@ void print_recarray(py::array_t<S> arr) {
|
||||
}
|
||||
|
||||
void print_format_descriptors() {
|
||||
std::cout << py::format_descriptor<SimpleStruct>::value() << std::endl;
|
||||
std::cout << py::format_descriptor<PackedStruct>::value() << std::endl;
|
||||
std::cout << py::format_descriptor<NestedStruct>::value() << std::endl;
|
||||
std::cout << py::format_descriptor<SimpleStruct>::format() << std::endl;
|
||||
std::cout << py::format_descriptor<PackedStruct>::format() << std::endl;
|
||||
std::cout << py::format_descriptor<NestedStruct>::format() << std::endl;
|
||||
}
|
||||
|
||||
void print_dtypes() {
|
||||
|
||||
Reference in New Issue
Block a user