51
51
from bzrlib import registry
53
from bzrlib.hooks import Hooks
53
from bzrlib.hooks import HookPoint, Hooks
54
54
from bzrlib.option import Option
605
605
Hooks.__init__(self)
606
# Introduced in 1.13:
607
# invoked after creating a command object to allow modifications such
608
# as adding or removing options, docs etc. Invoked with the command
610
self['extend_command'] = []
606
self.create_hook(HookPoint('extend_command',
607
"Called after creating a command object to allow modifications "
608
"such as adding or removing options, docs etc. Called with the "
609
"new bzrlib.commands.Command object.", (1, 13), None))
612
611
Command.hooks = CommandHooks()
684
683
tracer = trace.Trace(count=1, trace=0)
685
684
sys.settrace(tracer.globaltrace)
687
ret = the_callable(*args, **kwargs)
690
results = tracer.results()
691
results.write_results(show_missing=1, summary=False,
687
return exception_to_return_code(the_callable, *args, **kwargs)
690
results = tracer.results()
691
results.write_results(show_missing=1, summary=False,
695
695
def apply_profiled(the_callable, *args, **kwargs):
701
701
prof = hotshot.Profile(pfname)
703
ret = prof.runcall(the_callable, *args, **kwargs) or 0
703
ret = prof.runcall(exception_to_return_code, the_callable, *args,
706
707
stats = hotshot.stats.load(pfname)
715
716
os.remove(pfname)
719
def exception_to_return_code(the_callable, *args, **kwargs):
720
"""UI level helper for profiling and coverage.
722
This transforms exceptions into a return value of 3. As such its only
723
relevant to the UI layer, and should never be called where catching
724
exceptions may be desirable.
727
return the_callable(*args, **kwargs)
728
except (KeyboardInterrupt, Exception), e:
729
# used to handle AssertionError and KeyboardInterrupt
730
# specially here, but hopefully they're handled ok by the logger now
731
exc_info = sys.exc_info()
732
exitcode = trace.report_exception(exc_info, sys.stderr)
733
if os.environ.get('BZR_PDB'):
734
print '**** entering debugger'
737
if sys.version_info[:2] < (2, 6):
739
# pdb.post_mortem(tb)
740
# but because pdb.post_mortem gives bad results for tracebacks
741
# from inside generators, we do it manually.
742
# (http://bugs.python.org/issue4150, fixed in Python 2.6)
744
# Setup pdb on the traceback
747
p.setup(tb.tb_frame, tb)
748
# Point the debugger at the deepest frame of the stack
749
p.curindex = len(p.stack) - 1
750
p.curframe = p.stack[p.curindex][0]
751
# Start the pdb prompt.
752
p.print_stack_entry(p.stack[p.curindex])
718
760
def apply_lsprofiled(filename, the_callable, *args, **kwargs):
719
761
from bzrlib.lsprof import profile
720
ret, stats = profile(the_callable, *args, **kwargs)
762
ret, stats = profile(exception_to_return_code, the_callable, *args, **kwargs)
722
764
if filename is None:
929
973
def run_bzr_catch_errors(argv):
930
# Note: The except clause logic below should be kept in sync with the
931
# profile() routine in lsprof.py.
934
except (KeyboardInterrupt, Exception), e:
935
# used to handle AssertionError and KeyboardInterrupt
936
# specially here, but hopefully they're handled ok by the logger now
937
exc_info = sys.exc_info()
938
exitcode = trace.report_exception(exc_info, sys.stderr)
939
if os.environ.get('BZR_PDB'):
940
print '**** entering debugger'
943
if sys.version_info[:2] < (2, 6):
945
# pdb.post_mortem(tb)
946
# but because pdb.post_mortem gives bad results for tracebacks
947
# from inside generators, we do it manually.
948
# (http://bugs.python.org/issue4150, fixed in Python 2.6)
974
"""Run a bzr command with parameters as described by argv.
950
# Setup pdb on the traceback
953
p.setup(tb.tb_frame, tb)
954
# Point the debugger at the deepest frame of the stack
955
p.curindex = len(p.stack) - 1
956
p.curframe = p.stack[p.curindex]
957
# Start the pdb prompt.
958
p.print_stack_entry(p.stack[p.curindex])
976
This function assumed that that UI layer is setup, that symbol deprecations
977
are already applied, and that unicode decoding has already been performed on argv.
979
return exception_to_return_code(run_bzr, argv)
966
982
def run_bzr_catch_user_errors(argv):