1 """High-perfomance logging profiler, mostly written in C."""
5 from _hotshot
import ProfilerError
9 def __init__(self
, logfn
, lineevents
=0, linetimings
=1):
10 self
.lineevents
= lineevents
and 1 or 0
11 self
.linetimings
= (linetimings
and lineevents
) and 1 or 0
12 self
._prof
= p
= _hotshot
.profiler(
13 logfn
, self
.lineevents
, self
.linetimings
)
15 # Attempt to avoid confusing results caused by the presence of
16 # Python wrappers around these functions, but only if we can
17 # be sure the methods have not been overridden or extended.
18 if self
.__class
__ is Profile
:
22 self
.addinfo
= p
.addinfo
25 """Close the logfile and terminate the profiler."""
29 """Return the file descriptor of the profiler's log file."""
30 return self
._prof
.fileno()
33 """Start the profiler."""
37 """Stop the profiler."""
40 def addinfo(self
, key
, value
):
41 """Add an arbitrary labelled value to the profile log."""
42 self
._prof
.addinfo(key
, value
)
44 # These methods offer the same interface as the profile.Profile class,
45 # but delegate most of the work to the C implementation underneath.
48 """Profile an exec-compatible string in the script
51 The globals from the __main__ module are used as both the
52 globals and locals for the script.
55 dict = __main__
.__dict
__
56 return self
.runctx(cmd
, dict, dict)
58 def runctx(self
, cmd
, globals, locals):
59 """Evaluate an exec-compatible string in a specific
62 The string is compiled before profiling begins.
64 code
= compile(cmd
, "<string>", "exec")
65 self
._prof
.runcode(code
, globals, locals)
68 def runcall(self
, func
, *args
, **kw
):
69 """Profile a single call of a callable.
71 Additional positional and keyword arguments may be passed
72 along; the result of the call is returned, and exceptions are
73 allowed to propogate cleanly, while ensuring that profiling is
74 disabled on the way out.
76 return self
._prof
.runcall(func
, args
, kw
)