Skip to content

Commit

Permalink
Merge pull request #1742 from takluyver/customexc-check-once
Browse files Browse the repository at this point in the history
Check for custom_exceptions only once


We already catch custom exceptions in run_code and call the custom handler, but we were checking for them again in showtraceback. This just removes the second check, so custom exception handlers can safely call showtraceback().
  • Loading branch information
minrk committed May 31, 2012
2 parents b4c93db + 67f2d22 commit de0b095
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
29 changes: 13 additions & 16 deletions IPython/core/interactiveshell.py
Expand Up @@ -1703,23 +1703,20 @@ def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
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)
Expand Down
15 changes: 15 additions & 0 deletions IPython/core/tests/test_interactiveshell.py
Expand Up @@ -337,6 +337,21 @@ def cmagic(line, cell):
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):
Expand Down

0 comments on commit de0b095

Please sign in to comment.