"""A rough port of the _lsprof module to Python.""" import sys import time import types import collections hpTimer = time.time hpTimerUnit = 1.0 DOUBLE_TIMER_PRECISION = 4294967296.0 POF_ENABLED = 1 POF_SUBCALLS = 2 POF_BUILTINS = 3 def PyCFunction_Check(obj): return type(obj) is types.BuiltinFunctionType def NormalizeUserObj(obj): if not PyCFunction_Check(obj): return obj if obj.__self__ is None: mod = obj.__module__ if mod: return "<%s.%s>" % (mod, obj.__name__) else: return "<%s>" % (obj.__name__,) else: try: return repr(getattr(type(obj.__self__), obj.__name__)) except: return " 0.0: collect = StatsCollector(self.externalTimerUnit) else: collect = StatsCollector(1.0 / DOUBLE_TIMER_PRECISION) for entry in self.profilerEntries.values(): collect.stats_for_entry(entry) return collect.list def call_timer(self): if self.externalTimer is not None: result = self.externalTimer() if self.externalTimerUnit> 0.0: # XXX Should this coerce or raise a TypeError when the result # is a float? return int(result) else: # XXX Should this typecheck for a float? return result * DOUBLE_TIMER_PRECISION else: return hpTimer() def _set_config(self, subcalls, builtins): if subcalls is not None: if subcalls: self.flags.add(POF_SUBCALLS) else: self.flags.discard(POF_SUBCALLS) if builtins is not None: if builtins: self.flags.add(POF_BUILTINS) else: self.flags.discard(POF_BUILTINS) def _callback(self, frame, what, arg): if what == "call": self._ptrace_enter_call(frame.f_code, frame.f_code) elif what == "return": self._ptrace_leave_call(frame.f_code) elif what == "c_call": if POF_BUILTINS in self.flags and PyCFunction_Check(arg): self._ptrace_enter_call(arg, arg) elif what == "c_return" or what == "c_exception": if POF_BUILTINS in self.flags and PyCFunction_Check(arg): self._ptrace_leave_call(arg) def _flush_unmatched(self): while self.currentProfilerContext: pContext = self.currentProfilerContext profEntry = pContext.ctxEntry if profEntry: pContext.stop(self, profEntry) else: self.currentProfilerContext = pContext.previous def _ptrace_enter_call(self, key, userObj): # XXX cProfile restores exceptions raised here. This seems unneeded for # the Python version. profEntry = self.profilerEntries.get(key) if profEntry is None: profEntry = ProfilerEntry(self, key, userObj) pContext = self.freelistProfilerContext if pContext: self.freelistProfilerContext = pContext.previous else: pContext = ProfilerContext() pContext.start(self, profEntry) def _ptrace_leave_call(self, key): pContext = self.currentProfilerContext if pContext is None: return profEntry = self.profilerEntries.get(key) if profEntry: pContext.stop(self, profEntry) else: self.currentProfilerContext = pContext.previous pContext.previous = self.freelistProfilerContext self.freelistProfilerContext = pContext

AltStyle によって変換されたページ (->オリジナル) /