You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The problem:
I am trying to pass an exception as an argument to python function. I think that this should work because it works in Python. For example I can do this.
Now, if I try to do the same thing in C++ using pybind11 like this
py::print(py::value_error());
I get this error message "Unable to convert call argument '0' of type 'value_error' to Python object". I think that this is expected because as the documentation says here "Note that pybind11 wrappers around Python exceptions such as pybind11::value_error are not Python exceptions; they are C++ exceptions". Therefore, I tried to cast the error like this.
py::print(py::cast(py::value_error()));
then I got this error message "Unable to convert call argument '0' of type 'object' to Python object" which is a bit confusing. The last thing that I tried was to create exception using Python C API. Then I got this error message "Unable to convert call argument '0' of type '_object*' to Python object".
My goal:
I want to register a custom exception like advised in the documentation py::register_exception<CppExp>(module, "PyExp"); and then being able to pass this custom exception as an argument to a python function. Here is a full example.
py::register_exception<CustomCppExc>(module, "CustomPyExc");
auto py_future = py::module_::import("asyncio").attr("Future")();
py_future.attr("set_exception")(CustomCppExc);
I understand that pybind11 does not have support for asyncio. However, this has nothing to do with asyncio module. The underlying problem is that I am unable (I don't know how) to pass exception as an argument to python function.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello and Thanks for your help in advance.
The problem:
I am trying to pass an exception as an argument to python function. I think that this should work because it works in Python. For example I can do this.
or more practical thing like this
Now, if I try to do the same thing in C++ using pybind11 like this
py::print(py::value_error());
I get this error message
"Unable to convert call argument '0' of type 'value_error' to Python object"
. I think that this is expected because as the documentation says here "Note that pybind11 wrappers around Python exceptions such as pybind11::value_error are not Python exceptions; they are C++ exceptions". Therefore, I tried to cast the error like this.py::print(py::cast(py::value_error()));
then I got this error message
"Unable to convert call argument '0' of type 'object' to Python object"
which is a bit confusing. The last thing that I tried was to create exception using Python C API. Then I got this error message"Unable to convert call argument '0' of type '_object*' to Python object"
.My goal:
I want to register a custom exception like advised in the documentation
py::register_exception<CppExp>(module, "PyExp");
and then being able to pass this custom exception as an argument to a python function. Here is a full example.I understand that pybind11 does not have support for
asyncio
. However, this has nothing to do withasyncio
module. The underlying problem is that I am unable (I don't know how) to pass exception as an argument to python function.Thank you!
Beta Was this translation helpful? Give feedback.
All reactions