Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions IPython/core/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ def display_latex(*objs, **kwargs):

def display_json(*objs, **kwargs):
"""Display the JSON representation of an object.

Note that not many frontends support displaying JSON.

Parameters
----------
Expand Down
28 changes: 7 additions & 21 deletions IPython/core/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,6 @@
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
#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -1735,18 +1719,20 @@ def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
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 '
'the full traceback.\n']
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)
Expand Down
13 changes: 13 additions & 0 deletions IPython/core/tests/test_interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

# Our own
from IPython.testing.decorators import skipif
from IPython.testing import tools as tt
from IPython.utils import io

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -401,6 +402,18 @@ def test_1(self):
cmd = ur'''python -c "'åäö'" '''
ip.system_raw(cmd)

class TestModules(unittest.TestCase, tt.TempFileMixin):
def test_extraneous_loads(self):
"""Test we're not loading modules on startup that we shouldn't.
"""
self.mktmp("import sys\n"
"print('numpy' in sys.modules)\n"
"print('IPython.parallel' in sys.modules)\n"
"print('IPython.zmq' in sys.modules)\n"
)
out = "False\nFalse\nFalse\n"
tt.ipexec_validate(self.fname, out)


def test__IPYTHON__():
# This shouldn't raise a NameError, that's all
Expand Down
3 changes: 3 additions & 0 deletions IPython/parallel/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ def __str__(self):
def render_traceback(self):
"""render traceback to a list of lines"""
return (self.traceback or "No traceback available").splitlines()

# Special method for custom tracebacks within IPython
_render_traceback_ = render_traceback

def print_traceback(self, excid=None):
"""print my traceback"""
Expand Down
1 change: 1 addition & 0 deletions docs/source/config/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ Configuration and customization
overview.txt
extensions/index.txt
ipython.txt
integrating.txt
editors.txt
old.txt
44 changes: 44 additions & 0 deletions docs/source/config/integrating.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.. _integrating:

=====================================
Integrating your objects with IPython
=====================================

Tab completion
==============

To change the attributes displayed by tab-completing your object, define a
``__dir__(self)`` method for it. For more details, see the documentation of the
built-in `dir() function <http://docs.python.org/library/functions.html#dir>`_.

Rich display
============

The notebook and the Qt console can display richer representations of objects.
To use this, you can define any of a number of ``_repr_*_()`` methods. Note that
these are surrounded by single, not double underscores.

Both the notebook and the Qt console can display ``svg``, ``png`` and ``jpeg``
representations. The notebook can also display ``html``, ``javascript``,
and ``latex``. If the methods don't exist, or return ``None``, it falls
back to a standard ``repr()``.

For example::

class Shout(object):
def __init__(self, text):
self.text = text

def _repr_html_(self):
return "<h1>" + self.text + "</h1>"

Custom exception tracebacks
===========================

Rarely, you might want to display a different traceback with an exception -
IPython's own parallel computing framework does this to display errors from the
engines. To do this, define a ``_render_traceback_(self)`` method which returns
a list of strings, each containing one line of the traceback.

Please be conservative in using this feature; by replacing the default traceback
you may hide important information from the user.
6 changes: 5 additions & 1 deletion docs/source/whatsnew/development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ This document describes in-flight development work.

The CodeMirror js library has been updated fron 2.23 to 2.32
this might induce a few changes in behavior of keymaps in the notebook,
especially intenting/deindenting blocks that is now bounded to Ctrl+] and ctr+[
especially intenting/deindenting blocks that is now bound to Ctrl+] and ctr+[

* Exception types can now be displayed with a custom traceback, by defining a
``_render_traceback_()`` method which returns a list of strings, each
containing one line of the traceback.