@@ -197,6 +197,7 @@ static int compiler_visit_slice(struct compiler *, slice_ty,
197197 expr_context_ty );
198198
199199static int inplace_binop (struct compiler * , operator_ty );
200+ static int are_all_items_const (asdl_seq * , Py_ssize_t , Py_ssize_t );
200201static int expr_constant (expr_ty );
201202
202203static int compiler_with (struct compiler * , stmt_ty , int );
@@ -3655,6 +3656,27 @@ starunpack_helper(struct compiler *c, asdl_seq *elts,
36553656{
36563657 Py_ssize_t n = asdl_seq_LEN (elts );
36573658 Py_ssize_t i , nsubitems = 0 , nseen = 0 ;
3659+ if (n > 2 && are_all_items_const (elts , 0 , n )) {
3660+ PyObject * folded = PyTuple_New (n );
3661+ if (folded == NULL ) {
3662+ return 0 ;
3663+ }
3664+ PyObject * val ;
3665+ for (i = 0 ; i < n ; i ++ ) {
3666+ val = ((expr_ty )asdl_seq_GET (elts , i ))-> v .Constant .value ;
3667+ Py_INCREF (val );
3668+ PyTuple_SET_ITEM (folded , i , val );
3669+ }
3670+ if (outer_op == BUILD_SET_UNPACK ) {
3671+ Py_SETREF (folded , PyFrozenSet_New (folded ));
3672+ if (folded == NULL ) {
3673+ return 0 ;
3674+ }
3675+ }
3676+ ADDOP_LOAD_CONST_NEW (c , folded );
3677+ ADDOP_I (c , outer_op , 1 );
3678+ return 1 ;
3679+ }
36583680 for (i = 0 ; i < n ; i ++ ) {
36593681 expr_ty elt = asdl_seq_GET (elts , i );
36603682 if (elt -> kind == Starred_kind ) {
0 commit comments