Details
-
Type:
Story
-
Status: Done
-
Resolution: Done
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
-
Story Points:4
-
Epic Link:
-
Sprint:DRP F16-1, DRP F16-2
-
Team:Data Release Production
Description
DM-6302 adds support for custom exception translators to pybind11. However exceptions mapped do not inherit from Python BaseException or higher. This prevents exceptions from being raised and caught with except Exception as e in Python. This behaviour also occurs with Boost Python and Swig (we hack around it with a pure Python wrapper).
This ticket aims to solve the problem by adding support for inheritance from Python exception types to pybind11.
Attachments
Issue Links
- blocks
-
DM-6302 Wrap pex_exceptions with pybind11
- Done
After a bit of hacking I have managed to add support for types derived from Python Exception to pybind11.
So one can easily say (in C++ only, without requiring a Python wrapper) this exception type should inherit from x (pick a favourite Python exception type) and
it all works. You can raise these exceptions in Python and catch them with except Exception as e as well as except mymodule.MyException as e.
All that is needed is to add py::base<py::Exception> to your py::class_ line in the interface file.
However, the inheritance tree can only be specified on one side of the fence.
When types inherit both in C++ and Python (say a custom tree intersecting with the standard Python exception tree) things break down (i.e. segfault).
From a first look it seems that fixing this would require somehow adding support for multiple inheritance to pybind11 as well.
This seems like quite a big task that we might want to avoid if we don't absolutely need it.
One way to do that is to only derive from Exception or BaseException such that raise and except Exception as e works on the Python side but forego on mapping some types to say ValueError and such;
Is this an option? Or do we really need multiple inheritance?