-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add support for user defined exception translators #273
Conversation
Hello Pim, I had a look at your PR and added a few changes: The first was with regards to your usage of I think it's reasonable to impose that exception translators should be stateless, which makes things even simpler. Thus I switched from
to raw function pointers, e.g.
I also removed the A patch with these changes and some documentation edits is here: patch.txt At that point, I unfortunately hit a snag. On Python 3.5, I get the following error message:
So it looks like there is no way around creating some kind of py::exception wrapper as disucussed here: https://github.com/lsst-dm/pybind11/pull/3/files. However, a lean version of this modification would need to be found that doesn't create overheads for normal non-exception objects. |
7dc65ac
to
949b237
Compare
Hi, I agree with your changes. State is not important, and it is nice to be lean. As for the Python 3.5 incompatibility. That is indeed unfortunate. And I would like to add the functionality for inheriting from Kind regards, Pim Schellart |
What are your thoughts on adding a wrapper |
looks like there is some minor issue with the name of the exception on Python 3.5 (Travis CI build bot) |
949b237
to
c0f8040
Compare
Yes, that sounds like a good idea. I'll put it in and update the request. |
PyModule_AddObject(m.ptr(), "MyException", my_exception_type); | ||
|
||
// make a new custom exception and use it as a translation target | ||
static py::exception<MyException> ex(m, "MyException"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: change
static py::exception<MyException> ex(m, "MyException");
to
py::exception<MyException> ex(m, "MyException");
together with other change below
I'll add the |
no, this seems reasonable then. |
Ok, shall I rebase all commits into a single one? Or do you want something else for the merge? Assuming you want the code of course. |
Rebase + squash will be perfect! |
Did a rebase + squash + a minor change to the documentation. |
Perfect -- thank you very much for contributing this feature! |
Thanks for taking it! And for the productive discussion. |
The custom exception handling added in PR pybind#273 is robust, but is overly complex for declaring the most common simple C++ -> Python exception mapping that needs only to copy `what()`. This add a simple py::register_exception<CppException>(module, "PythonException"); function that greatly simplifies the basic case of translation of a simple CppException into a simple PythonException.
The custom exception handling added in PR pybind#273 is robust, but is overly complex for declaring the most common simple C++ -> Python exception mapping that needs only to copy `what()`. This add a simple py::register_exception<CppException>(module, "PythonException"); function that greatly simplifies the basic case of translation of a simple CppException into a simple PythonException.
The custom exception handling added in PR pybind#273 is robust, but is overly complex for declaring the most common simple C++ -> Python exception mapping that needs only to copy `what()`. This add a simple py::register_exception<CppException>(module, "PythonException"); function that greatly simplifies the basic case of translation of a simple CppException into a simple PythonException.
The custom exception handling added in PR pybind#273 is robust, but is overly complex for declaring the most common simple C++ -> Python exception mapping that needs only to copy `what()`. This add a simpler `py::register_exception<CppExp>(module, "PyExp");` function that greatly simplifies the common basic case of translation of a simple CppException into a simple PythonException, while not removing the more advanced capabilities of defining custom exception handlers.
The custom exception handling added in PR #273 is robust, but is overly complex for declaring the most common simple C++ -> Python exception mapping that needs only to copy `what()`. This add a simpler `py::register_exception<CppExp>(module, "PyExp");` function that greatly simplifies the common basic case of translation of a simple CppException into a simple PythonException, while not removing the more advanced capabilities of defining custom exception handlers.
Implementation of the feature requested in issue #246.