
import os
import platform
import sys, logging, datetime, tempfile, pathlib
import logging.handlers
import traceback as tb_module
from typing import Type, Optional
from types import TracebackType

from PySide6.QtWidgets import QApplication, QMessageBox
from PySide6 import QtGui

[...]

# by default Qt abort on Python exceptions so we need to provide
# our own hook that does the job
def handle_exception(type_: Type[BaseException], value: BaseException, traceback: Optional[TracebackType]) -> None:
    # note: the argument names have to have the same name as in the stdlib
    # else mypy will complain.
    if issubclass(type_, KeyboardInterrupt) and traceback is not None:
        sys.__excepthook__(type_, value, traceback)
        return

    # yeah ... logging also raises exceptions sometimes...
    # noinspection PyBroadException
    try:
        logging.error("Uncaught exception", exc_info=(type_, value, traceback))
    except Exception:
        pass

    if app:
        exc_msg = ''.join( tb_module.format_exception(type_, value, traceback) )
        # noinspection PyTypeChecker
        QMessageBox.critical(None, 'Fatal error',
'''A fatal error occured:
%s\n
Please report the problem on opening an issue on https://github.com/idemia/multigit/ .
Please include the file log_multigit_debug.log which you can find in the menu About / Show Multigit log files .
''' % exc_msg )

    # do nothing else, it's enough already



# mandatory to avoid Python crashing on exceptions raised inside slots
app: Optional[ QApplication ] = None

def main_gui() -> None:
    global app
    app = QApplication([])
    w = MgMainWindow(sys.argv)
    w.show()
    app.exec_()


def main() -> None:

    # to avoid crashes when Python exceptions are raised inside Qt slots
    sys.excepthook = handle_exception

    [...]

    main_gui()


if __name__ == '__main__':
    main()

