FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-153354: Fix bool conversion in generated __annotate__ by BHUVANSH855 · Pull Request #153360 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
31 changes: 31 additions & 0 deletions Lib/test/test_annotationlib.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,37 @@ def annotate(format, /):
with self.assertRaises(DemoException):
annotationlib.call_annotate_function(annotate, format=fmt)

def test_generated_annotate_non_bool_compare_result(self):
class NonBool(int):
def __gt__(self, other):
return None

# Function __annotate__
def func(x: int):
pass

self.assertEqual(
func.__annotate__(NonBool(1)),
{"x": int},
)

# Class __annotate__
class C:
y: int

self.assertEqual(
C.__annotate__(NonBool(1)),
{"y": int},
)

# Module __annotate__
ns = {}
exec("z: int", ns)

self.assertEqual(
ns["__annotate__"](NonBool(1)),
{"z": int},
)

class MetaclassTests(unittest.TestCase):
def test_annotated_meta(self):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in compiler-generated ``__annotate__`` functions when the
annotation format comparison returned a non-boolean result.
1 change: 1 addition & 0 deletions Python/codegen.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ codegen_setup_annotations_scope(compiler *c, location loc,
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(c, loc, TO_BOOL);
NEW_JUMP_TARGET_LABEL(c, body);
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, body);
ADDOP_I(c, loc, LOAD_COMMON_CONSTANT, CONSTANT_NOTIMPLEMENTEDERROR);
Expand Down
Loading

Back | FazBrowse Home | New Git URL