A traceback printer which can be called on the fly. It will find out about exceptions by itself. A brief example:: AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') try: ... except: AutoTB() # or AutoTB(out=logfile) where logfile
| 1386 | |
| 1387 | #---------------------------------------------------------------------------- |
| 1388 | class AutoFormattedTB(FormattedTB): |
| 1389 | """A traceback printer which can be called on the fly. |
| 1390 | |
| 1391 | It will find out about exceptions by itself. |
| 1392 | |
| 1393 | A brief example:: |
| 1394 | |
| 1395 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') |
| 1396 | try: |
| 1397 | ... |
| 1398 | except: |
| 1399 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object |
| 1400 | """ |
| 1401 | |
| 1402 | def __call__(self, etype=None, evalue=None, etb=None, |
| 1403 | out=None, tb_offset=None): |
| 1404 | """Print out a formatted exception traceback. |
| 1405 | |
| 1406 | Optional arguments: |
| 1407 | - out: an open file-like object to direct output to. |
| 1408 | |
| 1409 | - tb_offset: the number of frames to skip over in the stack, on a |
| 1410 | per-call basis (this overrides temporarily the instance's tb_offset |
| 1411 | given at initialization time. """ |
| 1412 | |
| 1413 | if out is None: |
| 1414 | out = self.ostream |
| 1415 | out.flush() |
| 1416 | out.write(self.text(etype, evalue, etb, tb_offset)) |
| 1417 | out.write('\n') |
| 1418 | out.flush() |
| 1419 | # FIXME: we should remove the auto pdb behavior from here and leave |
| 1420 | # that to the clients. |
| 1421 | try: |
| 1422 | self.debugger() |
| 1423 | except KeyboardInterrupt: |
| 1424 | print("\nKeyboardInterrupt") |
| 1425 | |
| 1426 | def structured_traceback(self, etype=None, value=None, tb=None, |
| 1427 | tb_offset=None, number_of_lines_of_context=5): |
| 1428 | if etype is None: |
| 1429 | etype, value, tb = sys.exc_info() |
| 1430 | if isinstance(tb, tuple): |
| 1431 | # tb is a tuple if this is a chained exception. |
| 1432 | self.tb = tb[0] |
| 1433 | else: |
| 1434 | self.tb = tb |
| 1435 | return FormattedTB.structured_traceback( |
| 1436 | self, etype, value, tb, tb_offset, number_of_lines_of_context) |
| 1437 | |
| 1438 | |
| 1439 | #--------------------------------------------------------------------------- |