672
672
tracer = trace.Trace(count=1, trace=0)
673
673
sys.settrace(tracer.globaltrace)
675
ret = the_callable(*args, **kwargs)
678
results = tracer.results()
679
results.write_results(show_missing=1, summary=False,
676
return exception_to_return_code(the_callable, *args, **kwargs)
679
results = tracer.results()
680
results.write_results(show_missing=1, summary=False,
683
684
def apply_profiled(the_callable, *args, **kwargs):
689
690
prof = hotshot.Profile(pfname)
691
ret = prof.runcall(the_callable, *args, **kwargs) or 0
692
ret = prof.runcall(exception_to_return_code, the_callable, *args,
694
696
stats = hotshot.stats.load(pfname)
703
705
os.remove(pfname)
708
def exception_to_return_code(the_callable, *args, **kwargs):
709
"""UI level helper for profiling and coverage.
711
This transforms exceptions into a return value of 3. As such its only
712
relevant to the UI layer, and should never be called where catching
713
exceptions may be desirable.
716
return the_callable(*args, **kwargs)
717
except (KeyboardInterrupt, Exception), e:
718
# used to handle AssertionError and KeyboardInterrupt
719
# specially here, but hopefully they're handled ok by the logger now
720
exc_info = sys.exc_info()
721
exitcode = trace.report_exception(exc_info, sys.stderr)
722
if os.environ.get('BZR_PDB'):
723
print '**** entering debugger'
726
if sys.version_info[:2] < (2, 6):
728
# pdb.post_mortem(tb)
729
# but because pdb.post_mortem gives bad results for tracebacks
730
# from inside generators, we do it manually.
731
# (http://bugs.python.org/issue4150, fixed in Python 2.6)
733
# Setup pdb on the traceback
736
p.setup(tb.tb_frame, tb)
737
# Point the debugger at the deepest frame of the stack
738
p.curindex = len(p.stack) - 1
739
p.curframe = p.stack[p.curindex][0]
740
# Start the pdb prompt.
741
p.print_stack_entry(p.stack[p.curindex])
706
749
def apply_lsprofiled(filename, the_callable, *args, **kwargs):
707
750
from bzrlib.lsprof import profile
708
ret, stats = profile(the_callable, *args, **kwargs)
751
ret, stats = profile(exception_to_return_code, the_callable, *args, **kwargs)
710
753
if filename is None:
919
962
def run_bzr_catch_errors(argv):
920
# Note: The except clause logic below should be kept in sync with the
921
# profile() routine in lsprof.py.
924
except (KeyboardInterrupt, Exception), e:
925
# used to handle AssertionError and KeyboardInterrupt
926
# specially here, but hopefully they're handled ok by the logger now
927
exc_info = sys.exc_info()
928
exitcode = trace.report_exception(exc_info, sys.stderr)
929
if os.environ.get('BZR_PDB'):
930
print '**** entering debugger'
933
if sys.version_info[:2] < (2, 6):
935
# pdb.post_mortem(tb)
936
# but because pdb.post_mortem gives bad results for tracebacks
937
# from inside generators, we do it manually.
938
# (http://bugs.python.org/issue4150, fixed in Python 2.6)
963
"""Run a bzr command with parameters as described by argv.
940
# Setup pdb on the traceback
943
p.setup(tb.tb_frame, tb)
944
# Point the debugger at the deepest frame of the stack
945
p.curindex = len(p.stack) - 1
946
p.curframe = p.stack[p.curindex][0]
947
# Start the pdb prompt.
948
p.print_stack_entry(p.stack[p.curindex])
965
This function assumed that that UI layer is setup, that symbol deprecations
966
are already applied, and that unicode decoding has already been performed on argv.
968
return exception_to_return_code(run_bzr, argv)
956
971
def run_bzr_catch_user_errors(argv):