Exemple #1
0
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")
Exemple #2
0
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")
Exemple #3
0
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())
Exemple #4
0
def on(*args, **kwargs):
    """Install the tracestack exception handler as the system exception 
    handler."""

    try:
        ipython_shell = get_ipython()
        handler = ExceptionHandler(*args, **kwargs)

        def _get_ipython_handler(*args, **kwargs):
            def handle_ipython(shell, etype, value, tb, tb_offset=None):
                shell.showtraceback((etype, value, tb), tb_offset=tb_offset)
                handler.handle_error(etype, value, tb)
                return traceback.format_tb(tb)

            return handle_ipython

        ipython_shell.set_custom_exc((Exception, ),
                                     _get_ipython_handler(*args, **kwargs))
    except NameError:
        sys.excepthook = ExceptionHandler(*args, **kwargs)
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()
Exemple #6
0
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()
Exemple #7
0
 def __init__(self, *args, **kwargs):
     self.handler = ExceptionHandler(*args, **kwargs)
     kwargs.pop("engine")
     kwargs.pop("prompt")
     kwargs.pop("arguments")
     InteractiveConsole.__init__(self, *args, **kwargs)