[PyQt] Exception within Slot causes Abort

Dave Gradwell davegradwell at yahoo.co.uk
Sun Mar 20 10:17:30 GMT 2016


In PyQt 5.5.1, raising an Exception within a Slot results in qFatal() calling Abort.
That behaviour seems to be documented here, so in a way it seems hard to argue with:
http://doc.qt.io/qt-5/exceptionsafety.html#signals-and-slots
"""Throwing an exception from a slot invoked by Qt's signal-slot connection mechanism is considered undefined behaviour, unless it is handled within the slot"""

But it worked nicely in 5.4.1 so it seems like a pity. 
Is there any way of allowing Python Exceptions to be raised within PyQt slots?
If not, what is the recommended way of achieving something similar?

With thanks, Dave.


Python 3.4.4
Mac OS X 10.11 El Capitan. 
PyQt 5.5.1
Qt 5.5.1
SIP 4.17


Code:
""""""""""""""
import sys
from PyQt5 import QtCore, QtWidgets

app = QtWidgets.QApplication(sys.argv)

timer = QtCore.QTimer()
timer.start(1000)
timer.timeout.connect(lambda: timerFinished())

def timerFinished():
  print("timer finished")
  raise Exception
""""""""""""""


Output:
""""""""""""""
timer finished
Traceback (most recent call last):
File "/Users/dave/PycharmProjects/test/test.py", line 11, in <lambda>
  timer.timeout.connect(lambda: timerFinished())
File "/Users/dave/PycharmProjects/test/test.py", line 15, in timerFinished
  raise Exception
Exception

Process finished with exit code 134
""""""""""""""


Salient part of crash log:
""""""""""""""
Exception Type:        EXC_CRASH (SIGABRT)
[...]
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib            0x00007fff8a1e6002 __pthread_kill + 10
1   libsystem_pthread.dylib           0x00007fff8a7765c5 pthread_kill + 90
2   libsystem_c.dylib                 0x00007fff97f5f6e7 abort + 129
3   org.qt-project.QtCore             0x0000000103025ab9 qt_message_fatal(QtMsgType, QMessageLogContext const&, QString const&) + 9
4   org.qt-project.QtCore             0x0000000103027487 QMessageLogger::fatal(char const*, ...) const + 231
5   QtCore.so                         0x00000001015172b7 pyqt5_err_print() + 887
6   QtCore.so                         0x000000010151c9ac PyQtSlotProxy::unislot(void**) + 124
7   QtCore.so                         0x000000010151c915 PyQtSlotProxy::qt_metacall(QMetaObject::Call, int, void**) + 85
8   org.qt-project.QtCore             0x0000000103213209 QMetaObject::activate(QObject*, int, int, void**) + 2777
9   QtCore.so                         0x00000001014e23c7 sipQTimer::timerEvent(QTimerEvent*) + 103
""""""""""""""


More information about the PyQt mailing list