From 6c86c46427267c1598777af2771b74bc679df093 2012-07-30 20:10:53 From: Thomas Kluyver Date: 2012-07-30 20:10:53 Subject: [PATCH] Allow custom tracebacks for exception types with a _render_traceback_() method. --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 60dfc5c..4a24fe3 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -86,22 +86,6 @@ from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum, from IPython.utils.warn import warn, error import IPython.core.hooks -# FIXME: do this in a function to avoid circular dependencies -# A better solution is to remove IPython.parallel.error, -# and place those classes in IPython.core.error. - -class RemoteError(Exception): - pass - -def _import_remote_error(): - global RemoteError - try: - from IPython.parallel.error import RemoteError - except: - pass - -_import_remote_error() - #----------------------------------------------------------------------------- # Globals #----------------------------------------------------------------------------- @@ -1735,10 +1719,6 @@ class InteractiveShell(SingletonConfigurable): self.showsyntaxerror(filename) elif etype is UsageError: self.write_err("UsageError: %s" % value) - elif issubclass(etype, RemoteError): - # IPython.parallel remote exceptions. - # Draw the remote traceback, not the local one. - self._showtraceback(etype, value, value.render_traceback()) else: if exception_only: stb = ['An exception has occurred, use %tb to see ' @@ -1746,7 +1726,13 @@ class InteractiveShell(SingletonConfigurable): stb.extend(self.InteractiveTB.get_exception_only(etype, value)) else: - stb = self.InteractiveTB.structured_traceback(etype, + try: + # Exception classes can customise their traceback - we + # use this in IPython.parallel for exceptions occurring + # in the engines. This should return a list of strings. + stb = value._render_traceback_() + except Exception: + stb = self.InteractiveTB.structured_traceback(etype, value, tb, tb_offset=tb_offset) self._showtraceback(etype, value, stb) diff --git a/IPython/parallel/error.py b/IPython/parallel/error.py index 910c422..6e2d078 100644 --- a/IPython/parallel/error.py +++ b/IPython/parallel/error.py @@ -190,9 +190,12 @@ class RemoteError(KernelError): def __str__(self): return "%s(%s)" % (self.ename, self.evalue) - def render_traceback(self): + def _render_traceback_(self): """render traceback to a list of lines""" return (self.traceback or "No traceback available").splitlines() + + # Previous name retained for backwards compatibility. + render_traceback = _render_traceback_ def print_traceback(self, excid=None): """print my traceback"""