##// END OF EJS Templates
Merge pull request #2223 from takluyver/custom_traceback...
Thomas Kluyver -
r8088:06c27d89 merge
parent child Browse files
Show More
@@ -0,0 +1,44 b''
1 .. _integrating:
2
3 =====================================
4 Integrating your objects with IPython
5 =====================================
6
7 Tab completion
8 ==============
9
10 To change the attributes displayed by tab-completing your object, define a
11 ``__dir__(self)`` method for it. For more details, see the documentation of the
12 built-in `dir() function <http://docs.python.org/library/functions.html#dir>`_.
13
14 Rich display
15 ============
16
17 The notebook and the Qt console can display richer representations of objects.
18 To use this, you can define any of a number of ``_repr_*_()`` methods. Note that
19 these are surrounded by single, not double underscores.
20
21 Both the notebook and the Qt console can display ``svg``, ``png`` and ``jpeg``
22 representations. The notebook can also display ``html``, ``javascript``,
23 and ``latex``. If the methods don't exist, or return ``None``, it falls
24 back to a standard ``repr()``.
25
26 For example::
27
28 class Shout(object):
29 def __init__(self, text):
30 self.text = text
31
32 def _repr_html_(self):
33 return "<h1>" + self.text + "</h1>"
34
35 Custom exception tracebacks
36 ===========================
37
38 Rarely, you might want to display a different traceback with an exception -
39 IPython's own parallel computing framework does this to display errors from the
40 engines. To do this, define a ``_render_traceback_(self)`` method which returns
41 a list of strings, each containing one line of the traceback.
42
43 Please be conservative in using this feature; by replacing the default traceback
44 you may hide important information from the user.
@@ -186,6 +186,8 b' def display_latex(*objs, **kwargs):'
186 186
187 187 def display_json(*objs, **kwargs):
188 188 """Display the JSON representation of an object.
189
190 Note that not many frontends support displaying JSON.
189 191
190 192 Parameters
191 193 ----------
@@ -86,22 +86,6 b' from IPython.utils.traitlets import (Integer, CBool, CaselessStrEnum, Enum,'
86 86 from IPython.utils.warn import warn, error
87 87 import IPython.core.hooks
88 88
89 # FIXME: do this in a function to avoid circular dependencies
90 # A better solution is to remove IPython.parallel.error,
91 # and place those classes in IPython.core.error.
92
93 class RemoteError(Exception):
94 pass
95
96 def _import_remote_error():
97 global RemoteError
98 try:
99 from IPython.parallel.error import RemoteError
100 except:
101 pass
102
103 _import_remote_error()
104
105 89 #-----------------------------------------------------------------------------
106 90 # Globals
107 91 #-----------------------------------------------------------------------------
@@ -1735,10 +1719,6 b' class InteractiveShell(SingletonConfigurable):'
1735 1719 self.showsyntaxerror(filename)
1736 1720 elif etype is UsageError:
1737 1721 self.write_err("UsageError: %s" % value)
1738 elif issubclass(etype, RemoteError):
1739 # IPython.parallel remote exceptions.
1740 # Draw the remote traceback, not the local one.
1741 self._showtraceback(etype, value, value.render_traceback())
1742 1722 else:
1743 1723 if exception_only:
1744 1724 stb = ['An exception has occurred, use %tb to see '
@@ -1746,7 +1726,13 b' class InteractiveShell(SingletonConfigurable):'
1746 1726 stb.extend(self.InteractiveTB.get_exception_only(etype,
1747 1727 value))
1748 1728 else:
1749 stb = self.InteractiveTB.structured_traceback(etype,
1729 try:
1730 # Exception classes can customise their traceback - we
1731 # use this in IPython.parallel for exceptions occurring
1732 # in the engines. This should return a list of strings.
1733 stb = value._render_traceback_()
1734 except Exception:
1735 stb = self.InteractiveTB.structured_traceback(etype,
1750 1736 value, tb, tb_offset=tb_offset)
1751 1737
1752 1738 self._showtraceback(etype, value, stb)
@@ -33,6 +33,7 b' import nose.tools as nt'
33 33
34 34 # Our own
35 35 from IPython.testing.decorators import skipif
36 from IPython.testing import tools as tt
36 37 from IPython.utils import io
37 38
38 39 #-----------------------------------------------------------------------------
@@ -401,6 +402,18 b' class TestSystemRaw(unittest.TestCase):'
401 402 cmd = ur'''python -c "'åäö'" '''
402 403 ip.system_raw(cmd)
403 404
405 class TestModules(unittest.TestCase, tt.TempFileMixin):
406 def test_extraneous_loads(self):
407 """Test we're not loading modules on startup that we shouldn't.
408 """
409 self.mktmp("import sys\n"
410 "print('numpy' in sys.modules)\n"
411 "print('IPython.parallel' in sys.modules)\n"
412 "print('IPython.zmq' in sys.modules)\n"
413 )
414 out = "False\nFalse\nFalse\n"
415 tt.ipexec_validate(self.fname, out)
416
404 417
405 418 def test__IPYTHON__():
406 419 # This shouldn't raise a NameError, that's all
@@ -193,6 +193,9 b' class RemoteError(KernelError):'
193 193 def render_traceback(self):
194 194 """render traceback to a list of lines"""
195 195 return (self.traceback or "No traceback available").splitlines()
196
197 # Special method for custom tracebacks within IPython
198 _render_traceback_ = render_traceback
196 199
197 200 def print_traceback(self, excid=None):
198 201 """print my traceback"""
@@ -10,5 +10,6 b' Configuration and customization'
10 10 overview.txt
11 11 extensions/index.txt
12 12 ipython.txt
13 integrating.txt
13 14 editors.txt
14 15 old.txt
@@ -6,4 +6,8 b' This document describes in-flight development work.'
6 6
7 7 The CodeMirror js library has been updated fron 2.23 to 2.32
8 8 this might induce a few changes in behavior of keymaps in the notebook,
9 especially intenting/deindenting blocks that is now bounded to Ctrl+] and ctr+[
9 especially intenting/deindenting blocks that is now bound to Ctrl+] and ctr+[
10
11 * Exception types can now be displayed with a custom traceback, by defining a
12 ``_render_traceback_()`` method which returns a list of strings, each
13 containing one line of the traceback.
General Comments 0
You need to be logged in to leave comments. Login now