~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lsprof.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-03-10 03:35:20 UTC
  • mfrom: (4084.6.3 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20090310033520-f2ynw0fprjw433m2
(robertc) Refactor profiling exception handling support to clear
        layers up and provide the same support for coverage and
        non-lsprof profiling. (Robert Collins)

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():