~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lsprof.py

  • Committer: Robert Collins
  • Date: 2009-03-08 06:18:06 UTC
  • mto: This revision was merged to the branch mainline in revision 4102.
  • Revision ID: robertc@robertcollins.net-20090308061806-uvu37ccjrrzuspei
Refactor profiling exception handling to restore clear layers - command handling in commands.py, profiling in lsprof.py.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
def profile(f, *args, **kwds):
30
30
    """Run a function profile.
31
31
 
 
32
    Exceptions are not caught: If you need stats even when exceptions are to be
 
33
    raised, passing in a closure that will catch the exceptions and transform
 
34
    them appropriately for your driver function.
 
35
 
32
36
    :return: The functions return value and a stats object.
33
37
    """
34
38
    global _g_threadmap
35
39
    p = Profiler()
36
40
    p.enable(subcalls=True)
37
41
    threading.setprofile(_thread_profile)
38
 
    # Note: The except clause is needed below so that profiling data still
39
 
    # gets dumped even when exceptions are encountered. The except clause code
40
 
    # is taken straight from run_bzr_catch_errrors() in commands.py and ought
41
 
    # to be kept in sync with it.
42
42
    try:
43
 
        try:
44
 
            ret = f(*args, **kwds)
45
 
        except (KeyboardInterrupt, Exception), e:
46
 
            import bzrlib.trace
47
 
            bzrlib.trace.report_exception(sys.exc_info(), sys.stderr)
48
 
            ret = 3
 
43
        ret = f(*args, **kwds)
49
44
    finally:
50
45
        p.disable()
51
46
        for pp in _g_threadmap.values():