From: timv Date: Fri, 14 Jun 2013 14:42:00 +0000 (-0400) Subject: Crash handler. All uncatch exceptions are beautifully formatted and logged. X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=8019514aeba40fb624cb2e2c6c37eaac6ab01c9f;p=dyna2 Crash handler. All uncatch exceptions are beautifully formatted and logged. --- diff --git a/src/Dyna/Backend/Python/interpreter.py b/src/Dyna/Backend/Python/interpreter.py index da10aeb..fdd6b81 100644 --- a/src/Dyna/Backend/Python/interpreter.py +++ b/src/Dyna/Backend/Python/interpreter.py @@ -203,7 +203,7 @@ import debug from chart import Chart, Term, _repr from defn import aggregator from utils import ip, red, green, blue, magenta, yellow, \ - notimplemented, parse_attrs, ddict, dynac, \ + notimplemented, parse_attrs, ddict, dynac, enable_crash_handler, \ DynaCompilerError, DynaInitializerException from prioritydict import prioritydict from config import dotdynadir, dynahome @@ -641,6 +641,9 @@ def main(): interp = Interpreter() + enable_crash_handler() + + if args.profile: # When profiling, its common practice to disable the garbage collector. import gc diff --git a/src/Dyna/Backend/Python/repl.py b/src/Dyna/Backend/Python/repl.py index aac79fe..7758c8b 100644 --- a/src/Dyna/Backend/Python/repl.py +++ b/src/Dyna/Backend/Python/repl.py @@ -1,10 +1,9 @@ -import os -import cmd, readline -import interpreter -from utils import blue, yellow, green, magenta, ip, DynaCompilerError, DynaInitializerException +import os, cmd, readline + +import debug, interpreter +from utils import DynaCompilerError, DynaInitializerException, ip, crash_handler from chart import _repr from config import dotdynadir -import debug class REPL(cmd.Cmd, object): @@ -126,8 +125,11 @@ class REPL(cmd.Cmd, object): try: super(REPL, self).cmdloop() except KeyboardInterrupt: + # Catch Control-C and resume REPL. print '^C' - self.cmdloop() - except Exception as e: + self.cmdloop() + except: + # report uncatch exception. + crash_handler() + finally: readline.write_history_file(self.hist) - raise e diff --git a/src/Dyna/Backend/Python/utils.py b/src/Dyna/Backend/Python/utils.py index 4a35be7..4a78017 100644 --- a/src/Dyna/Backend/Python/utils.py +++ b/src/Dyna/Backend/Python/utils.py @@ -1,7 +1,8 @@ -import re +import re, sys from subprocess import Popen, PIPE from IPython.frontend.terminal.embed import InteractiveShellEmbed -from config import dynahome +from IPython.core.ultratb import VerboseTB +from config import dynahome, dotdynadir # interactive IPython shell @@ -45,6 +46,43 @@ def dynac(f, out): raise DynaCompilerError(stderr) +def crash_handler(): + exception_handler(*sys.exc_info()) + + +def exception_handler(etype, evalue, tb): + + # once for the log file. + with file(dotdynadir / 'crash.log', 'wb') as crashreport: + h = VerboseTB(color_scheme='Linux', call_pdb=False, + ostream=crashreport, + long_header=True, include_vars=True, + check_cache=None) + h(etype, evalue, tb) + + # once for the user + h = VerboseTB(color_scheme='Linux', call_pdb=False, ostream=None, + tb_offset=0, long_header=False, include_vars=True, + check_cache=None) + h(etype, evalue, tb) + + # TODO: we should package up all relevant state including compiler + # version, codegen output, interpreter state (possibly without the + # chart -- because it might be too big to email); input to repl. + # This should all go into a tarball. + + print 'FATAL ERROR (%s): %s' % (etype.__name__, evalue) + print 'Please report this error by emailing bugs@dyna.org. ' \ + 'Please attach the following file %s' % crashreport.name + + +def enable_crash_handler(): + """ + Use our custom exception handler for handling uncaught exceptions. + """ + sys.excepthook = exception_handler + + def notimplemented(*_,**__): raise NotImplementedError @@ -214,6 +252,6 @@ if __name__ == '__main__': btree = t(x) assert check_binary(btree) - print + print print 'sentence(%s) :=\n%s.' % (i, pretty(btree, 4).rstrip()) print