Skip to content

[BUG]throw in def_buffer cause terminate #2764

Closed
@caishanli

Description

@caishanli

cpp

.def_buffer([](Matrix &m) -> py::buffer_info {
    throw std::runtime_error("Incompatible buffer format!");
    return py::buffer_info(
        m.data(),                                /* Pointer to buffer */
        sizeof(Scalar),                          /* Size of one scalar */
        py::format_descriptor<Scalar>::format(), /* Python struct-style format descriptor */
        2,                                       /* Number of dimensions */
        { m.rows(), m.cols() },                  /* Buffer dimensions */
        { sizeof(Scalar) * (rowMajor ? m.cols() : 1),
          sizeof(Scalar) * (rowMajor ? 1 : m.rows()) }
                                                 /* Strides (in bytes) for each index */
    );
 })

and in python

m = Matrix(3, 2)
np.array(m)

cause python terminate, output is

terminate called after throwing an instance of 'std::runtime_error'
  what():  Incompatible buffer format!
Aborted (core dumped)

expected output is runtime error, not terminate

RuntimeError: Incompatible buffer format!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions