[PyQt] Exceptions in Python Implementations of Virtuals
Mathias.Born at gmx.de
Mathias.Born at gmx.de
Tue Sep 30 20:56:10 BST 2014
I have a strong opinion about this, which I'd like to share.
The Python re-implementation is called by C++ code. This code could
have been written by someone who never expected the re-implementation
to be written in Python. Thus, a good way to look at this is what
the author of the base class has planned for this case. Qt itself is a
If I call a virtual method, knowing that this might call a
re-implementation which I know nothing about, I must have a plan in place
to handle errors. Not to care about errors is simply inacceptable.
The canonical C++ way is to rely on exceptions. And this is also how
the Python re-implementation should be treated. Thus, any exception
raised by the Python code should throw a C++ exception into the
Some libraries like Qt explicitely avoid exceptions for a number
of reasons. There, it is the responsibility of the re-implementation
not to throw any. Thus, even a Python re-implementation should not.
In this thread, it has already been pointed out how easy it is in
Python to inadvertently cause an exception because of a spelling
mistake. I believe this to be a very poor argument. This sort of
error is a typical weakness of an interpreted language like Python.
If you use Python, you simply have to accept that risk.
(Or, guard your code with exception handlers, use static analysis etc.)
It is wrong to make this go away by simply ignoring all exceptions.
Finally, the question remains how to convert the Python exception
into a C++ exception. The already existing type-mapping could deal
with this, using std::runtime_error as a default.
You could even ignore any "noexcept" exception specification, because
throwing an exception inside a "noexcept" method still does the
right thing, which is to call std::terminate().
It's true that introducing such a behavior now is a significant
change. However, Python code which relies on the current behavior
of simply ignoring exceptions is already broken anyway.
On 30.09.2014, 18:23:08 Phil Thompson wrote:
> Florian asked this question but it didn't result in any discussion, so
> I'd like to have another poke at it.
> Currently, if an exception is raised by a Python re-implementation of a
> C++ virtual then the exception is printed and the application continues.
> A default result will be constructed by PyQt and returned to C++.
> Instead, should PyQt print the exception and then terminate (by calling
> Some advantages...
> - consistency in what happens when an unhandled exception is raised (ie.
> the application always terminates)
> - such bugs become obvious and easy to find
> Some disadvantages...
> - incompatible with current behaviour
> - may cause problems with applications that already try to handle such
> exceptions using exception hooks
> If I make this change it will be for PyQt5 only. I don't plan to make
> the change configurable in any way.
> PyQt mailing list PyQt at riverbankcomputing.com
More information about the PyQt