我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用sys.getprofile()。
def restore_profiler(): """If a typechecking profiler is active, e.g. created by pytypes.set_global_typechecked_profiler(), such a profiler must be restored whenever a TypeCheckError is caught. The call must stem from the thread that raised the error. Otherwise the typechecking profiler is implicitly disabled. Alternatively one can turn pytypes into warning mode. In that mode no calls to this function are required (unless one uses filterwarnings("error") or likewise). """ idn = threading.current_thread().ident if not sys.getprofile() is None: warn("restore_profiler: Current profile is not None!") if not idn in _saved_profilers: warn("restore_profiler: No saved profiler for calling thread!") else: sys.setprofile(_saved_profilers[idn]) del _saved_profilers[idn]
def start(self): if self._active: raise RuntimeError('type checker already running') elif self._pending: raise RuntimeError('type checker already starting up') self._pending = True # Install this instance as the current profiler self._previous_profiler = sys.getprofile() self._set_caller_level_shift(0) sys.setprofile(self) # If requested, set this instance as the default profiler for all future threads # (does not affect existing threads) if self.all_threads: self._previous_thread_profiler = threading._profile_hook threading.setprofile(self) self._active, self._pending = True, False
def stop(self): if self._active and not self._pending: self._pending = True if sys.getprofile() is self: sys.setprofile(self._previous_profiler) if not self._previous_profiler is None and \ isinstance(self._previous_profiler, TypeAgent): self._previous_profiler._set_caller_level_shift(0) else: if not (sys.getprofile() is None and self._cleared): warn('the system profiling hook has changed unexpectedly') if self.all_threads: if threading._profile_hook is self: threading.setprofile(self._previous_thread_profiler) else: # pragma: no cover warn('the threading profiling hook has changed unexpectedly') self._active, self._pending = False, False
def test_execute_and_capture_locals(): def func(): a=3 return a+4 def outer_func(): b=4 def nested(): return func()+b+5 return nested out, local_vars = execute_and_capture_locals(func) assert out == 3+4 assert local_vars == {'a': 3} assert sys.getprofile() is None out, local_vars = execute_and_capture_locals(outer_func()) assert out == 7+4+5 assert local_vars == {'b': 4, 'func': func} assert sys.getprofile() is None
def start(): """ Turn on profiling. """ global _profile_start, _profile_setup, _call_stack, _inst_data if _profile_start is not None: print("profiling is already active.") return if not _profile_setup: setup() # just do a default setup _profile_start = etime() _call_stack.append(('$total', _profile_start, None)) if '$total' not in _inst_data: _inst_data['$total'] = [None, 0., 0] if sys.getprofile() is not None: raise RuntimeError("another profile function is already active.") sys.setprofile(_instance_profile_callback)
def trace_calls( logger: CallTraceLogger, code_filter: Optional[CodeFilter] = None, sample_rate: Optional[int] = None, ) -> Iterator[None]: """Enable call tracing for a block of code""" old_trace = sys.getprofile() sys.setprofile(CallTracer(logger, code_filter, sample_rate)) try: yield finally: sys.setprofile(old_trace) logger.flush()
def test_empty(self): self.assertIsNone(sys.getprofile())
def test_setget(self): def fn(*args): pass sys.setprofile(fn) self.assertIs(sys.getprofile(), fn)
def start(): """ Start call tracing. """ global _trace_calls if sys.getprofile() is not None: raise RuntimeError("another profile function is already active.") if _trace_calls is None: raise RuntimeError("trace.setup() was not called before trace.start().") sys.setprofile(_trace_calls)
def start(): """ Turn on memory profiling. """ global _trace_memory if sys.getprofile() is not None: raise RuntimeError("another profile function is already active.") if _trace_memory is None: raise RuntimeError("trace.setup() was not called before trace.start().") sys.setprofile(_trace_memory)