##// END OF EJS Templates
protect IPython from bad custom exception handlers...
MinRK -
Show More
@@ -64,7 +64,7 b' def BdbQuit_excepthook(et,ev,tb):'
64 else:
64 else:
65 BdbQuit_excepthook.excepthook_ori(et,ev,tb)
65 BdbQuit_excepthook.excepthook_ori(et,ev,tb)
66
66
67 def BdbQuit_IPython_excepthook(self,et,ev,tb):
67 def BdbQuit_IPython_excepthook(self,et,ev,tb,tb_offset=None):
68 print 'Exiting Debugger.'
68 print 'Exiting Debugger.'
69
69
70
70
@@ -1478,16 +1478,30 b' class InteractiveShell(SingletonConfigurable, Magic):'
1478 assert type(exc_tuple)==type(()) , \
1478 assert type(exc_tuple)==type(()) , \
1479 "The custom exceptions must be given AS A TUPLE."
1479 "The custom exceptions must be given AS A TUPLE."
1480
1480
1481 def dummy_handler(self,etype,value,tb):
1481 def dummy_handler(self,etype,value,tb,tb_offset=None):
1482 print '*** Simple custom exception handler ***'
1482 print '*** Simple custom exception handler ***'
1483 print 'Exception type :',etype
1483 print 'Exception type :',etype
1484 print 'Exception value:',value
1484 print 'Exception value:',value
1485 print 'Traceback :',tb
1485 print 'Traceback :',tb
1486 #print 'Source code :','\n'.join(self.buffer)
1486 #print 'Source code :','\n'.join(self.buffer)
1487
1487
1488 if handler is None: handler = dummy_handler
1488 if handler is None:
1489
1489 wrapped = dummy_handler
1490 self.CustomTB = types.MethodType(handler,self)
1490 else:
1491 def wrapped(self,etype,value,tb,tb_offset=None):
1492 try:
1493 return handler(self,etype,value,tb,tb_offset=tb_offset)
1494 except:
1495 # clear custom handler immediately
1496 self.set_custom_exc((), None)
1497 print >> io.stderr, "Custom TB Handler failed, unregistering"
1498 # show the exception in handler first
1499 stb = self.InteractiveTB.structured_traceback(*sys.exc_info())
1500 print >> io.stdout, self.InteractiveTB.stb2text(stb)
1501 print >> io.stdout, "The original exception:"
1502 self.showtraceback((etype,value,tb), tb_offset=tb_offset)
1503
1504 self.CustomTB = types.MethodType(wrapped,self)
1491 self.custom_exceptions = exc_tuple
1505 self.custom_exceptions = exc_tuple
1492
1506
1493 def excepthook(self, etype, value, tb):
1507 def excepthook(self, etype, value, tb):
@@ -146,3 +146,21 b' class InteractiveShellTestCase(unittest.TestCase):'
146 finally:
146 finally:
147 # Reset compiler flags so we don't mess up other tests.
147 # Reset compiler flags so we don't mess up other tests.
148 ip.compile.reset_compiler_flags()
148 ip.compile.reset_compiler_flags()
149
150 def test_bad_custom_tb(self):
151 """Check that InteractiveShell is protected from bad custom exception handlers"""
152 ip = get_ipython()
153 from IPython.utils import io
154 save_stderr = io.stderr
155 try:
156 # capture stderr
157 io.stderr = StringIO()
158 ip.set_custom_exc((IOError,),lambda etype,value,tb: None)
159 self.assertEquals(ip.custom_exceptions, (IOError,))
160 ip.run_cell(u'raise IOError("foo")')
161 self.assertEquals(ip.custom_exceptions, ())
162 self.assertTrue("Custom TB Handler failed" in io.stderr.getvalue())
163 finally:
164 io.stderr = save_stderr
165
166
General Comments 0
You need to be logged in to leave comments. Login now