gh-137200: support frame lineno setter with `BRANCH_LEFT` and `BRANCH_RIGHT` events by xuantengh · Pull Request #137229 · python/cpython · GitHub
Skip to content

gh-137200: support frame lineno setter with BRANCH_LEFT and BRANCH_RIGHT events - #137229

Merged
markshannon merged 5 commits into
python:mainfrom
xuantengh:branch-event
Jul 31, 2025
Merged

markshannon merged 5 commits into
python:mainfrom
xuantengh:branch-event

Conversation

@xuantengh

@xuantengh xuantengh commented Jul 30, 2025

Copy link
Copy Markdown
Contributor

@xuantengh

Copy link
Copy Markdown
Contributor Author

Comment thread Lib/test/test_monitoring.py Outdated
@xuantengh

xuantengh commented Jul 31, 2025

Copy link
Copy Markdown
Contributor Author

We should probably raise an exception if we try to set the line number of the currently executing frame.

Yeah, and I try to raise a RuntimeError in frame.f_lineno setter. But the interpreter cannot even reach there as it fails at the STORE_ATTR bytecode. So maybe we should have a document suggesting users not to setattr on current frame (i.e., the callback frame itself) in event callback?

assert(WITHIN_STACK_BOUNDS());

@markshannon

Copy link
Copy Markdown
Member

We should probably raise an exception if we try to set the line number of the currently executing frame.

Yeah, and I try to raise a RuntimeError in frame.f_lineno setter. But the interpreter cannot even reach there as it fails at the STORE_ATTR bytecode. So maybe we should have a document suggesting users not to setattr on current frame (i.e., the callback frame itself) in event callback?

frame_lineno_set_impl should not modify the stack if it raises, so the assertion should pass.

@markshannon

Copy link
Copy Markdown
Member

No need to do that in this PR though.

@markshannon
markshannon merged commit d18f73a into python:main Jul 31, 2025
44 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @xuantengh for the PR, and @markshannon for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 31, 2025
…BRANCH_RIGHT` events (pythonGH-137229)

(cherry picked from commit d18f73a)

Co-authored-by: Xuanteng Huang <44627253+xuantengh@users.noreply.github.com>
@bedevere-app

bedevere-app Bot commented Jul 31, 2025

Copy link
Copy Markdown

GH-137280 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jul 31, 2025
@xuantengh

xuantengh commented Jul 31, 2025

Copy link
Copy Markdown
Contributor Author

Sorry for the late reply, I just found the lineno setter may modify the frame stack of the function being monitored, so the frame stack pointer may be altered after the callback, which makes the WITHIN_STACK_BOUNDS checks fail in some bytecodes. But as you said, we may need to do it in another PR.

cpython/Objects/frameobject.c

Lines 1831 to 1833 in d18f73a

@xuantengh
xuantengh deleted the branch-event branch July 31, 2025 13:57
hugovk pushed a commit that referenced this pull request Aug 11, 2025
…`BRANCH_RIGHT` events (GH-137229) (#137280)

Co-authored-by: Xuanteng Huang <44627253+xuantengh@users.noreply.github.com>
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
kumaraditya303 pushed a commit to miss-islington/cpython that referenced this pull request Sep 9, 2025
…` and `BRANCH_RIGHT` events (pythonGH-137229) (python#137280)

Co-authored-by: Xuanteng Huang <44627253+xuantengh@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants