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]
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:
917
960
def run_bzr_catch_errors(argv):
918
# Note: The except clause logic below should be kept in sync with the
919
# profile() routine in lsprof.py.
922
except (KeyboardInterrupt, Exception), e:
923
# used to handle AssertionError and KeyboardInterrupt
924
# specially here, but hopefully they're handled ok by the logger now
925
exc_info = sys.exc_info()
926
exitcode = trace.report_exception(exc_info, sys.stderr)
927
if os.environ.get('BZR_PDB'):
928
print '**** entering debugger'
931
if sys.version_info[:2] < (2, 6):
933
# pdb.post_mortem(tb)
934
# but because pdb.post_mortem gives bad results for tracebacks
935
# from inside generators, we do it manually.
936
# (http://bugs.python.org/issue4150, fixed in Python 2.6)
938
# Setup pdb on the traceback
941
p.setup(tb.tb_frame, tb)
942
# Point the debugger at the deepest frame of the stack
943
p.curindex = len(p.stack) - 1
944
p.curframe = p.stack[p.curindex]
945
# Start the pdb prompt.
946
p.print_stack_entry(p.stack[p.curindex])
961
"""Run a bzr command with parameters as described by argv.
963
This function assumed that that UI layer is setup, that symbol deprecations
964
are already applied, and that unicode decoding has already been performed on argv.
966
return exception_to_return_code(run_bzr, argv)
954
969
def run_bzr_catch_user_errors(argv):