@@ -369,7 +369,8 @@ fold_subscr(expr_ty node, PyArena *arena, int optimize)
369369}
370370
371371/* Change literal list or set of constants into constant
372- tuple or frozenset respectively.
372+ tuple or frozenset respectively. Change literal list of
373+ non-constants into tuple.
373374 Used for right operand of "in" and "not in" tests and for iterable
374375 in "for" loop and comprehensions.
375376*/
@@ -378,7 +379,21 @@ fold_iter(expr_ty arg, PyArena *arena, int optimize)
378379{
379380 PyObject * newval ;
380381 if (arg -> kind == List_kind ) {
381- newval = make_const_tuple (arg -> v .List .elts );
382+ /* First change a list into tuple. */
383+ asdl_seq * elts = arg -> v .List .elts ;
384+ Py_ssize_t n = asdl_seq_LEN (elts );
385+ for (Py_ssize_t i = 0 ; i < n ; i ++ ) {
386+ expr_ty e = (expr_ty )asdl_seq_GET (elts , i );
387+ if (e -> kind == Starred_kind ) {
388+ return 1 ;
389+ }
390+ }
391+ expr_context_ty ctx = arg -> v .List .ctx ;
392+ arg -> kind = Tuple_kind ;
393+ arg -> v .Tuple .elts = elts ;
394+ arg -> v .Tuple .ctx = ctx ;
395+ /* Try to create a constant tuple. */
396+ newval = make_const_tuple (elts );
382397 }
383398 else if (arg -> kind == Set_kind ) {
384399 newval = make_const_tuple (arg -> v .Set .elts );
0 commit comments