Skip to content

gh-137030: Fix YIELD_VALUE bytecode assertion#149184

Merged
vstinner merged 2 commits intopython:mainfrom
vstinner:for_iter_gen
May 4, 2026
Merged

gh-137030: Fix YIELD_VALUE bytecode assertion#149184
vstinner merged 2 commits intopython:mainfrom
vstinner:for_iter_gen

Conversation

@vstinner
Copy link
Copy Markdown
Member

@vstinner vstinner commented Apr 30, 2026

Expect also INSTRUMENTED_FOR_ITER.
@vstinner
Copy link
Copy Markdown
Member Author

@Fidget-Spinner @markshannon @emmatyping: Would you mind to review this fix?

See #137030 (comment) to reproduce the issue.

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented Apr 30, 2026

Documentation build overview

📚 cpython-previews | 🛠️ Build #32485691 | 📁 Comparing a28cf41 against main (4599335)

  🔍 Preview build  

16 files changed · ± 16 modified

± Modified

Comment thread Python/bytecodes.c Outdated
@@ -1781,6 +1781,7 @@ dummy_func(
#if TIER_ONE
assert(frame->instr_ptr->op.code == INSTRUMENTED_LINE ||
Copy link
Copy Markdown
Member

@markshannon markshannon Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure there's much value in this assert any more.
I'd be fine with removing the whole thing.

If you want to keep the check, this

Suggested change
assert(frame->instr_ptr->op.code == INSTRUMENTED_LINE ||
#if TIER_ONE && defined(Py_DEBUG)
if (!PyStackRef_IsNone(frame->f_executable)) {
int opcode = _Py_GetBaseCodeUnit(_PyFrame_GetCode(frame), frame->instr_ptr - _PyFrame_GetBytecode(frame)).op.code;
assert(opcode == SEND || opcode == FOR_ITER);
}
#endif

is probably a better formulation as it catches errors that would be hidden behind instrumentation.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I updated my PR to use _Py_GetBaseCodeUnit().

Co-Authored-by: Mark Shannon <mark@hotpy.org>
@markshannon
Copy link
Copy Markdown
Member

@emmatyping can you confirm that this fixes the issue?

Copy link
Copy Markdown
Member

@emmatyping emmatyping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm this fixed the crash I found. Thanks for the fix!

@vstinner vstinner merged commit 952784a into python:main May 4, 2026
78 checks passed
@miss-islington-app
Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@vstinner vstinner deleted the for_iter_gen branch May 4, 2026 16:10
@miss-islington-app
Copy link
Copy Markdown

Sorry, @vstinner, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 952784af4793e6f819bfecc6e0a50676c7256e3a 3.14

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 4, 2026

GH-149383 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 May 4, 2026
vstinner added a commit to vstinner/cpython that referenced this pull request May 4, 2026
Co-authored-by: Mark Shannon <mark@hotpy.org>
(cherry picked from commit 952784a)
vstinner added a commit that referenced this pull request May 4, 2026
gh-137030: Fix YIELD_VALUE bytecode assertion (#149184)


(cherry picked from commit 952784a)

Co-authored-by: Mark Shannon <mark@hotpy.org>
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.

3 participants