[PyQt] Exception within Slot causes Abort
Florian Bruhin
me at the-compiler.org
Sun Mar 20 15:46:41 GMT 2016
* Dave Gradwell <davegradwell at yahoo.co.uk> [2016-03-20 13:20:41 +0000]:
> With PyQt 5.5 the behaviour is inconsistent (consequence depends on context);
> and aborting because you encountered something exceptional seems really quite
> disproportionate to me. And not at all in the spirit of Exceptions as a
> programmer's friend.
I'm not aware of any programming language which has exceptions and
does not abort on an unhandled one.
> > No, it doesn't continue. It quits.
> I am unable to see this behaviour.
> For me, with PyQt 5.4, it continues.
> And to clarify what I mean by 'it continues' I could either say 'it
> doesn't abort' or 'it behaves like a non PyQt Python program would'.
> I am not suggesting it pushes on past the exception or something
> ridiculous like that.
I still don't follow. Any Python code not involving PyQt does *quit*
when there's an unhandled exception:
$ cat exc.py
print("Before an exception")
1/0
print("After an exception (not printed)")
$ python exc.py
Before an exception
Traceback (most recent call last):
File "exc.py", line 2, in <module>
1/0
ZeroDivisionError: division by zero
Since PyQt 5.5, PyQt does the same.
If you handle the exception, neither a PyQt nor a non-PyQt application
quits - adopted from your example:
$ cat exc2.py
import sys
from PyQt5 import QtCore, QtWidgets
def timerFinished():
print("timer finished")
try:
raise Exception
except Exception:
print("whoops")
app = QtWidgets.QApplication(sys.argv)
timer = QtCore.QTimer()
timer.start(1000)
timer.timeout.connect(timerFinished)
app.exec_()
$ python exc2.py
timer finished
whoops
timer finished
whoops
So really the only thing which changes is that PyQt 5.5+ does not
silently ignore unhandled exceptions anymore, like *everything else
does*. How is this not consistent?
> If so, what construct can I use to regain the nice behaviour of PyQt 5.4?
You can do:
sys.excepthook = traceback.print_exception
But again, you'll have undefined behaviour on exceptions inside C++
virtuals then.
> Also, the Abortion is in full swing by this time. I.e. We're doomed. No?
If you have an unhandled exception, you're kind of doomed anyways?
Florian
--
http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc
I love long mails! | http://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160320/ea585868/attachment-0001.sig>
More information about the PyQt
mailing list