Skip to content

gh-153354: Fix bool conversion in generated __annotate__#153360

Open
BHUVANSH855 wants to merge 1 commit into
python:mainfrom
BHUVANSH855:fix-153354-annotate-compare-op
Open

gh-153354: Fix bool conversion in generated __annotate__#153360
BHUVANSH855 wants to merge 1 commit into
python:mainfrom
BHUVANSH855:fix-153354-annotate-compare-op

Conversation

@BHUVANSH855

@BHUVANSH855 BHUVANSH855 commented Jul 8, 2026

Copy link
Copy Markdown

Summary

Fix the compiler-generated __annotate__ guard by emitting TO_BOOL after COMPARE_OP, allowing the optimizer to generate the bool-converting COMPARE_OP before POP_JUMP_IF_FALSE.

Previously, the generated bytecode omitted the bool conversion required before POP_JUMP_IF_FALSE, allowing a non-boolean result from __gt__ to reach the jump. On debug builds this triggered an assertion failure, while release builds could exhibit incorrect behavior.

Changes

  • Emit TO_BOOL after the generated COMPARE_OP in codegen_setup_annotations_scope(), allowing the optimizer to fold it into a bool-converting COMPARE_OP.
  • Expand the regression test to cover compiler-generated __annotate__ functions for functions, classes, and modules.

Issue

Fixes gh-153354.

Testing

  • Built CPython (Debug, x64).
  • Verified that the original reproducer no longer crashes.
  • Verified that the generated bytecode now emits COMPARE_OP (bool(>)), matching normal conditional comparison code generation after optimization.
  • Ran the following test suites successfully:
PCbuild\amd64\python_d.exe -m test test_annotationlib
PCbuild\amd64\python_d.exe -m test test_compile

@bedevere-app

bedevere-app Bot commented Jul 8, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Comment thread Lib/test/test_annotationlib.py
Comment thread Python/codegen.c Outdated
@BHUVANSH855 BHUVANSH855 force-pushed the fix-153354-annotate-compare-op branch from 361e81b to de184ba Compare July 9, 2026 02:06
@bedevere-app

bedevere-app Bot commented Jul 9, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

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.

Generated __annotate__ uses COMPARE_OP without the bool-conversion flag, feeding a non-bool to POP_JUMP_IF_FALSE

2 participants