Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]throw in def_buffer cause terminate #2764

Closed
caishanli opened this issue Dec 31, 2020 · 4 comments · Fixed by #5324
Closed

[BUG]throw in def_buffer cause terminate #2764

caishanli opened this issue Dec 31, 2020 · 4 comments · Fixed by #5324
Labels

Comments

@caishanli
Copy link

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!
@caishanli caishanli changed the title throw in def_buffer cause terminate [BUG]throw in def_buffer cause terminate Dec 31, 2020
@YannickJadoul
Copy link
Collaborator

@caishanli, can you specify why you want to throw from def_buffer, though? This should be fixable, but I'd figure out if we should allow this. "Incompatible buffer format!" is not up to you to throw; def_buffer should specify what's offered and the caller will see if it can handle the buffer format offered.

@caishanli
Copy link
Author

@YannickJadoul Hi, I use pybind11 to bind Tencent/ncnn. And in ncnn::Mat, the element size(in byte) can be 1, 2, 3, 4 or any other positive integers, but as my best knowledge, python have no data type of 3 bytes, 5 bytes and so on, so I need to throw a runtime error, and the same as element pack.

@YannickJadoul
Copy link
Collaborator

@caishanli Ah, I see. And that's only known at runtime? :-(

But OK, then we mainly need a way to indicate failure from a buffer, rather than a "catch all exceptions"? The latter wouldn't be wrong to do either, though.

@caishanli
Copy link
Author

@YannickJadoul I think just catch all exceptions is ok as we have error message to indicate what's wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants