diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index bf99c91..361b7b2 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -1703,23 +1703,20 @@ class InteractiveShell(SingletonConfigurable): elif etype is UsageError: self.write_err("UsageError: %s" % value) else: - if etype in self.custom_exceptions: - stb = self.CustomTB(etype, value, tb, tb_offset) + 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: - 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, - value, tb, tb_offset=tb_offset) - - self._showtraceback(etype, value, stb) - if self.call_pdb: - # drop into debugger - self.debugger(force=True) - return + stb = self.InteractiveTB.structured_traceback(etype, + value, tb, tb_offset=tb_offset) + + self._showtraceback(etype, value, stb) + if self.call_pdb: + # drop into debugger + self.debugger(force=True) + return # Actually show the traceback self._showtraceback(etype, value, stb) diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index fa5bf1b..d978d5d 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -337,6 +337,21 @@ class InteractiveShellTestCase(unittest.TestCase): namespace = 'IPython internal', obj= cmagic.__wrapped__, parent = None) nt.assert_equal(find, info) + + def test_custom_exception(self): + called = [] + def my_handler(shell, etype, value, tb, tb_offset=None): + called.append(etype) + shell.showtraceback((etype, value, tb), tb_offset=tb_offset) + + ip.set_custom_exc((ValueError,), my_handler) + try: + ip.run_cell("raise ValueError('test')") + # Check that this was called, and only once. + self.assertEqual(called, [ValueError]) + finally: + # Reset the custom exception hook + ip.set_custom_exc((), None) class TestSafeExecfileNonAsciiPath(unittest.TestCase):