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

Copy link
Copy Markdown

Summary

Fix the compiler-generated __annotate__ guard to emit a COMPARE_OP with the bool-conversion flag set before POP_JUMP_IF_FALSE.

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

Changes

  • Set the bool-conversion flag when emitting the COMPARE_OP in codegen_setup_annotations_scope().
  • Added a regression test covering a compiler-generated __annotate__ function invoked with an object whose __gt__ returns a non-boolean value.

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.
  • 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.

pass

self.assertEqual(
func.__annotate__(NonBool()),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please, test other types of __annotate__ functions as well: all must work correctly, not only per-function one.

Comment thread Python/codegen.c
ADDOP_I(c, loc, LOAD_FAST, 0);
ADDOP_LOAD_CONST_NEW(c, loc, value_with_fake_globals);
ADDOP_I(c, loc, COMPARE_OP, (Py_GT << 5) | compare_masks[Py_GT]);
ADDOP_I(c, loc, COMPARE_OP,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Question: maybe we should just add ADDOP(c, loc, TO_BOOL); and let the optimizer do its job?

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