root/branches/gajim_0.11/src/gtkexcepthook.py

Revision 7116, 3.1 kB (checked in by nk, 23 months ago)

at last fix the damn thing so I can see my TraceBacks?!

Line 
1##      gtkexcepthook.py
2##
3## Copyright (C) 2005-2006 Yann Le Boulanger <asterix@lagaule.org>
4## Copyright (C) 2005-2006 Nikos Kouremenos <kourem@gmail.com>
5##
6## Initially written and submitted by Gustavo J. A. M. Carneiro
7##
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published
10## by the Free Software Foundation; version 2 only.
11##
12## This program is distributed in the hope that it will be useful,
13## but WITHOUT ANY WARRANTY; without even the implied warranty of
14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15## GNU General Public License for more details.
16##
17
18import sys
19import os
20import traceback
21import threading
22
23import gtk
24import pango
25from common import i18n
26import dialogs
27
28from cStringIO import StringIO
29from common import helpers
30
31_exception_in_progress = threading.Lock()
32
33def _info(type, value, tb):
34        if not _exception_in_progress.acquire(False):
35                # Exceptions have piled up, so we use the default exception
36                # handler for such exceptions
37                _excepthook_save(type, value, tb)
38                return
39
40        dialog = dialogs.HigDialog(None, gtk.MESSAGE_WARNING, gtk.BUTTONS_NONE,
41                                _('A programming error has been detected'),
42                                _('It probably is not fatal, but should be reported '
43                                'to the developers nonetheless.'))
44       
45        #FIXME: add icon to this button
46        RESPONSE_REPORT_BUG = 42
47        dialog.add_buttons(gtk.STOCK_CLOSE, gtk.BUTTONS_CLOSE,
48                _('_Report Bug'), RESPONSE_REPORT_BUG)
49        dialog.set_default_response(RESPONSE_REPORT_BUG)
50        report_button = dialog.action_area.get_children()[0] # right to left
51        report_button.grab_focus()
52
53        # Details
54        textview = gtk.TextView()
55        textview.set_editable(False)
56        textview.modify_font(pango.FontDescription('Monospace'))
57        sw = gtk.ScrolledWindow()
58        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
59        sw.add(textview)
60        frame = gtk.Frame()
61        frame.set_shadow_type(gtk.SHADOW_IN)
62        frame.add(sw)
63        frame.set_border_width(6)
64        textbuffer = textview.get_buffer()
65        trace = StringIO()
66        traceback.print_exception(type, value, tb, None, trace)
67        textbuffer.set_text(trace.getvalue())
68        textview.set_size_request(
69                gtk.gdk.screen_width() / 3,
70                gtk.gdk.screen_height() / 4)
71        expander = gtk.Expander(_('Details'))
72        expander.add(frame)
73        dialog.vbox.add(expander)
74
75        dialog.set_resizable(True)
76        # on expand the details the dialog remains centered on screen
77        dialog.set_position(gtk.WIN_POS_CENTER_ALWAYS)
78
79        dialog.show_all()
80
81        close_clicked = False
82        while not close_clicked:
83                resp = dialog.run()
84                if resp == RESPONSE_REPORT_BUG:
85                        url = 'http://trac.gajim.org/wiki/WikiStart#howto_report_ticket'
86                        helpers.launch_browser_mailer('url', url)
87                else:
88                        close_clicked = True
89       
90        dialog.destroy()
91
92        _exception_in_progress.release()
93       
94# gdb/kdm etc if we use startx this is not True
95if os.name == 'nt' or not sys.stderr.isatty():
96        #FIXME: maybe always show dialog?
97        _excepthook_save = sys.excepthook
98        sys.excepthook = _info
99
100# this is just to assist testing (python gtkexcepthook.py)
101if __name__ == '__main__':
102        _excepthook_save = sys.excepthook
103        sys.excepthook = _info
104        print x # this always tracebacks
Note: See TracBrowser for help on using the browser.