Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-2321

Exceptions raised from event handlers cause the traceback module to crash

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • Not Evaluated
    • 6.5.1, 6.6.0
    • 6.5.0
    • PySide
    • None
    • macOS
    • 91d0d3f1e (dev), 5fb531118 (6.5), baedbe835 (dev), 28ddc0e50 (6.5)

    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

        1. crashtest.zip
          0.8 kB
        2. pyside2321_pyqt.py
          0.6 kB
        3. pyside2321.py
          2 kB

        Issue Links

          For Gerrit Dashboard: PYSIDE-2321
          # Subject Branch Project Status CR V

          Activity

            People

              ctismer Christian Tismer
              rkoecher Rene Koecher
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews