Details
Description
It seems that PySide, somewhere down the line, either changes or sets an invalid context for native Python exceptions that are not caught within an event handler or a signal.
If an exception like this is caught and `traceback.print_exc()` is called on it the backtrace will fail and the python interpreter will crash soon after.
I attached a sample program - simply launch it in a terminal, press a key a few times and then try to close the window.
Scenario:
- a global exception_hook is set to catch all otherwise unhandled exceptions
- alternatively a a try-catch block is defined on a higher level like main()
- a Python class derives from QtCore.QWidget (or any QtCore.QObject subclass)
- the Python class overrides an event handler like `show_event` or `key_press_event`
- an exception is generated, but not handled in the event handler method
- the exception is handled in the exception_hook and a traceback is attempted via `traceback.print_exc()` or any of the methods in this module
Excepted Behaviour:
- the exception along with it's details and a backtrace should be printed
- the program execution should continue or at least cleanly terminate
Actual Behaviour:
- the traceback fails because the Exception raised from the Qt event handler has an invalid {}context{} property (a `str` object instead of None or another Exception)
- this failure causes a chain of other errors down the line
- python crashes with a segmentation fault soon after
Workaround:
- check the if `exc_value._context_` is an instance of `str` and if so set it to None and call `traceback.print_exeption()` on the modified `exc_value`
Here's the crash-log from the sample application:
python ./crashtest.py Caught an exception with an invalid context: "Delayed ValueError exception:" Error in sys.excepthook: Traceback (most recent call last): File "/Users/rkoecher/Projects/crashtest/./crashtest.py", line 22, in my_excepthook traceback.print_exception(exc_type, exc_value, tb) File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/traceback.py", line 103, in print_exception for line in TracebackException( File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/traceback.py", line 496, in __init__ exc_value.__context__.__traceback__, AttributeError: 'str' object has no attribute '__traceback__' Original exception was: TypeError: print_exception(): Exception expected for value, str found During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/rkoecher/Projects/crashtest/./crashtest.py", line 31, in <module> sys.exit(app.exec()) File "/Users/rkoecher/Projects/crashtest/./crashtest.py", line 11, in key_press_event raise ValueError('Value error from KeyPress!') ValueError: Value error from KeyPress!
Attachments
Issue Links
- is duplicated by
-
PYSIDE-2335 Exceptions in __cause__ turn into str when raised inside virtual methods
- Closed
- relates to
-
PYSIDE-2745 Exceptions are suppressed when thrown by a zero-delay single-shot timer
- Closed
-
PYSIDE-2310 Reg->6.4.3: Crashing without error reporting in Qt-based threads starting from PySide6 v6.4.3
- Closed
For Gerrit Dashboard: PYSIDE-2321 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
477688,4 | shiboken: Simplify and tighten delayed exception reporting | dev | pyside/pyside-setup | Status: ABANDONED | +1 | 0 |
478729,2 | Shiboken: Undo the unraisable exception delay for now | dev | pyside/pyside-setup | Status: MERGED | +2 | 0 |
478804,2 | Shiboken: Undo the unraisable exception delay for now | 6.5 | pyside/pyside-setup | Status: MERGED | +2 | 0 |
479761,13 | shiboken: Implement raising of unraisable exceptions correctly | dev | pyside/pyside-setup | Status: MERGED | +2 | 0 |
482278,2 | shiboken: Implement raising of unraisable exceptions correctly | 6.5 | pyside/pyside-setup | Status: MERGED | +2 | 0 |