Skip to content

Commit

Permalink
Fix constant folding optimization for positional only arguments (pyth…
Browse files Browse the repository at this point in the history
…onGH-17837)

(cherry picked from commit b121a4a)

Co-authored-by: Anthony Sottile <[email protected]>
  • Loading branch information
asottile authored and miss-islington committed Jan 5, 2020
1 parent 34aa3e7 commit ad1e7a6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Lib/test/test_positional_only_arg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Unit tests for the positional only argument syntax specified in PEP 570."""

import dis
import pickle
import unittest

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

def test_annotations_constant_fold(self):
def g():
def f(x: not (int is int), /): ...

# without constant folding we end up with
# COMPARE_OP(is), UNARY_NOT
# with constant folding we should expect a COMPARE_OP(is not)
codes = [(i.opname, i.argval) for i in dis.get_instructions(g)]
self.assertNotIn(('UNARY_NOT', None), codes)
self.assertIn(('COMPARE_OP', 'is not'), codes)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix constant folding optimization for positional only arguments - by Anthony
Sottile.
1 change: 1 addition & 0 deletions Python/ast_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ astfold_comprehension(comprehension_ty node_, PyArena *ctx_, int optimize_)
static int
astfold_arguments(arguments_ty node_, PyArena *ctx_, int optimize_)
{
CALL_SEQ(astfold_arg, arg_ty, node_->posonlyargs);
CALL_SEQ(astfold_arg, arg_ty, node_->args);
CALL_OPT(astfold_arg, arg_ty, node_->vararg);
CALL_SEQ(astfold_arg, arg_ty, node_->kwonlyargs);
Expand Down

0 comments on commit ad1e7a6

Please sign in to comment.