common.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:pyjam 作者: 10se1ucgo 项目源码 文件源码
def wrap_exceptions(func):
    """Wraps a function with an "exception hook" for threads.

    Args:
        func (function): The function to wrap.

    Returns:
        function: The wrapped function
    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        # args[0] = when wrapping a class method. (IT BETTER BE. WHYDOISUCKATPROGRAMMINGOHGOD)
        # This should really only be used on threads (main thread has a sys.excepthook)
        try:
            return func(*args, **kwargs)
        except (SystemExit, KeyboardInterrupt):
            raise
        except Exception:
            if isinstance(args[0], wx.TopLevelWindow):
                parent = args[0]
            elif hasattr(args[0], "parent") and isinstance(args[0].parent, wx.TopLevelWindow):
                parent = args[0].parent
            else:
                parent = wx.GetApp().GetTopWindow()
            error_message = ''.join(traceback.format_exc())
            error_dialog = wx.MessageDialog(parent=parent,
                                            message="An error has occured\n\n" + error_message,
                                            caption="Error!", style=wx.OK | wx.ICON_ERROR)
            error_dialog.RequestUserAttention()
            error_dialog.ShowModal()
            error_dialog.Destroy()
            logger.critical(error_message)
            raise

    return wrapper
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号