Crash reporting in Python

帅比萌擦擦* 提交于 2019-12-04 22:57:18

问题


Is there a crash reporting framework that can be used for pure Python Tkinter applications? Ideally, it should work cross-platform.

Practically speaking, this is more of 'exception reporting' since the Python interpreter itself hardly crashes.

Here's a sample crash reporter:


回答1:


Rather than polluting your code with try..except everywhere, you should just implement your own except hook by setting sys.excepthook. Here is an example:

import sys
import traceback

def install_excepthook():
    def my_excepthook(exctype, value, tb):
        s = ''.join(traceback.format_exception(exctype, value, tb))
        dialog = ErrorReportDialog(None, s)
        dialog.exec_()

    sys.excepthook = my_excepthook

Call install_exception() when your application starts.

ErrorReportDialog is a Qt dialog I've made. traceback.format_exception() will format argument passed to the except hook in the same way it does in Python's interpreter.

EDIT: I forgot to mention a little gotcha with that. It doesn't work with threads (well, at least it didn't last time I checked). For code running in another thread, you will need to wrap it in a try..except block.




回答2:


Stick try excepts everywhere your application can crash (I/O, networking etc.). Whenever an except is called, call a function that will kill the old window, spawn a new tkinter notification window, or a custom one with your error message.

Do a root.after to the new window and send your error report (urllib).

Put a restart button if you wish.

There is no crash reporting framework - as tkinter is not that type of GUI. It's pretty much a wrapper for simple command line apps.

Go pyqt/gtk or wxpython if you want the features seen in the screen-shot above. But I'm pretty sure that where ever you go, you'll have to write your own reporter.



来源:https://stackoverflow.com/questions/1964336/crash-reporting-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!