Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 31, 2025

Conversation

xuantengh
Copy link
Contributor

@xuantengh xuantengh commented Jul 30, 2025

@xuantengh
Copy link
Contributor Author

I want to add a test by setting frame.f_lineno in a callback function with sys.monitoring.register_callback. But as the callback is invoked by the vector calling convention, the frame is owned by thread (not interpreter), which makes WITHIN_STACK_BOUNDS fail in setattr with --with-pydebug build.

@xuantengh
Copy link
Contributor Author

xuantengh commented Jul 31, 2025

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
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
Member

No need to do that in this PR though.

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

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
Copy link

bedevere-app bot commented Jul 31, 2025

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
Copy link
Contributor Author

xuantengh commented Jul 31, 2025

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

while (start_stack > best_stack) {
_PyStackRef popped = _PyFrame_StackPop(self->f_frame);
if (top_of_stack(start_stack) == Except) {

@xuantengh xuantengh deleted the branch-event branch July 31, 2025 13:57
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy