Skip to content

Commit 51b365b

Browse files
committed
test: cover STOP_ITERATION event
1 parent b1629aa commit 51b365b

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

.agents/tasks/2025/08/20-0906-tracer-trait2

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ Currently in print_tracer.rs we have only one test for the CALL event. We want t
44
--- FOLLOW UP TASK ---
55
Write tests for the complete Tracer trait\nCurrently in print_tracer.rs we have only one test for the CALL event. We want tests for all other events. The test must verify that the corresponding event was raised and handled at least once.
66
--- FOLLOW UP TASK ---
7-
For each handler in CountingTracer also print the line at which the event fired. Refer to the sys.monitoring API and specifically to the CodeObject type for details.
7+
For each handler in CountingTracer also print the line at which the event fired. Refer to the sys.monitoring API and specifically to the CodeObject type for details.
8+
--- FOLLOW UP TASK ---
9+
A test is missing for the STOP_ITERATION event

codetracer-python-recorder/tests/print_tracer.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ static PY_UNWIND_COUNT: AtomicUsize = AtomicUsize::new(0);
5858
static RAISE_COUNT: AtomicUsize = AtomicUsize::new(0);
5959
static RERAISE_COUNT: AtomicUsize = AtomicUsize::new(0);
6060
static EXCEPTION_HANDLED_COUNT: AtomicUsize = AtomicUsize::new(0);
61+
static STOP_ITERATION_COUNT: AtomicUsize = AtomicUsize::new(0);
6162
static C_RETURN_COUNT: AtomicUsize = AtomicUsize::new(0);
6263
static C_RAISE_COUNT: AtomicUsize = AtomicUsize::new(0);
6364

@@ -93,6 +94,7 @@ impl Tracer for CountingTracer {
9394
events.PY_YIELD,
9495
events.PY_THROW,
9596
events.PY_UNWIND,
97+
events.STOP_ITERATION,
9698
events.RAISE,
9799
events.RERAISE,
98100
events.EXCEPTION_HANDLED,
@@ -264,6 +266,19 @@ impl Tracer for CountingTracer {
264266
}
265267
}
266268

269+
fn on_stop_iteration(
270+
&mut self,
271+
_py: Python<'_>,
272+
code: &pyo3::Bound<'_, pyo3::types::PyAny>,
273+
offset: i32,
274+
_exception: &pyo3::Bound<'_, pyo3::types::PyAny>,
275+
) {
276+
STOP_ITERATION_COUNT.fetch_add(1, Ordering::SeqCst);
277+
if let Some(line) = offset_to_line(code, offset) {
278+
println!("STOP_ITERATION at {}", line);
279+
}
280+
}
281+
267282
fn on_c_return(
268283
&mut self,
269284
_py: Python<'_>,
@@ -309,6 +324,7 @@ fn tracer_handles_all_events() {
309324
RAISE_COUNT.store(0, Ordering::SeqCst);
310325
RERAISE_COUNT.store(0, Ordering::SeqCst);
311326
EXCEPTION_HANDLED_COUNT.store(0, Ordering::SeqCst);
327+
STOP_ITERATION_COUNT.store(0, Ordering::SeqCst);
312328
C_RETURN_COUNT.store(0, Ordering::SeqCst);
313329
C_RAISE_COUNT.store(0, Ordering::SeqCst);
314330
if let Err(e) = install_tracer(py, Box::new(CountingTracer)) {
@@ -344,6 +360,10 @@ def test_all():
344360
pass
345361
for _ in []:
346362
pass
363+
def gen2():
364+
yield 1
365+
for _ in gen2():
366+
pass
347367
len("abc")
348368
try:
349369
int("a")
@@ -363,6 +383,11 @@ def test_all():
363383
except OSError:
364384
pass
365385
test_all()
386+
def only_stop_iter():
387+
if False:
388+
yield
389+
for _ in only_stop_iter():
390+
pass
366391
"#).expect("CString::new failed");
367392
if let Err(e) = py.run(code.as_c_str(), None, None) {
368393
e.print(py);
@@ -383,6 +408,7 @@ test_all()
383408
assert!(RAISE_COUNT.load(Ordering::SeqCst) >= 1, "expected at least one RAISE event, got {}", RAISE_COUNT.load(Ordering::SeqCst));
384409
assert!(RERAISE_COUNT.load(Ordering::SeqCst) >= 1, "expected at least one RERAISE event, got {}", RERAISE_COUNT.load(Ordering::SeqCst));
385410
assert!(EXCEPTION_HANDLED_COUNT.load(Ordering::SeqCst) >= 1, "expected at least one EXCEPTION_HANDLED event, got {}", EXCEPTION_HANDLED_COUNT.load(Ordering::SeqCst));
411+
assert!(STOP_ITERATION_COUNT.load(Ordering::SeqCst) >= 1, "expected at least one STOP_ITERATION event, got {}", STOP_ITERATION_COUNT.load(Ordering::SeqCst));
386412
assert!(C_RETURN_COUNT.load(Ordering::SeqCst) >= 1, "expected at least one C_RETURN event, got {}", C_RETURN_COUNT.load(Ordering::SeqCst));
387413
assert!(C_RAISE_COUNT.load(Ordering::SeqCst) >= 1, "expected at least one C_RAISE event, got {}", C_RAISE_COUNT.load(Ordering::SeqCst));
388414
});

0 commit comments

Comments
 (0)