crashhandler.py
180 lines
| 6.1 KiB
| text/x-python
|
PythonLexer
Brian Granger
|
r2506 | # encoding: utf-8 | ||
fperez
|
r0 | """sys.excepthook for IPython itself, leaves a detailed report on disk. | ||
Fernando Perez
|
r1875 | |||
Brian Granger
|
r2506 | Authors: | ||
Fernando Perez
|
r1875 | |||
Brian Granger
|
r2506 | * Fernando Perez | ||
* Brian E. Granger | ||||
Fernando Perez
|
r1853 | """ | ||
fperez
|
r0 | |||
Brian Granger
|
r2506 | #----------------------------------------------------------------------------- | ||
# Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> | ||||
# Copyright (C) 2008-2010 The IPython Development Team | ||||
fperez
|
r0 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
Brian Granger
|
r2506 | #----------------------------------------------------------------------------- | ||
fperez
|
r0 | |||
Brian Granger
|
r2506 | #----------------------------------------------------------------------------- | ||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
fperez
|
r0 | |||
fperez
|
r52 | import os | ||
import sys | ||||
Brian Granger
|
r2010 | from pprint import pformat | ||
fperez
|
r0 | |||
Brian Granger
|
r2048 | from IPython.core import ultratb | ||
Brian Granger
|
r2498 | from IPython.utils.sysinfo import sys_info | ||
Fernando Perez
|
r2495 | |||
Brian Granger
|
r2506 | #----------------------------------------------------------------------------- | ||
# Code | ||||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r1875 | |||
Brian Granger
|
r2506 | # Template for the user message. | ||
_default_message_template = """\ | ||||
Thomas Kluyver
|
r4049 | Oops, {app_name} crashed. We do our best to make it stable, but... | ||
Fernando Perez
|
r2495 | |||
Brian Granger
|
r2506 | A crash report was automatically generated with the following information: | ||
- A verbatim copy of the crash traceback. | ||||
- A copy of your input history during this session. | ||||
- Data on your current $self.app_name configuration. | ||||
fperez
|
r0 | |||
Brian Granger
|
r2506 | It was left in the file named: | ||
Thomas Kluyver
|
r4049 | \t'{crash_report_fname}' | ||
Brian Granger
|
r2506 | If you can email this file to the developers, the information in it will help | ||
them in understanding and correcting the problem. | ||||
fptest
|
r381 | |||
Thomas Kluyver
|
r4049 | You can mail it to: $self.contact_name at {contact_email} | ||
with the subject '{app_name} Crash Report'. | ||||
fptest
|
r381 | |||
Brian Granger
|
r2506 | If you want to do it now, the following command will work (under Unix): | ||
Thomas Kluyver
|
r4049 | mail -s '{app_name} Crash Report' {contact_email} < {crash_report_fname} | ||
Brian Granger
|
r2506 | |||
To ensure accurate tracking of this issue, please file a report about it at: | ||||
Thomas Kluyver
|
r4049 | {bug_tracker} | ||
Brian Granger
|
r2506 | """ | ||
fptest
|
r381 | |||
Brian Granger
|
r2506 | class CrashHandler(object): | ||
"""Customizable crash handlers for IPython applications. | ||||
fptest
|
r381 | |||
Brian Granger
|
r2506 | Instances of this class provide a :meth:`__call__` method which can be | ||
used as a ``sys.excepthook``. The :meth:`__call__` signature is:: | ||||
fptest
|
r381 | |||
Brian Granger
|
r2506 | def __call__(self, etype, evalue, etb) | ||
""" | ||||
fptest
|
r381 | |||
Brian Granger
|
r2506 | message_template = _default_message_template | ||
Thomas Kluyver
|
r4049 | section_sep = '\n\n'+'*'*75+'\n\n' | ||
fptest
|
r381 | |||
Brian Granger
|
r2506 | def __init__(self, app, contact_name=None, contact_email=None, | ||
bug_tracker=None, show_crash_traceback=True, call_pdb=False): | ||||
"""Create a new crash handler | ||||
fptest
|
r381 | |||
Brian Granger
|
r2506 | Parameters | ||
---------- | ||||
app : Application | ||||
A running :class:`Application` instance, which will be queried at | ||||
crash time for internal information. | ||||
fptest
|
r381 | |||
Brian Granger
|
r2506 | contact_name : str | ||
A string with the name of the person to contact. | ||||
fptest
|
r381 | |||
Brian Granger
|
r2506 | contact_email : str | ||
A string with the email address of the contact. | ||||
fptest
|
r381 | |||
Brian Granger
|
r2506 | bug_tracker : str | ||
A string with the URL for your project's bug tracker. | ||||
show_crash_traceback : bool | ||||
If false, don't print the crash traceback on stderr, only generate | ||||
the on-disk report | ||||
fptest
|
r381 | |||
Non-argument instance attributes: | ||||
Brian Granger
|
r2506 | These instances contain some non-argument attributes which allow for | ||
further customization of the crash handler's behavior. Please see the | ||||
fptest
|
r381 | source for further details. | ||
""" | ||||
Thomas Kluyver
|
r4049 | self.crash_report_fname = "Crash_report_%s.txt" % app.name | ||
Fernando Perez
|
r2403 | self.app = app | ||
Fernando Perez
|
r2437 | self.call_pdb = call_pdb | ||
#self.call_pdb = True # dbg | ||||
Thomas Kluyver
|
r4049 | self.show_crash_traceback = show_crash_traceback | ||
self.info = dict(app_name = app.name, | ||||
contact_name = contact_name, | ||||
contact_email = contact_email, | ||||
bug_tracker = bug_tracker, | ||||
crash_report_fname = self.crash_report_fname) | ||||
fperez
|
r0 | |||
Brian Granger
|
r2506 | def __call__(self, etype, evalue, etb): | ||
fptest
|
r381 | """Handle an exception, call for compatible with sys.excepthook""" | ||
fperez
|
r0 | |||
# Report tracebacks shouldn't use color in general (safer for users) | ||||
color_scheme = 'NoColor' | ||||
# Use this ONLY for developer debugging (keep commented out for release) | ||||
#color_scheme = 'Linux' # dbg | ||||
try: | ||||
Fernando Perez
|
r2403 | rptdir = self.app.ipython_dir | ||
fperez
|
r0 | except: | ||
Jörgen Stenarson
|
r4208 | rptdir = os.getcwdu() | ||
Fernando Perez
|
r2646 | if rptdir is None or not os.path.isdir(rptdir): | ||
Jörgen Stenarson
|
r4208 | rptdir = os.getcwdu() | ||
fptest
|
r381 | report_name = os.path.join(rptdir,self.crash_report_fname) | ||
# write the report filename into the instance dict so it can get | ||||
# properly expanded out in the user message template | ||||
self.crash_report_fname = report_name | ||||
Brian Granger
|
r2506 | TBhandler = ultratb.VerboseTB( | ||
color_scheme=color_scheme, | ||||
long_header=1, | ||||
call_pdb=self.call_pdb, | ||||
) | ||||
Fernando Perez
|
r2437 | if self.call_pdb: | ||
TBhandler(etype,evalue,etb) | ||||
return | ||||
else: | ||||
traceback = TBhandler.text(etype,evalue,etb,context=31) | ||||
fperez
|
r0 | |||
# print traceback to screen | ||||
fptest
|
r381 | if self.show_crash_traceback: | ||
print >> sys.stderr, traceback | ||||
fperez
|
r0 | |||
# and generate a complete report on disk | ||||
try: | ||||
fptest
|
r381 | report = open(report_name,'w') | ||
fperez
|
r0 | except: | ||
print >> sys.stderr, 'Could not create crash report on disk.' | ||||
return | ||||
fptest
|
r381 | # Inform user on stderr of what happened | ||
Thomas Kluyver
|
r4049 | print >> sys.stderr, '\n'+'*'*70+'\n' | ||
print >> sys.stderr, self.message_template.format(**self.info) | ||||
fperez
|
r0 | |||
fptest
|
r381 | # Construct report on disk | ||
report.write(self.make_report(traceback)) | ||||
report.close() | ||||
Fernando Perez
|
r2402 | raw_input("Hit <Enter> to quit this message (your terminal may close):") | ||
fperez
|
r0 | |||
fptest
|
r381 | def make_report(self,traceback): | ||
"""Return a string containing a crash report.""" | ||||
Fernando Perez
|
r2403 | |||
sec_sep = self.section_sep | ||||
Fernando Perez
|
r2495 | report = ['*'*75+'\n\n'+'IPython post-mortem report\n\n'] | ||
fptest
|
r381 | rpt_add = report.append | ||
Fernando Perez
|
r2495 | rpt_add(sys_info()) | ||
fptest
|
r381 | |||
try: | ||||
Fernando Perez
|
r2403 | config = pformat(self.app.config) | ||
Brian Granger
|
r2506 | rpt_add(sec_sep) | ||
rpt_add('Application name: %s\n\n' % self.app_name) | ||||
rpt_add('Current user configuration structure:\n\n') | ||||
Fernando Perez
|
r2403 | rpt_add(config) | ||
fptest
|
r381 | except: | ||
pass | ||||
Fernando Perez
|
r2403 | rpt_add(sec_sep+'Crash traceback:\n\n' + traceback) | ||
fperez
|
r0 | |||
fptest
|
r381 | return ''.join(report) | ||