Skip to content

Commit aa76a09

Browse files
committed
ISSUE-003: Add failing test for fail-fast on frame access
Add pytest `test_fail_fast_when_frame_access_fails` that monkeypatches `sys._getframe` to raise during `PY_START`. It asserts the runtime tracer propagates a Python exception instead of silently swallowing the failure. This currently fails due to the defensive fallback in `on_py_start`.
1 parent 25662c0 commit aa76a09

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import runpy
2+
import sys
3+
from pathlib import Path
4+
5+
import pytest
6+
7+
8+
def test_fail_fast_when_frame_access_fails(tmp_path: Path):
9+
# Import the built extension module
10+
import codetracer_python_recorder as cpr
11+
12+
# Prepare a simple program that triggers a Python function call
13+
prog = tmp_path / "prog.py"
14+
prog.write_text(
15+
"""
16+
def f():
17+
return 1
18+
19+
f()
20+
"""
21+
)
22+
23+
# Monkeypatch sys._getframe to simulate a failure when capturing args
24+
original_getframe = getattr(sys, "_getframe")
25+
26+
def boom(*_args, **_kwargs): # pragma: no cover - intentionally fails
27+
raise RuntimeError("boom: _getframe disabled")
28+
29+
sys._getframe = boom # type: ignore[attr-defined]
30+
31+
try:
32+
# Start tracing; activate only for our program path so stray imports don't trigger
33+
cpr.start_tracing(str(tmp_path), "json", activation_path=str(prog))
34+
35+
with pytest.raises(RuntimeError) as excinfo:
36+
runpy.run_path(str(prog), run_name="__main__")
37+
38+
# Ensure the error surfaced clearly and didn’t get swallowed
39+
assert "_getframe" in str(excinfo.value) or "boom" in str(excinfo.value)
40+
finally:
41+
# Restore state
42+
sys._getframe = original_getframe # type: ignore[attr-defined]
43+
cpr.stop_tracing()
44+

0 commit comments

Comments
 (0)