Repository navigation
Replies: 1 comment
|
🤖 AI text below 🤖
auto &exc_type = py::register_exception<CustomCppExc>(m, "CustomPyExc");
auto fut = py::module_::import("asyncio").attr("Future")();
fut.attr("set_exception")(exc_type("UH-OH")); // instance
// or pass the type itself: fut.attr("set_exception")(exc_type);For built-in exceptions, use the C API globals: py::print(py::handle(PyExc_ValueError)); // <class 'ValueError'>
auto err = py::handle(PyExc_RuntimeError)("UH-OH"); // RuntimeError('UH-OH')If you need the type somewhere else, get it from the module with |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
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 withasynciomodule. The underlying problem is that I am unable (I don't know how) to pass exception as an argument to python function.Thank you!
All reactions