diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py --- a/Lib/test/test_sys_settrace.py +++ b/Lib/test/test_sys_settrace.py @@ -238,6 +238,14 @@ self.events = [] def trace(self, frame, event, arg): self.events.append((frame.f_lineno, event)) + if event == 'call': + if ('call_stop_tracing' in frame.f_locals and + frame.f_locals['call_stop_tracing'] is True): + return + elif event == 'line': + if ('line_stop_tracing' in frame.f_locals and + frame.f_locals['line_stop_tracing'] is True): + return return self.trace def traceWithGenexp(self, frame, event, arg): (o for o in [1]) @@ -388,6 +396,60 @@ (257, 'line'), (257, 'return')]) + def test_17_line_stop_tracing(self): + # issue 11992: tracing not disabled when the local trace function + # returns None. + def func(): + line_stop_tracing = True + lineno = 2 + lineno = 3 + + func.events = [(0, 'call'), + (1, 'line'), + (2, 'line')] + + self.run_test(func) + + def test_18_call_stop_tracing(self): + # issue 20040: tracing not disabled in generator when the system trace + # function returns None. + def func(): + it = gen() + next(it) + next(it) + sys.settrace(None) + + def gen(): + lineno = 7 + call_stop_tracing = True + yield call_stop_tracing + yield call_stop_tracing + + func.events = [(0, 'call'), + (1, 'line'), + (2, 'line'), + (6, 'call'), + (7, 'line'), + (8, 'line'), + (9, 'line'), + (9, 'return'), + (3, 'line'), + (9, 'call'), + (4, 'line')] + + self.run_test(func) + + def test_19_none_f_trace(self): + # issue 20041: TypeError when f_trace is None. + def func(): + sys._getframe().f_trace = None + lineno = 2 + + func.events = [(0, 'call'), + (1, 'line')] + + self.run_test(func) + class RaisingTraceFuncTestCase(unittest.TestCase): def setUp(self):