我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用sys.settrace()。
def run(self, cmd, globals=None, locals=None): if globals is None: import __main__ globals = __main__.__dict__ if locals is None: locals = globals self.reset() sys.settrace(self.trace_dispatch) if not isinstance(cmd, types.CodeType): cmd = cmd+'\n' try: exec cmd in globals, locals except BdbQuit: pass finally: self.quitting = 1 sys.settrace(None)
def set_continue(self): # Don't stop except at breakpoints or when finished self._set_stopinfo(self.botframe, None, -1) if not self.breaks: # no breakpoints; run without debugger overhead sys.settrace(None) frame = sys._getframe().f_back while frame and frame is not self.botframe: del frame.f_trace frame = frame.f_back
def updateSysTrace(self): ## Install or uninstall sys.settrace handler if not self.ui.catchNextExceptionBtn.isChecked() and not self.ui.catchAllExceptionsBtn.isChecked(): if sys.gettrace() == self.systrace: sys.settrace(None) return if self.ui.onlyUncaughtCheck.isChecked(): if sys.gettrace() == self.systrace: sys.settrace(None) else: if sys.gettrace() is not None and sys.gettrace() != self.systrace: self.ui.onlyUncaughtCheck.setChecked(False) raise Exception("sys.settrace is in use; cannot monitor for caught exceptions.") else: sys.settrace(self.systrace)
def spewer(frame, s, ignored): """A trace function for sys.settrace that prints every function or method call.""" from twisted.python import reflect if frame.f_locals.has_key('self'): se = frame.f_locals['self'] if hasattr(se, '__class__'): k = reflect.qual(se.__class__) else: k = reflect.qual(type(se)) print 'method %s of %s at %s' % ( frame.f_code.co_name, k, id(se) ) else: print 'function %s in %s, line %s' % ( frame.f_code.co_name, frame.f_code.co_filename, frame.f_lineno)
def run(): if len(sys.argv) == 1: sys.argv.append("--help") config = Options() try: config.parseOptions() except usage.error, ue: raise SystemExit, "%s: %s" % (sys.argv[0], ue) _initialDebugSetup(config) trialRunner = _makeRunner(config) suite = _getSuite(config) if config['until-failure']: test_result = trialRunner.runUntilFailure(suite) else: test_result = trialRunner.run(suite) if config.tracer: sys.settrace(None) results = config.tracer.results() results.write_results(show_missing=1, summary=False, coverdir=config.coverdir) sys.exit(not test_result.wasSuccessful())
def runeval(self, expr, globals=None, locals=None): if globals is None: import __main__ globals = __main__.__dict__ if locals is None: locals = globals self.reset() sys.settrace(self.trace_dispatch) if not isinstance(expr, types.CodeType): expr = expr+'\n' try: return eval(expr, globals, locals) except BdbQuit: pass finally: self.quitting = 1 sys.settrace(None)
def tracing(f): """Enable tracing just within a function call.""" def globaltrace(frame,why,arg): if why == "call": return localtrace return None def localtrace(frame, why, arg): if why == "line": fname = frame.f_code.co_filename lineno = frame.f_lineno base = os.path.basename(fname) print("%s(%s): %s" % (base, lineno, linecache.getline(fname, lineno))) return localtrace @wrap(f) def wrapper(*args,**kw): sys.settrace(globaltrace) result = f(*args,**kw) sys.settrace(None) return result return wrapper
def test_element_cyclic_gc_none(self): # test if cyclic reference can crash etree Element = self.etree.Element # must disable tracing as it could change the refcounts trace_func = sys.gettrace() try: sys.settrace(None) gc.collect() count = sys.getrefcount(None) l = [Element('name'), Element('name')] l.append(l) del l gc.collect() self.assertEqual(sys.getrefcount(None), count) finally: sys.settrace(trace_func)
def test_ipdb_magics2(): '''Test ipdb with a very short function. >>> old_trace = sys.gettrace() >>> def bar(): ... pass Run ipdb. >>> with PdbTestInput([ ... 'continue', ... ]): ... debugger.Pdb().runcall(bar) > <doctest ...>(2)bar() 1 def bar(): ----> 2 pass <BLANKLINE> ipdb> continue Restore previous trace function, e.g. for coverage.py >>> sys.settrace(old_trace) '''
def can_quit(): '''Test that quit work in ipydb >>> old_trace = sys.gettrace() >>> def bar(): ... pass >>> with PdbTestInput([ ... 'quit', ... ]): ... debugger.Pdb().runcall(bar) > <doctest ...>(2)bar() 1 def bar(): ----> 2 pass <BLANKLINE> ipdb> quit Restore previous trace function, e.g. for coverage.py >>> sys.settrace(old_trace) '''
def can_exit(): '''Test that quit work in ipydb >>> old_trace = sys.gettrace() >>> def bar(): ... pass >>> with PdbTestInput([ ... 'exit', ... ]): ... debugger.Pdb().runcall(bar) > <doctest ...>(2)bar() 1 def bar(): ----> 2 pass <BLANKLINE> ipdb> exit Restore previous trace function, e.g. for coverage.py >>> sys.settrace(old_trace) '''
def do_debug(self, arg): """debug code Enter a recursive debugger that steps through the code argument (which is an arbitrary expression or statement to be executed in the current environment). """ sys.settrace(None) globals = self.curframe.f_globals locals = self.curframe_locals p = Pdb(self.completekey, self.stdin, self.stdout) p.prompt = "(%s) " % self.prompt.strip() self.message("ENTERING RECURSIVE DEBUGGER") sys.call_tracing(p.run, (arg, globals, locals)) self.message("LEAVING RECURSIVE DEBUGGER") sys.settrace(self.trace_dispatch) self.lastcmd = p.lastcmd
def run(self, cmd, globals=None, locals=None): if globals is None: import __main__ globals = __main__.__dict__ if locals is None: locals = globals self.reset() if isinstance(cmd, str): cmd = compile(cmd, "<string>", "exec") sys.settrace(self.trace_dispatch) try: exec(cmd, globals, locals) except BdbQuit: pass finally: self.quitting = True sys.settrace(None)
def testLocalsClass_WithTrace(self): # Issue23728: after the trace function returns, the locals() # dictionary is used to update all variables, this used to # include free variables. But in class statements, free # variables are not inserted... import sys sys.settrace(lambda a,b,c:None) try: x = 12 class C: def f(self): return x self.assertEqual(x, 12) # Used to raise UnboundLocalError finally: sys.settrace(None)
def testInteractionWithTraceFunc(self): import sys def tracer(a,b,c): return tracer def adaptgetter(name, klass, getter): kind, des = getter if kind == 1: # AV happens when stepping from this line to next if des == "": des = "_%s__%s" % (klass.__name__, name) return lambda obj: getattr(obj, des) class TestClass: pass sys.settrace(tracer) adaptgetter("foo", TestClass, (1, "")) sys.settrace(None) self.assertRaises(TypeError, sys.settrace)
def run_test_for_event(self, event): """Tests that an exception raised in response to the given event is handled OK.""" self.raiseOnEvent = event try: for i in range(sys.getrecursionlimit() + 1): sys.settrace(self.trace) try: self.f() except ValueError: pass else: self.fail("exception not thrown!") except RuntimeError: self.fail("recursion counter not reset") # Test the handling of exceptions raised by each kind of trace event.
def test_trash_stack(self): def f(): for i in range(5): print(i) # line tracing will raise an exception at this line def g(frame, why, extra): if (why == 'line' and frame.f_lineno == f.__code__.co_firstlineno + 2): raise RuntimeError("i am crashing") return g sys.settrace(g) try: f() except RuntimeError: # the test is really that this doesn't segfault: import gc gc.collect() else: self.fail("exception not propagated") # 'Jump' tests: assigning to frame.f_lineno within a trace function # moves the execution position - it's how debuggers implement a Jump # command (aka. "Set next statement").
def test_jump_to_firstlineno(self): # This tests that PDB can jump back to the first line in a # file. See issue #1689458. It can only be triggered in a # function call if the function is defined on a single line. code = compile(""" # Comments don't count. output.append(2) # firstlineno is here. output.append(3) output.append(4) """, "<fake module>", "exec") class fake_function: __code__ = code jump = (2, 0) tracer = JumpTracer(fake_function) sys.settrace(tracer.trace) namespace = {"output": []} exec(code, namespace) sys.settrace(None) self.compare_jump_output([2, 3, 2, 3, 4], namespace["output"])
def test_finalize_with_trace(self): # Issue1733757 # Avoid a deadlock when sys.settrace steps into threading._shutdown assert_python_ok("-c", """if 1: import sys, threading # A deadlock-killer, to prevent the # testsuite to hang forever def killer(): import os, time time.sleep(2) print('program blocked; aborting') os._exit(2) t = threading.Thread(target=killer) t.daemon = True t.start() # This is the trace function def func(frame, event, arg): threading.current_thread() return func sys.settrace(func) """)
def test_issue9936(self): tracer = trace.Trace(trace=0, count=1) modname = 'test.tracedmodules.testmod' # Ensure that the module is executed in import if modname in sys.modules: del sys.modules[modname] cmd = ("import test.tracedmodules.testmod as t;" "t.func(0); t.func2();") with captured_stdout() as stdout: self._coverage(tracer, cmd) stdout.seek(0) stdout.readline() coverage = {} for line in stdout: lines, cov, module = line.split()[:3] coverage[module] = (int(lines), int(cov[:-1])) # XXX This is needed to run regrtest.py as a script modname = trace._fullmodname(sys.modules[modname].__file__) self.assertIn(modname, coverage) self.assertEqual(coverage[modname], (5, 100)) ### Tests that don't mess with sys.settrace and can be traced ### themselves TODO: Skip tests that do mess with sys.settrace when ### regrtest is invoked with -T option.
def _call_httpie_main(self): context = self._final_context() args = extract_args_for_httpie_main(context, self.method) env = Environment(stdout=self.output, stdin=sys.stdin, is_windows=False) env.stdout_isatty = self.output.isatty() env.stdin_isatty = sys.stdin.isatty() # XXX: httpie_main() doesn't provide an API for us to get the # HTTP response object, so we use this super dirty hack - # sys.settrace() to intercept get_response() that is called in # httpie_main() internally. The HTTP response intercepted is # assigned to self.last_response, which self.listener may be # interested in. sys.settrace(self._trace_get_response) try: httpie_main(args, env=env) finally: sys.settrace(None)
def run(self): if hasattr(self.service, 'name'): name = self.service.name else: name = self.service.__class__.__name__ self.log.debug("running ServiceThread '%s'" % name) self.name = name if trace: sys.settrace(trace) try: self.service.run() except Exception: exc_info = sys.exc_info() backtrace = '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info()))) self.log.error("Exception in main loop. backtrace: %s" % backtrace) os._exit(-1)
def no_tracing(func): """Decorator to temporarily turn off tracing for the duration of a test.""" if not hasattr(sys, 'gettrace'): return func else: def wrapper(*args, **kwargs): original_trace = sys.gettrace() try: sys.settrace(None) return func(*args, **kwargs) finally: sys.settrace(original_trace) wrapper.__name__ = func.__name__ return wrapper # Return True if opcode code appears in the pickle, else False.
def run_test_for_event(self, event): """Tests that an exception raised in response to the given event is handled OK.""" self.raiseOnEvent = event try: for i in xrange(sys.getrecursionlimit() + 1): sys.settrace(self.trace) try: self.f() except ValueError: pass else: self.fail("exception not raised!") except RuntimeError: self.fail("recursion counter not reset") # Test the handling of exceptions raised by each kind of trace event.
def test_trash_stack(self): def f(): for i in range(5): print i # line tracing will raise an exception at this line def g(frame, why, extra): if (why == 'line' and frame.f_lineno == f.func_code.co_firstlineno + 2): raise RuntimeError, "i am crashing" return g sys.settrace(g) try: f() except RuntimeError: # the test is really that this doesn't segfault: import gc gc.collect() else: self.fail("exception not propagated") # 'Jump' tests: assigning to frame.f_lineno within a trace function # moves the execution position - it's how debuggers implement a Jump # command (aka. "Set next statement").
def test_jump_to_firstlineno(self): # This tests that PDB can jump back to the first line in a # file. See issue #1689458. It can only be triggered in a # function call if the function is defined on a single line. code = compile(""" # Comments don't count. output.append(2) # firstlineno is here. output.append(3) output.append(4) """, "<fake module>", "exec") class fake_function: func_code = code jump = (2, 0) tracer = JumpTracer(fake_function) sys.settrace(tracer.trace) namespace = {"output": []} exec code in namespace sys.settrace(None) self.compare_jump_output([2, 3, 2, 3, 4], namespace["output"])
def do_debug(self, arg): sys.settrace(None) globals = self.curframe.f_globals locals = self.curframe_locals p = Pdb(self.completekey, self.stdin, self.stdout) p.prompt = "(%s) " % self.prompt.strip() print >>self.stdout, "ENTERING RECURSIVE DEBUGGER" sys.call_tracing(p.run, (arg, globals, locals)) print >>self.stdout, "LEAVING RECURSIVE DEBUGGER" sys.settrace(self.trace_dispatch) self.lastcmd = p.lastcmd
def set_trace(self, frame=None): """Start debugging from `frame`. If frame is not specified, debugging starts from caller's frame. """ if frame is None: frame = sys._getframe().f_back self.reset() while frame: frame.f_trace = self.trace_dispatch self.botframe = frame frame = frame.f_back self.set_step() sys.settrace(self.trace_dispatch)
def set_quit(self): self.stopframe = self.botframe self.returnframe = None self.quitting = 1 sys.settrace(None) # Derived classes and clients can call the following methods # to manipulate breakpoints. These methods return an # error message is something went wrong, None if all is well. # Set_break prints out the breakpoint line and file:lineno. # Call self.get_*break*() to see the breakpoints or better # for bp in Breakpoint.bpbynumber: if bp: bp.bpprint().
def runcall(self, func, *args, **kwds): self.reset() sys.settrace(self.trace_dispatch) res = None try: res = func(*args, **kwds) except BdbQuit: pass finally: self.quitting = 1 sys.settrace(None) return res
def settrace(func): global _trace_hook _trace_hook = func # Synchronization classes
def set_continue(self): # Calling set_continue unconditionally would break unit test # coverage reporting, as Bdb.set_continue calls sys.settrace(None). if self.__debugger_used: pdb.Pdb.set_continue(self)
def _unsettrace(): sys.settrace(None)
def _settrace(func): threading.settrace(func) sys.settrace(func)