~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lsprof.py

  • Committer: Matt Nordhoff
  • Date: 2009-04-04 02:50:01 UTC
  • mfrom: (4253 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4256.
  • Revision ID: mnordhoff@mattnordhoff.com-20090404025001-z1403k0tatmc8l91
Merge bzr.dev, fixing conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
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():
52
47
            pp.disable()
53
48
        threading.setprofile(None)
54
 
    
 
49
 
55
50
    threads = {}
56
51
    for tid, pp in _g_threadmap.items():
57
52
        threads[tid] = Stats(pp.getstats(), {})
156
151
 
157
152
    This code is taken from http://ddaa.net/blog/python/lsprof-calltree
158
153
    with the changes made by J.P. Calderone and Itamar applied. Note that
159
 
    isinstance(code, str) needs to be used at times to determine if the code 
 
154
    isinstance(code, str) needs to be used at times to determine if the code
160
155
    object is actually an external code object (with a filename, etc.) or
161
156
    a Python built-in.
162
157
    """
166
161
        self.out_file = None
167
162
 
168
163
    def output(self, out_file):
169
 
        self.out_file = out_file        
 
164
        self.out_file = out_file
170
165
        out_file.write('events: Ticks\n')
171
166
        self._print_summary()
172
167
        for entry in self.data: