Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash reporting in Python

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:

alt text

like image 832
Sridhar Ratnakumar Avatar asked Dec 26 '09 20:12

Sridhar Ratnakumar


2 Answers

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.

like image 106
Virgil Dupras Avatar answered Oct 20 '22 04:10

Virgil Dupras


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.

like image 29
torger Avatar answered Oct 20 '22 05:10

torger