diff -r 3ccd9dfb8ab7 Lib/test/test_trace.py --- a/Lib/test/test_trace.py Sat Nov 21 14:10:30 2015 +0200 +++ b/Lib/test/test_trace.py Sat Nov 21 15:12:20 2015 -0500 @@ -99,14 +99,14 @@ class TestLineCounts(unittest.TestCase): - """White-box testing of line-counting, via runfunc""" + """White-box testing of line-counting, via runcall""" def setUp(self): self.addCleanup(sys.settrace, sys.gettrace()) self.tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0) self.my_py_filename = fix_ext_py(__file__) def test_traced_func_linear(self): - result = self.tracer.runfunc(traced_func_linear, 2, 5) + result = self.tracer.runcall(traced_func_linear, 2, 5) self.assertEqual(result, 7) # all lines are executed once @@ -118,7 +118,7 @@ self.assertEqual(self.tracer.results().counts, expected) def test_traced_func_loop(self): - self.tracer.runfunc(traced_func_loop, 2, 3) + self.tracer.runcall(traced_func_loop, 2, 3) firstlineno = get_firstlineno(traced_func_loop) expected = { @@ -130,7 +130,7 @@ self.assertEqual(self.tracer.results().counts, expected) def test_traced_func_importing(self): - self.tracer.runfunc(traced_func_importing, 2, 5) + self.tracer.runcall(traced_func_importing, 2, 5) firstlineno = get_firstlineno(traced_func_importing) expected = { @@ -142,7 +142,7 @@ self.assertEqual(self.tracer.results().counts, expected) def test_trace_func_generator(self): - self.tracer.runfunc(traced_func_calling_generator) + self.tracer.runcall(traced_func_calling_generator) firstlineno_calling = get_firstlineno(traced_func_calling_generator) firstlineno_gen = get_firstlineno(traced_func_generator) @@ -157,7 +157,7 @@ self.assertEqual(self.tracer.results().counts, expected) def test_trace_list_comprehension(self): - self.tracer.runfunc(traced_caller_list_comprehension) + self.tracer.runcall(traced_caller_list_comprehension) firstlineno_calling = get_firstlineno(traced_caller_list_comprehension) firstlineno_called = get_firstlineno(traced_doubler) @@ -180,7 +180,7 @@ tracer = Trace(count=1, trace=0, countfuncs=0, countcallers=0) traced_obj = TracedClass(25) method = getattr(traced_obj, methname) - tracer.runfunc(method, 20) + tracer.runcall(method, 20) firstlineno = get_firstlineno(method) expected = { @@ -229,7 +229,7 @@ sys.settrace(self._saved_tracefunc) def test_simple_caller(self): - self.tracer.runfunc(traced_func_simple_caller, 1) + self.tracer.runcall(traced_func_simple_caller, 1) expected = { self.filemod + ('traced_func_simple_caller',): 1, @@ -238,7 +238,7 @@ self.assertEqual(self.tracer.results().calledfuncs, expected) def test_loop_caller_importing(self): - self.tracer.runfunc(traced_func_importing_caller, 1) + self.tracer.runcall(traced_func_importing_caller, 1) expected = { self.filemod + ('traced_func_simple_caller',): 1, @@ -253,7 +253,7 @@ 'pre-existing trace function throws off measurements') def test_inst_method_calling(self): obj = TracedClass(20) - self.tracer.runfunc(obj.inst_method_calling, 1) + self.tracer.runcall(obj.inst_method_calling, 1) expected = { self.filemod + ('TracedClass.inst_method_calling',): 1, @@ -273,10 +273,10 @@ @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), 'pre-existing trace function throws off measurements') def test_loop_caller_importing(self): - self.tracer.runfunc(traced_func_importing_caller, 1) + self.tracer.runcall(traced_func_importing_caller, 1) expected = { - ((os.path.splitext(trace.__file__)[0] + '.py', 'trace', 'Trace.runfunc'), + ((os.path.splitext(trace.__file__)[0] + '.py', 'trace', 'Trace.runcall'), (self.filemod + ('traced_func_importing_caller',))): 1, ((self.filemod + ('traced_func_simple_caller',)), (self.filemod + ('traced_func_linear',))): 1, diff -r 3ccd9dfb8ab7 Lib/trace.py --- a/Lib/trace.py Sat Nov 21 14:10:30 2015 +0200 +++ b/Lib/trace.py Sat Nov 21 15:12:20 2015 -0500 @@ -499,6 +499,17 @@ dict = __main__.__dict__ self.runctx(cmd, dict, dict) + def runcall(self, func, *args, **kw): + result = None + if not self.donothing: + sys.settrace(self.globaltrace) + try: + result = func(*args, **kw) + finally: + if not self.donothing: + sys.settrace(None) + return result + def runctx(self, cmd, globals=None, locals=None): if globals is None: globals = {} if locals is None: locals = {} @@ -511,15 +522,8 @@ _unsettrace() def runfunc(self, func, *args, **kw): - result = None - if not self.donothing: - sys.settrace(self.globaltrace) - try: - result = func(*args, **kw) - finally: - if not self.donothing: - sys.settrace(None) - return result + #replaced by runcall for consistency with Trace.runcall + return runcall(self, func, *args, **kw) def file_module_function_of(self, frame): code = frame.f_code