Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 49c108c

Browse files
Fix constant folding optimization for positional only arguments (GH-17837)
(cherry picked from commit b121a4a) Co-authored-by: Anthony Sottile <[email protected]>
1 parent 636a850 commit 49c108c

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

Lib/test/test_positional_only_arg.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Unit tests for the positional only argument syntax specified in PEP 570."""
22

3+
import dis
34
import pickle
45
import unittest
56

@@ -419,6 +420,17 @@ def method(self, /):
419420
def test_annotations(self):
420421
assert global_inner_has_pos_only().__annotations__ == {'x': int}
421422

423+
def test_annotations_constant_fold(self):
424+
def g():
425+
def f(x: not (int is int), /): ...
426+
427+
# without constant folding we end up with
428+
# COMPARE_OP(is), UNARY_NOT
429+
# with constant folding we should expect a COMPARE_OP(is not)
430+
codes = [(i.opname, i.argval) for i in dis.get_instructions(g)]
431+
self.assertNotIn(('UNARY_NOT', None), codes)
432+
self.assertIn(('COMPARE_OP', 'is not'), codes)
433+
422434

423435
if __name__ == "__main__":
424436
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix constant folding optimization for positional only arguments - by Anthony
2+
Sottile.

Python/ast_opt.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ astfold_comprehension(comprehension_ty node_, PyArena *ctx_, int optimize_)
617617
static int
618618
astfold_arguments(arguments_ty node_, PyArena *ctx_, int optimize_)
619619
{
620+
CALL_SEQ(astfold_arg, arg_ty, node_->posonlyargs);
620621
CALL_SEQ(astfold_arg, arg_ty, node_->args);
621622
CALL_OPT(astfold_arg, arg_ty, node_->vararg);
622623
CALL_SEQ(astfold_arg, arg_ty, node_->kwonlyargs);

0 commit comments

Comments
 (0)