gh-153354: Fix bool conversion in generated __annotate__ - #153360
gh-153354: Fix bool conversion in generated __annotate__#153360BHUVANSH855 wants to merge 2 commits into
Conversation
|
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 |
361e81b to
de184ba
Compare
|
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 |
sobolevn
left a comment
There was a problem hiding this comment.
Please, don't forget a NEWS entry.
de184ba to
ad3708a
Compare
| pass | ||
|
|
||
| self.assertEqual( | ||
| func.__annotate__(NonBool()), |
There was a problem hiding this comment.
The doc says that the arg to __annotate__ should be a member of the [annotationlib.Format](https://docs.python.org/3/library/annotationlib.html#annotationlib.Format) enum, or an integer with a value corresponding to a member of the enum.
Is NonBool() here even valid?
There was a problem hiding this comment.
There was a problem hiding this comment.
It's not valid in that sense, but that doesn't give us a license to crash if an incorrect value is passed.
Summary
Fix the compiler-generated
__annotate__guard by emittingTO_BOOLafterCOMPARE_OP, allowing the optimizer to generate the bool-convertingCOMPARE_OPbeforePOP_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
TO_BOOLafter the generatedCOMPARE_OPincodegen_setup_annotations_scope(), allowing the optimizer to fold it into a bool-convertingCOMPARE_OP.__annotate__functions for functions, classes, and modules.Issue
Fixes gh-153354.
Testing
COMPARE_OP (bool(>)), matching normal conditional comparison code generation after optimization.