summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_trace.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_trace.py')
-rw-r--r--Lib/test/test_trace.py49
1 files changed, 47 insertions, 2 deletions
diff --git a/Lib/test/test_trace.py b/Lib/test/test_trace.py
index f67da77..3ec00d2 100644
--- a/Lib/test/test_trace.py
+++ b/Lib/test/test_trace.py
@@ -204,12 +204,44 @@ tighterloop_example.events = [(0, 'call'),
(6, 'line'),
(6, 'return')]
+def generator_function():
+ try:
+ yield True
+ "continued"
+ finally:
+ "finally"
+def generator_example():
+ # any() will leave the generator before its end
+ x = any(generator_function())
+
+ # the following lines were not traced
+ for x in range(10):
+ y = x
+
+generator_example.events = ([(0, 'call'),
+ (2, 'line'),
+ (-6, 'call'),
+ (-5, 'line'),
+ (-4, 'line'),
+ (-4, 'return'),
+ (-4, 'call'),
+ (-4, 'exception'),
+ (-1, 'line'),
+ (-1, 'return')] +
+ [(5, 'line'), (6, 'line')] * 10 +
+ [(5, 'line'), (5, 'return')])
+
+
class Tracer:
def __init__(self):
self.events = []
def trace(self, frame, event, arg):
self.events.append((frame.f_lineno, event))
return self.trace
+ def traceWithGenexp(self, frame, event, arg):
+ (o for o in [1])
+ self.events.append((frame.f_lineno, event))
+ return self.trace
class TraceTestCase(unittest.TestCase):
def compare_events(self, line_offset, events, expected_events):
@@ -217,8 +249,8 @@ class TraceTestCase(unittest.TestCase):
if events != expected_events:
self.fail(
"events did not match expectation:\n" +
- "\n".join(difflib.ndiff(map(str, expected_events),
- map(str, events))))
+ "\n".join(difflib.ndiff([str(x) for x in expected_events],
+ [str(x) for x in events])))
def run_test(self, func):
@@ -262,6 +294,19 @@ class TraceTestCase(unittest.TestCase):
def test_12_tighterloop(self):
self.run_test(tighterloop_example)
+ def test_13_genexp(self):
+ self.run_test(generator_example)
+ # issue1265: if the trace function contains a generator,
+ # and if the traced function contains another generator
+ # that is not completely exhausted, the trace stopped.
+ # Worse: the 'finally' clause was not invoked.
+ tracer = Tracer()
+ sys.settrace(tracer.traceWithGenexp)
+ generator_example()
+ sys.settrace(None)
+ self.compare_events(generator_example.__code__.co_firstlineno,
+ tracer.events, generator_example.events)
+
class RaisingTraceFuncTestCase(unittest.TestCase):
def trace(self, frame, event, arg):
"""A trace function that raises an exception in response to a