def _hook(type_, value, tback):
"""Exception hook callback."""
if hasattr(sys, 'ps1') or not sys.stderr.isatty():
# we are in interactive mode or we don't have a tty-like
# device, so we call the default hook
sys.__excepthook__(type_, value, tback)
else:
import traceback
import pdb
# we are NOT in interactive mode, print the exception...
traceback.print_exception(type_, value, tback)
# Dirty hack because Py27 doesn't chain exceptions
if value.args:
tb2 = value.args[-1]
if isinstance(tb2, type(tback)):
ex = value.args[-2]
print >>sys.stderr, '{}Caused by{} '.format(
ansi('1;35m'), ansi('0m')),
traceback.print_exception(type_(ex), ex, tb2)
print
# ...then start the debugger in post-mortem mode.
# pdb.pm() # deprecated
pdb.post_mortem(tback) # more "modern"
python类pm()的实例源码
def pm():
post_mortem(sys.last_traceback)
# Main program for testing
def debug_hook(type_, value, tb):
if hasattr(sys, 'ps1') or not sys.stderr.isatty():
sys.__excepthook__(type_, value, tb)
else:
import traceback
import pdb
traceback.print_exception(type_, value, tb)
print(u"\n")
pdb.pm()
def _DebugHandler(exc_class, value, tb):
if not flags.FLAGS.pdb or hasattr(sys, 'ps1') or not sys.stderr.isatty():
# we aren't in interactive mode or we don't have a tty-like
# device, so we call the default hook
old_excepthook(exc_class, value, tb)
else:
# Don't impose import overhead on apps that never raise an exception.
import traceback
import pdb
# we are in interactive mode, print the exception...
traceback.print_exception(exc_class, value, tb)
sys.stdout.write('\n')
# ...then start the debugger in post-mortem mode.
pdb.pm()
def _DebugHandler(exc_class, value, tb):
if not flags.FLAGS.pdb or hasattr(sys, 'ps1') or not sys.stderr.isatty():
# we aren't in interactive mode or we don't have a tty-like
# device, so we call the default hook
old_excepthook(exc_class, value, tb)
else:
# Don't impose import overhead on apps that never raise an exception.
import traceback
import pdb
# we are in interactive mode, print the exception...
traceback.print_exception(exc_class, value, tb)
sys.stdout.write('\n')
# ...then start the debugger in post-mortem mode.
pdb.pm()
def pm():
post_mortem(sys.last_traceback)
# Main program for testing
def info(type, value, tb):
if hasattr(sys, 'ps1') or not sys.stderr.isatty() or type != AssertionError:
# we are in interactive mode or we don't have a tty-like
# device, so we call the default hook
sys.__excepthook__(type, value, tb)
else:
import traceback, pdb
# we are NOT in interactive mode, print the exception...
traceback.print_exception(type, value, tb)
print
# ...then start the debugger in post-mortem mode.
pdb.pm()
def pm():
post_mortem(sys.last_traceback)
# Main program for testing
def debug_hook(type_, value, tb):
# http://stackoverflow.com/a/1237407/690430
if hasattr(sys, 'ps1') or not sys.stderr.isatty():
sys.__excepthook__(type_, value, tb)
else:
import traceback
import pdb
traceback.print_exception(type_, value, tb)
print(u"\n")
pdb.pm()
def pm():
post_mortem(sys.last_traceback)
# Main program for testing
def cli(post_mortem):
if post_mortem:
import traceback
try:
import ipdb as pdb
except ImportError:
import pdb
def _excepthook(exc_type, value, tb):
traceback.print_exception(exc_type, value, tb)
print()
pdb.pm()
sys.excepthook = _excepthook
def run(command, conf=None, tutorial=False, pdb=False, **args):
"""Main entry point for a direct call from Python
Example usage:
>>> from yam import run
>>> run(conf='conf.json')
:param command: if ``'create'`` the example configuration is created,
optionally the tutorial data files are downloaded
For all other commands this function loads the configuration
and construct the arguments which are passed to `run2()`
All args correspond to the respective command line and
configuration options.
See the example configuration file for help and possible arguments.
Options in args can overwrite the configuration from the file.
E.g. ``run(conf='conf.json', bla='bla')`` will set bla configuration
value to ``'bla'``.
"""
if pdb:
import traceback, pdb
def info(type, value, tb):
traceback.print_exception(type, value, tb)
print
# ...then start the debugger in post-mortem mode.
pdb.pm()
sys.excepthook = info
if conf in ('None', 'none', 'null', ''):
conf = None
# Copy example files if create_config or tutorial
if command == 'create':
if conf is None:
conf = 'conf.json'
create_config(conf, tutorial=tutorial)
return
# Parse config file
if conf:
try:
with open(conf) as f:
conf = json.load(f, cls=ConfigJSONDecoder)
except ValueError as ex:
msg = 'Error while parsing the configuration: %s' % ex
raise ConfigError(msg)
except IOError as ex:
raise ConfigError(ex)
# Populate args with conf, but prefer args
conf.update(args)
args = conf
run2(command, **args)