def test_handles_bound_methods(self): class Class: def method(self, x): return x + 1 meth = Class().method rewrite_function(meth) assert_equal(meth(1), 2)
def test_handles_functions_with_free_variables(self): x = 1 def fun(): return x + 1 rewrite_function(fun) assert_equal(fun(), 2)
def trace_function(self, fun): dis.dis(fun.func_code) rewrite_function(fun) self.btracer.setup() sys.settrace(self._trace) try: fun() finally: sys.settrace(None) self.btracer.teardown()
def test_rewrites_each_function_only_once(self): def other(): pass def fun(): other() other() rewrite_function(other) rewritten_code = other.func_code self.trace_function(fun) assert other.func_code is rewritten_code
def trace(self, code): """Trace execution of given code. Code may be either a function or a string with Python code. This method may be invoked many times for a single tracer instance. """ self.setup(code) self.btracer.setup() rewrite_function(self.top_level_function) sys.settrace(self.tracer) try: self.top_level_function() finally: sys.settrace(None) self.teardown() self.btracer.teardown()
def doimport(): import foo foo.bleh() def doyield(): def y(): yield 1 yield 2 yield 3 for x in y(): chr(x) def doprint(): print 1, 2, 3 print>>sys.stderr, 4 ###################################################################### if __name__ == '__main__': f = doprint btracer.setup() dis.dis(f) rewrite_function(f) sys.settrace(trace) try: f() finally: sys.settrace(None)
def doyield(): def y(): yield 1 yield 2 yield 3 for x in y(): chr(x) def doprint(): print 1, 2, 3 print >> sys.stderr, 4 ###################################################################### if __name__ == '__main__': f = doprint btracer.setup() dis.dis(f) rewrite_function(f) sys.settrace(trace) try: f() finally: sys.settrace(None)