@@ -35,7 +35,7 @@ unary_not(PyObject *v)
3535}
3636
3737static int
38- fold_unaryop (expr_ty node , PyArena * arena , int optimize )
38+ fold_unaryop (expr_ty node , PyArena * arena , _PyASTOptimizeState * state )
3939{
4040 expr_ty arg = node -> v .UnaryOp .operand ;
4141
@@ -212,7 +212,7 @@ safe_mod(PyObject *v, PyObject *w)
212212}
213213
214214static int
215- fold_binop (expr_ty node , PyArena * arena , int optimize )
215+ fold_binop (expr_ty node , PyArena * arena , _PyASTOptimizeState * state )
216216{
217217 expr_ty lhs , rhs ;
218218 lhs = node -> v .BinOp .left ;
@@ -294,7 +294,7 @@ make_const_tuple(asdl_seq *elts)
294294}
295295
296296static int
297- fold_tuple (expr_ty node , PyArena * arena , int optimize )
297+ fold_tuple (expr_ty node , PyArena * arena , _PyASTOptimizeState * state )
298298{
299299 PyObject * newval ;
300300
@@ -306,7 +306,7 @@ fold_tuple(expr_ty node, PyArena *arena, int optimize)
306306}
307307
308308static int
309- fold_subscr (expr_ty node , PyArena * arena , int optimize )
309+ fold_subscr (expr_ty node , PyArena * arena , _PyASTOptimizeState * state )
310310{
311311 PyObject * newval ;
312312 expr_ty arg , idx ;
@@ -331,7 +331,7 @@ fold_subscr(expr_ty node, PyArena *arena, int optimize)
331331 in "for" loop and comprehensions.
332332*/
333333static int
334- fold_iter (expr_ty arg , PyArena * arena , int optimize )
334+ fold_iter (expr_ty arg , PyArena * arena , _PyASTOptimizeState * state )
335335{
336336 PyObject * newval ;
337337 if (arg -> kind == List_kind ) {
@@ -364,7 +364,7 @@ fold_iter(expr_ty arg, PyArena *arena, int optimize)
364364}
365365
366366static int
367- fold_compare (expr_ty node , PyArena * arena , int optimize )
367+ fold_compare (expr_ty node , PyArena * arena , _PyASTOptimizeState * state )
368368{
369369 asdl_int_seq * ops ;
370370 asdl_seq * args ;
@@ -378,36 +378,36 @@ fold_compare(expr_ty node, PyArena *arena, int optimize)
378378 i = asdl_seq_LEN (ops ) - 1 ;
379379 int op = asdl_seq_GET (ops , i );
380380 if (op == In || op == NotIn ) {
381- if (!fold_iter ((expr_ty )asdl_seq_GET (args , i ), arena , optimize )) {
381+ if (!fold_iter ((expr_ty )asdl_seq_GET (args , i ), arena , state )) {
382382 return 0 ;
383383 }
384384 }
385385 return 1 ;
386386}
387387
388- static int astfold_mod (mod_ty node_ , PyArena * ctx_ , int optimize_ );
389- static int astfold_stmt (stmt_ty node_ , PyArena * ctx_ , int optimize_ );
390- static int astfold_expr (expr_ty node_ , PyArena * ctx_ , int optimize_ );
391- static int astfold_arguments (arguments_ty node_ , PyArena * ctx_ , int optimize_ );
392- static int astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , int optimize_ );
393- static int astfold_keyword (keyword_ty node_ , PyArena * ctx_ , int optimize_ );
394- static int astfold_arg (arg_ty node_ , PyArena * ctx_ , int optimize_ );
395- static int astfold_withitem (withitem_ty node_ , PyArena * ctx_ , int optimize_ );
396- static int astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , int optimize_ );
388+ static int astfold_mod (mod_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
389+ static int astfold_stmt (stmt_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
390+ static int astfold_expr (expr_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
391+ static int astfold_arguments (arguments_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
392+ static int astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
393+ static int astfold_keyword (keyword_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
394+ static int astfold_arg (arg_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
395+ static int astfold_withitem (withitem_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
396+ static int astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
397397#define CALL (FUNC , TYPE , ARG ) \
398- if (!FUNC((ARG), ctx_, optimize_ )) \
398+ if (!FUNC((ARG), ctx_, state )) \
399399 return 0;
400400
401401#define CALL_OPT (FUNC , TYPE , ARG ) \
402- if ((ARG) != NULL && !FUNC((ARG), ctx_, optimize_ )) \
402+ if ((ARG) != NULL && !FUNC((ARG), ctx_, state )) \
403403 return 0;
404404
405405#define CALL_SEQ (FUNC , TYPE , ARG ) { \
406406 int i; \
407407 asdl_seq *seq = (ARG); /* avoid variable capture */ \
408408 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
409409 TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
410- if (elt != NULL && !FUNC(elt, ctx_, optimize_ )) \
410+ if (elt != NULL && !FUNC(elt, ctx_, state )) \
411411 return 0; \
412412 } \
413413}
@@ -417,13 +417,13 @@ static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, int opti
417417 asdl_int_seq *seq = (ARG); /* avoid variable capture */ \
418418 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
419419 TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
420- if (!FUNC(elt, ctx_, optimize_ )) \
420+ if (!FUNC(elt, ctx_, state )) \
421421 return 0; \
422422 } \
423423}
424424
425425static int
426- astfold_body (asdl_seq * stmts , PyArena * ctx_ , int optimize_ )
426+ astfold_body (asdl_seq * stmts , PyArena * ctx_ , _PyASTOptimizeState * state )
427427{
428428 int docstring = _PyAST_GetDocString (stmts ) != NULL ;
429429 CALL_SEQ (astfold_stmt , stmt_ty , stmts );
@@ -445,7 +445,7 @@ astfold_body(asdl_seq *stmts, PyArena *ctx_, int optimize_)
445445}
446446
447447static int
448- astfold_mod (mod_ty node_ , PyArena * ctx_ , int optimize_ )
448+ astfold_mod (mod_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
449449{
450450 switch (node_ -> kind ) {
451451 case Module_kind :
@@ -464,7 +464,7 @@ astfold_mod(mod_ty node_, PyArena *ctx_, int optimize_)
464464}
465465
466466static int
467- astfold_expr (expr_ty node_ , PyArena * ctx_ , int optimize_ )
467+ astfold_expr (expr_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
468468{
469469 switch (node_ -> kind ) {
470470 case BoolOp_kind :
@@ -563,7 +563,7 @@ astfold_expr(expr_ty node_, PyArena *ctx_, int optimize_)
563563 break ;
564564 case Name_kind :
565565 if (_PyUnicode_EqualToASCIIString (node_ -> v .Name .id , "__debug__" )) {
566- return make_const (node_ , PyBool_FromLong (!optimize_ ), ctx_ );
566+ return make_const (node_ , PyBool_FromLong (!state -> optimize ), ctx_ );
567567 }
568568 break ;
569569 default :
@@ -573,14 +573,14 @@ astfold_expr(expr_ty node_, PyArena *ctx_, int optimize_)
573573}
574574
575575static int
576- astfold_keyword (keyword_ty node_ , PyArena * ctx_ , int optimize_ )
576+ astfold_keyword (keyword_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
577577{
578578 CALL (astfold_expr , expr_ty , node_ -> value );
579579 return 1 ;
580580}
581581
582582static int
583- astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , int optimize_ )
583+ astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
584584{
585585 CALL (astfold_expr , expr_ty , node_ -> target );
586586 CALL (astfold_expr , expr_ty , node_ -> iter );
@@ -591,7 +591,7 @@ astfold_comprehension(comprehension_ty node_, PyArena *ctx_, int optimize_)
591591}
592592
593593static int
594- astfold_arguments (arguments_ty node_ , PyArena * ctx_ , int optimize_ )
594+ astfold_arguments (arguments_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
595595{
596596 CALL_SEQ (astfold_arg , arg_ty , node_ -> posonlyargs );
597597 CALL_SEQ (astfold_arg , arg_ty , node_ -> args );
@@ -604,27 +604,33 @@ astfold_arguments(arguments_ty node_, PyArena *ctx_, int optimize_)
604604}
605605
606606static int
607- astfold_arg (arg_ty node_ , PyArena * ctx_ , int optimize_ )
607+ astfold_arg (arg_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
608608{
609- CALL_OPT (astfold_expr , expr_ty , node_ -> annotation );
609+ if (!(state -> ff_features & CO_FUTURE_ANNOTATIONS )) {
610+ CALL_OPT (astfold_expr , expr_ty , node_ -> annotation );
611+ }
610612 return 1 ;
611613}
612614
613615static int
614- astfold_stmt (stmt_ty node_ , PyArena * ctx_ , int optimize_ )
616+ astfold_stmt (stmt_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
615617{
616618 switch (node_ -> kind ) {
617619 case FunctionDef_kind :
618620 CALL (astfold_arguments , arguments_ty , node_ -> v .FunctionDef .args );
619621 CALL (astfold_body , asdl_seq , node_ -> v .FunctionDef .body );
620622 CALL_SEQ (astfold_expr , expr_ty , node_ -> v .FunctionDef .decorator_list );
621- CALL_OPT (astfold_expr , expr_ty , node_ -> v .FunctionDef .returns );
623+ if (!(state -> ff_features & CO_FUTURE_ANNOTATIONS )) {
624+ CALL_OPT (astfold_expr , expr_ty , node_ -> v .FunctionDef .returns );
625+ }
622626 break ;
623627 case AsyncFunctionDef_kind :
624628 CALL (astfold_arguments , arguments_ty , node_ -> v .AsyncFunctionDef .args );
625629 CALL (astfold_body , asdl_seq , node_ -> v .AsyncFunctionDef .body );
626630 CALL_SEQ (astfold_expr , expr_ty , node_ -> v .AsyncFunctionDef .decorator_list );
627- CALL_OPT (astfold_expr , expr_ty , node_ -> v .AsyncFunctionDef .returns );
631+ if (!(state -> ff_features & CO_FUTURE_ANNOTATIONS )) {
632+ CALL_OPT (astfold_expr , expr_ty , node_ -> v .AsyncFunctionDef .returns );
633+ }
628634 break ;
629635 case ClassDef_kind :
630636 CALL_SEQ (astfold_expr , expr_ty , node_ -> v .ClassDef .bases );
@@ -648,7 +654,9 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, int optimize_)
648654 break ;
649655 case AnnAssign_kind :
650656 CALL (astfold_expr , expr_ty , node_ -> v .AnnAssign .target );
651- CALL (astfold_expr , expr_ty , node_ -> v .AnnAssign .annotation );
657+ if (!(state -> ff_features & CO_FUTURE_ANNOTATIONS )) {
658+ CALL (astfold_expr , expr_ty , node_ -> v .AnnAssign .annotation );
659+ }
652660 CALL_OPT (astfold_expr , expr_ty , node_ -> v .AnnAssign .value );
653661 break ;
654662 case For_kind :
@@ -707,7 +715,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, int optimize_)
707715}
708716
709717static int
710- astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , int optimize_ )
718+ astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
711719{
712720 switch (node_ -> kind ) {
713721 case ExceptHandler_kind :
@@ -721,7 +729,7 @@ astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, int optimize_)
721729}
722730
723731static int
724- astfold_withitem (withitem_ty node_ , PyArena * ctx_ , int optimize_ )
732+ astfold_withitem (withitem_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
725733{
726734 CALL (astfold_expr , expr_ty , node_ -> context_expr );
727735 CALL_OPT (astfold_expr , expr_ty , node_ -> optional_vars );
@@ -734,9 +742,9 @@ astfold_withitem(withitem_ty node_, PyArena *ctx_, int optimize_)
734742#undef CALL_INT_SEQ
735743
736744int
737- _PyAST_Optimize (mod_ty mod , PyArena * arena , int optimize )
745+ _PyAST_Optimize (mod_ty mod , PyArena * arena , _PyASTOptimizeState * state )
738746{
739- int ret = astfold_mod (mod , arena , optimize );
747+ int ret = astfold_mod (mod , arena , state );
740748 assert (ret || PyErr_Occurred ());
741749 return ret ;
742750}
0 commit comments