0001 """High-perfomance logging profiler, mostly written in C.""" 0002 0003 import _hotshot 0004 0005 from _hotshot import ProfilerError 0006 0007 0008 class Profile: 0009 def __init__(self, logfn, lineevents=0, linetimings=1): 0010 self.lineevents = lineevents and 1 or 0 0011 self.linetimings = (linetimings and lineevents) and 1 or 0 0012 self._prof = p = _hotshot.profiler( 0013 logfn, self.lineevents, self.linetimings) 0014 0015 # Attempt to avoid confusing results caused by the presence of 0016 # Python wrappers around these functions, but only if we can 0017 # be sure the methods have not been overridden or extended. 0018 if self.__class__ is Profile: 0019 self.close = p.close 0020 self.start = p.start 0021 self.stop = p.stop 0022 self.addinfo = p.addinfo 0023 0024 def close(self): 0025 """Close the logfile and terminate the profiler.""" 0026 self._prof.close() 0027 0028 def fileno(self): 0029 """Return the file descriptor of the profiler's log file.""" 0030 return self._prof.fileno() 0031 0032 def start(self): 0033 """Start the profiler.""" 0034 self._prof.start() 0035 0036 def stop(self): 0037 """Stop the profiler.""" 0038 self._prof.stop() 0039 0040 def addinfo(self, key, value): 0041 """Add an arbitrary labelled value to the profile log.""" 0042 self._prof.addinfo(key, value) 0043 0044 # These methods offer the same interface as the profile.Profile class, 0045 # but delegate most of the work to the C implementation underneath. 0046 0047 def run(self, cmd): 0048 """Profile an exec-compatible string in the script 0049 environment. 0050 0051 The globals from the __main__ module are used as both the 0052 globals and locals for the script. 0053 """ 0054 import __main__ 0055 dict = __main__.__dict__ 0056 return self.runctx(cmd, dict, dict) 0057 0058 def runctx(self, cmd, globals, locals): 0059 """Evaluate an exec-compatible string in a specific 0060 environment. 0061 0062 The string is compiled before profiling begins. 0063 """ 0064 code = compile(cmd, "<string>", "exec") 0065 self._prof.runcode(code, globals, locals) 0066 return self 0067 0068 def runcall(self, func, *args, **kw): 0069 """Profile a single call of a callable. 0070 0071 Additional positional and keyword arguments may be passed 0072 along; the result of the call is returned, and exceptions are 0073 allowed to propogate cleanly, while ensuring that profiling is 0074 disabled on the way out. 0075 """ 0076 return self._prof.runcall(func, args, kw) 0077
Generated by PyXR 0.9.4