def pm(*args, **kwargs): """Post-mortem function that searches the last exception.""" if hasattr(sys, "last_type"): einfo = (sys.last_type, sys.last_value, sys.last_traceback) handler = ExceptionHandler(*args, **kwargs) try: ipython_shell = get_ipython() ipython_shell.showtraceback() handler.handle_error(*einfo) except NameError: handler(*einfo) else: raise ValueError("no last exception")
class TracestackConsole(InteractiveConsole): """An interactive console for running code or REPLs while handling errors using the tracestack handler.""" def __init__(self, *args, **kwargs): self.handler = ExceptionHandler(*args, **kwargs) kwargs.pop("engine") kwargs.pop("prompt") kwargs.pop("arguments") InteractiveConsole.__init__(self, *args, **kwargs) def showtraceback(self): InteractiveConsole.showtraceback(self) self.handler.handle_error(*sys.exc_info()) def showsyntaxerror(self, filename=None): InteractiveConsole.showsyntaxerror(self, filename) self.handler.handle_error(*sys.exc_info())
def run(): """Runs the script provided by the arguments, using the tracestack exception handler. """ parser = _build_parser() args = vars(parser.parse_args()) print(args) script = args.pop("script") handler = ExceptionHandler(**args) if script: # set up the system variables sys.argv = sys.argv[1:] sys.path[0] = os.path.dirname(os.path.abspath(script)) try: runpy.run_path(script, run_name="__main__") except: einfo = sys.exc_info() _print_clean_traceback(einfo) handler.handle_error(*einfo) else: # no additional arguments were given; run the REPL console = TracestackConsole(**args) console.interact()