def exc_info_hook(exc_type, value, tb):
"""An exception hook that starts IPdb automatically on error if in interactive mode."""
if hasattr(sys, 'ps1') or not sys.stderr.isatty() or exc_type == KeyboardInterrupt:
# we are in interactive mode, we don't have a tty-like
# device,, or the user triggered a KeyboardInterrupt,
# so we call the default hook
sys.__excepthook__(exc_type, value, tb)
else:
import traceback
import ipdb
# we are NOT in interactive mode, print the exception
traceback.print_exception(exc_type, value, tb)
# then start the debugger in post-mortem mode.
# pdb.pm() # deprecated
ipdb.post_mortem(tb) # more modern
python类post_mortem()的实例源码
def _main():
"""Entry point"""
try:
args, _, config = parseCommandLine()
eyed3.utils.console.AnsiCodes.init(not args.no_color)
mainFunc = main if args.debug_profile is False else profileMain
retval = mainFunc(args, config)
except KeyboardInterrupt:
retval = 0
except (StopIteration, IOError) as ex:
eyed3.utils.console.printError(UnicodeType(ex))
retval = 1
except Exception as ex:
eyed3.utils.console.printError("Uncaught exception: %s\n" % str(ex))
eyed3.log.exception(ex)
retval = 1
if args.debug_pdb:
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore", PendingDeprecationWarning)
# Must delay the import of ipdb as say as possible because
# of https://github.com/gotcha/ipdb/issues/48
import ipdb as pdb
except ImportError:
import pdb
e, m, tb = sys.exc_info()
pdb.post_mortem(tb)
sys.exit(retval)
def _debug_on_error(context, step):
if context.config.userdata.getbool("debug"):
try:
import ipdb
ipdb.post_mortem(step.exc_traceback)
except ImportError:
import pdb
pdb.post_mortem(step.exc_traceback)
def after_step(context, step):
# from https://github.com/behave/behave/blob/master/docs/tutorial.rst#
# debug-on-error-in-case-of-step-failures
# and https://stackoverflow.com/a/22344473/399726
if BEHAVE_DEBUG_ON_ERROR and step.status == "failed":
# -- ENTER DEBUGGER: Zoom in on failure location.
# NOTE: Use IPython debugger, same for pdb (basic python debugger).
import ipdb
ipdb.post_mortem(step.exc_traceback)
def debugger():
"""If called in the context of an exception, calls post_mortem; otherwise
set_trace.
``ipdb`` is preferred over ``pdb`` if installed.
"""
e, m, tb = sys.exc_info()
if tb is not None:
_debugger.post_mortem(tb)
else:
_debugger.set_trace()
def after_step(context, step):
if step.status == "failed":
# -- SOLUTION: But note that step.exc_info does not exist, yet.
import ipdb
ipdb.post_mortem(step.exc_traceback)
def handle_exception(self, e):
print("\nException:", e)
traceback.print_exc(file=sys.stdout)
if inquirer is None:
print('Please "pip install inquirer" if you would like more debug options')
sys.exit(0)
else:
q = inquirer.List('goto',
message='Now what?',
choices=['Restart browser', 'Debug with ipdb', 'Debug with pdb', 'Exit'],
)
answers = inquirer.prompt([q])
if answers['goto'] == 'Debug with ipdb':
try:
import ipdb
except ImportError:
print('Please run "pip install ipdb" to install ipdb')
sys.exit(1)
ipdb.post_mortem()
sys.exit(0)
elif answers['goto'] == 'Debug with pdb':
import pdb
pdb.post_mortem()
sys.exit(0)
elif answers['goto'] == 'Restart browser':
self.browser.restart()
else:
sys.exit(0)