Python Forum
[PyQt] Qt app won't crash after sys.excepthook override
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Qt app won't crash after sys.excepthook override
#1
Bug 
As detailed in the docs, all exceptions can be caught by monkey patching sys.excepthook, while __excepthook__ contains the original value. However, when used in some Qt events, altough the error is caught, the app doesn't crash.

In the example below, the app crash when raise is called in __init__, but not in keyPressEvent. The workaround is to call sys.exit(1) after __excepthook__.
But why is it needed? Anyone has an idea?

#!/usr/bin/python3
import sys
from PyQt5 import QtWidgets


class Text(QtWidgets.QPlainTextEdit):
    def __init__(self):
        super().__init__()
        # raise NotImplementedError  # Crash (expected behavior)

    def keyPressEvent(self, event):
        raise NotImplementedError  # Does not crash (unexpected)
        super().keyPressEvent(event)


class Main(QtWidgets.QWidget):
    def __init__(self, parent):
        super().__init__()
        layout = QtWidgets.QVBoxLayout()
        layout.insertWidget(0, Text())
        self.setLayout(layout)
        self.show()


def _exception(type_, value, tb):
    print(f"Caught: {type_} {value}".rstrip())
    sys.__excepthook__(type_, value, tb)
    # sys.exit(1)


sys.excepthook = _exception


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    gui = Main(app)
    app.exec()
Reply


Messages In This Thread
Qt app won't crash after sys.excepthook override - by Alfalfa - Dec-27-2020, 08:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Override the paste function(copy from excel file - paste in separate tkinter entryes) AndreiV 3 4,700 Jun-05-2020, 04:46 PM
Last Post: AndreiV
  [PyQt] timer makes my qmessagebox crash darktitan 17 7,930 Feb-19-2020, 07:11 PM
Last Post: darktitan
  PY TO EXE CRASH kozaizsvemira 0 2,305 Oct-19-2019, 07:27 PM
Last Post: kozaizsvemira

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020