diff --git a/docs/advanced.rst b/docs/advanced.rst index c68c33af..aca13259 100644 --- a/docs/advanced.rst +++ b/docs/advanced.rst @@ -1224,12 +1224,12 @@ completely avoid copy operations with Python expressions like py::class_(m, "Matrix") .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::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::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) } ); }); @@ -1273,7 +1273,7 @@ buffer objects (e.g. a NumPy matrix). py::buffer_info info = b.request(); /* Some sanity checks ... */ - if (info.format != py::format_descriptor::value) + if (info.format != py::format_descriptor::value()) throw std::runtime_error("Incompatible format: expected a double array!"); if (info.ndim != 2) @@ -1299,7 +1299,7 @@ as follows: m.data(), /* Pointer to buffer */ sizeof(Scalar), /* Size of one scalar */ /* Python struct-style format descriptor */ - py::format_descriptor::value, + py::format_descriptor::value(), /* Number of dimensions */ 2, /* Buffer dimensions */ diff --git a/example/example-buffers.cpp b/example/example-buffers.cpp index 17c8d271..2deee6f8 100644 --- a/example/example-buffers.cpp +++ b/example/example-buffers.cpp @@ -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::value || info.ndim != 2) + if (info.format != py::format_descriptor::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()); @@ -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::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::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) } ); }) diff --git a/include/pybind11/common.h b/include/pybind11/common.h index d1053507..ca5765ab 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -204,7 +204,7 @@ struct buffer_info { void *ptr; // Pointer to the underlying storage size_t itemsize; // Size of individual items in bytes size_t size; // Total number of entries - std::string format; // For homogeneous buffers, this should be set to format_descriptor::value + std::string format; // For homogeneous buffers, this should be set to format_descriptor::value() size_t ndim; // Number of dimensions std::vector shape; // Shape of the tensor (1 entry per dimension) std::vector strides; // Number of entries between adjacent entries (for each per dimension) @@ -348,14 +348,22 @@ PYBIND11_RUNTIME_EXCEPTION(reference_cast_error) /// Used internally [[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const std::string &reason) { throw std::runtime_error(reason); } /// Format strings for basic number types -#define PYBIND11_DECL_FMT(t, v) template<> struct format_descriptor { static constexpr const char *value = v; } +#define PYBIND11_DECL_FMT(t, v) template<> struct format_descriptor \ + { static constexpr const char* value() { return v; } }; + template struct format_descriptor { }; + template struct format_descriptor::value>::type> { - static constexpr const char value[2] = + static constexpr const char* value() { return format; } + static constexpr const char format[2] = { "bBhHiIqQ"[detail::log2(sizeof(T))*2 + (std::is_unsigned::value ? 1 : 0)], '\0' }; }; + template constexpr const char format_descriptor< - T, typename std::enable_if::value>::type>::value[2]; -PYBIND11_DECL_FMT(float, "f"); PYBIND11_DECL_FMT(double, "d"); PYBIND11_DECL_FMT(bool, "?"); + T, typename std::enable_if::value>::type>::format[2]; + +PYBIND11_DECL_FMT(float, "f"); +PYBIND11_DECL_FMT(double, "d"); +PYBIND11_DECL_FMT(bool, "?"); NAMESPACE_END(pybind11) diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index bfb93406..db8f9eb1 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -133,7 +133,7 @@ struct type_caster::value && /* Size of one scalar */ sizeof(Scalar), /* Python struct-style format descriptor */ - format_descriptor::value, + format_descriptor::value(), /* Number of dimensions */ 1, /* Buffer dimensions */ @@ -148,7 +148,7 @@ struct type_caster::value && /* Size of one scalar */ sizeof(Scalar), /* Python struct-style format descriptor */ - format_descriptor::value, + format_descriptor::value(), /* Number of dimensions */ isVector ? 1 : 2, /* Buffer dimensions */ @@ -233,7 +233,7 @@ struct type_caster::value>:: try { obj = matrix_type(obj); } catch (const error_already_set &) { - PyErr_Clear(); + PyErr_Clear(); return false; } } @@ -276,7 +276,7 @@ struct type_caster::value>:: // Size of one scalar sizeof(Scalar), // Python struct-style format descriptor - format_descriptor::value, + format_descriptor::value(), // Number of dimensions 1, // Buffer dimensions @@ -291,7 +291,7 @@ struct type_caster::value>:: // Size of one scalar sizeof(StorageIndex), // Python struct-style format descriptor - format_descriptor::value, + format_descriptor::value(), // Number of dimensions 1, // Buffer dimensions @@ -306,7 +306,7 @@ struct type_caster::value>:: // Size of one scalar sizeof(StorageIndex), // Python struct-style format descriptor - format_descriptor::value, + format_descriptor::value(), // Number of dimensions 1, // Buffer dimensions diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index e8182acd..7dff28da 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -351,7 +351,7 @@ struct vectorize_helper { return cast(f(*((Args *) buffers[Index].ptr)...)); array result(buffer_info(nullptr, sizeof(Return), - format_descriptor::value, + format_descriptor::value(), ndim, shape, strides)); buffer_info buf = result.request();