@@ -35,7 +35,7 @@ unary_not(PyObject *v)
35
35
}
36
36
37
37
static int
38
- fold_unaryop (expr_ty node , PyArena * arena , int optimize )
38
+ fold_unaryop (expr_ty node , PyArena * arena , _PyASTOptimizeState * state )
39
39
{
40
40
expr_ty arg = node -> v .UnaryOp .operand ;
41
41
@@ -212,7 +212,7 @@ safe_mod(PyObject *v, PyObject *w)
212
212
}
213
213
214
214
static int
215
- fold_binop (expr_ty node , PyArena * arena , int optimize )
215
+ fold_binop (expr_ty node , PyArena * arena , _PyASTOptimizeState * state )
216
216
{
217
217
expr_ty lhs , rhs ;
218
218
lhs = node -> v .BinOp .left ;
@@ -294,7 +294,7 @@ make_const_tuple(asdl_seq *elts)
294
294
}
295
295
296
296
static int
297
- fold_tuple (expr_ty node , PyArena * arena , int optimize )
297
+ fold_tuple (expr_ty node , PyArena * arena , _PyASTOptimizeState * state )
298
298
{
299
299
PyObject * newval ;
300
300
@@ -306,7 +306,7 @@ fold_tuple(expr_ty node, PyArena *arena, int optimize)
306
306
}
307
307
308
308
static int
309
- fold_subscr (expr_ty node , PyArena * arena , int optimize )
309
+ fold_subscr (expr_ty node , PyArena * arena , _PyASTOptimizeState * state )
310
310
{
311
311
PyObject * newval ;
312
312
expr_ty arg , idx ;
@@ -331,7 +331,7 @@ fold_subscr(expr_ty node, PyArena *arena, int optimize)
331
331
in "for" loop and comprehensions.
332
332
*/
333
333
static int
334
- fold_iter (expr_ty arg , PyArena * arena , int optimize )
334
+ fold_iter (expr_ty arg , PyArena * arena , _PyASTOptimizeState * state )
335
335
{
336
336
PyObject * newval ;
337
337
if (arg -> kind == List_kind ) {
@@ -364,7 +364,7 @@ fold_iter(expr_ty arg, PyArena *arena, int optimize)
364
364
}
365
365
366
366
static int
367
- fold_compare (expr_ty node , PyArena * arena , int optimize )
367
+ fold_compare (expr_ty node , PyArena * arena , _PyASTOptimizeState * state )
368
368
{
369
369
asdl_int_seq * ops ;
370
370
asdl_seq * args ;
@@ -378,36 +378,36 @@ fold_compare(expr_ty node, PyArena *arena, int optimize)
378
378
i = asdl_seq_LEN (ops ) - 1 ;
379
379
int op = asdl_seq_GET (ops , i );
380
380
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 )) {
382
382
return 0 ;
383
383
}
384
384
}
385
385
return 1 ;
386
386
}
387
387
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 );
397
397
#define CALL (FUNC , TYPE , ARG ) \
398
- if (!FUNC((ARG), ctx_, optimize_ )) \
398
+ if (!FUNC((ARG), ctx_, state )) \
399
399
return 0;
400
400
401
401
#define CALL_OPT (FUNC , TYPE , ARG ) \
402
- if ((ARG) != NULL && !FUNC((ARG), ctx_, optimize_ )) \
402
+ if ((ARG) != NULL && !FUNC((ARG), ctx_, state )) \
403
403
return 0;
404
404
405
405
#define CALL_SEQ (FUNC , TYPE , ARG ) { \
406
406
int i; \
407
407
asdl_seq *seq = (ARG); /* avoid variable capture */ \
408
408
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
409
409
TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
410
- if (elt != NULL && !FUNC(elt, ctx_, optimize_ )) \
410
+ if (elt != NULL && !FUNC(elt, ctx_, state )) \
411
411
return 0; \
412
412
} \
413
413
}
@@ -417,13 +417,13 @@ static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, int opti
417
417
asdl_int_seq *seq = (ARG); /* avoid variable capture */ \
418
418
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
419
419
TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
420
- if (!FUNC(elt, ctx_, optimize_ )) \
420
+ if (!FUNC(elt, ctx_, state )) \
421
421
return 0; \
422
422
} \
423
423
}
424
424
425
425
static int
426
- astfold_body (asdl_seq * stmts , PyArena * ctx_ , int optimize_ )
426
+ astfold_body (asdl_seq * stmts , PyArena * ctx_ , _PyASTOptimizeState * state )
427
427
{
428
428
int docstring = _PyAST_GetDocString (stmts ) != NULL ;
429
429
CALL_SEQ (astfold_stmt , stmt_ty , stmts );
@@ -445,7 +445,7 @@ astfold_body(asdl_seq *stmts, PyArena *ctx_, int optimize_)
445
445
}
446
446
447
447
static int
448
- astfold_mod (mod_ty node_ , PyArena * ctx_ , int optimize_ )
448
+ astfold_mod (mod_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
449
449
{
450
450
switch (node_ -> kind ) {
451
451
case Module_kind :
@@ -464,7 +464,7 @@ astfold_mod(mod_ty node_, PyArena *ctx_, int optimize_)
464
464
}
465
465
466
466
static int
467
- astfold_expr (expr_ty node_ , PyArena * ctx_ , int optimize_ )
467
+ astfold_expr (expr_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
468
468
{
469
469
switch (node_ -> kind ) {
470
470
case BoolOp_kind :
@@ -563,7 +563,7 @@ astfold_expr(expr_ty node_, PyArena *ctx_, int optimize_)
563
563
break ;
564
564
case Name_kind :
565
565
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_ );
567
567
}
568
568
break ;
569
569
default :
@@ -573,14 +573,14 @@ astfold_expr(expr_ty node_, PyArena *ctx_, int optimize_)
573
573
}
574
574
575
575
static int
576
- astfold_keyword (keyword_ty node_ , PyArena * ctx_ , int optimize_ )
576
+ astfold_keyword (keyword_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
577
577
{
578
578
CALL (astfold_expr , expr_ty , node_ -> value );
579
579
return 1 ;
580
580
}
581
581
582
582
static int
583
- astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , int optimize_ )
583
+ astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
584
584
{
585
585
CALL (astfold_expr , expr_ty , node_ -> target );
586
586
CALL (astfold_expr , expr_ty , node_ -> iter );
@@ -591,7 +591,7 @@ astfold_comprehension(comprehension_ty node_, PyArena *ctx_, int optimize_)
591
591
}
592
592
593
593
static int
594
- astfold_arguments (arguments_ty node_ , PyArena * ctx_ , int optimize_ )
594
+ astfold_arguments (arguments_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
595
595
{
596
596
CALL_SEQ (astfold_arg , arg_ty , node_ -> posonlyargs );
597
597
CALL_SEQ (astfold_arg , arg_ty , node_ -> args );
@@ -604,27 +604,33 @@ astfold_arguments(arguments_ty node_, PyArena *ctx_, int optimize_)
604
604
}
605
605
606
606
static int
607
- astfold_arg (arg_ty node_ , PyArena * ctx_ , int optimize_ )
607
+ astfold_arg (arg_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
608
608
{
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
+ }
610
612
return 1 ;
611
613
}
612
614
613
615
static int
614
- astfold_stmt (stmt_ty node_ , PyArena * ctx_ , int optimize_ )
616
+ astfold_stmt (stmt_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
615
617
{
616
618
switch (node_ -> kind ) {
617
619
case FunctionDef_kind :
618
620
CALL (astfold_arguments , arguments_ty , node_ -> v .FunctionDef .args );
619
621
CALL (astfold_body , asdl_seq , node_ -> v .FunctionDef .body );
620
622
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
+ }
622
626
break ;
623
627
case AsyncFunctionDef_kind :
624
628
CALL (astfold_arguments , arguments_ty , node_ -> v .AsyncFunctionDef .args );
625
629
CALL (astfold_body , asdl_seq , node_ -> v .AsyncFunctionDef .body );
626
630
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
+ }
628
634
break ;
629
635
case ClassDef_kind :
630
636
CALL_SEQ (astfold_expr , expr_ty , node_ -> v .ClassDef .bases );
@@ -648,7 +654,9 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, int optimize_)
648
654
break ;
649
655
case AnnAssign_kind :
650
656
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
+ }
652
660
CALL_OPT (astfold_expr , expr_ty , node_ -> v .AnnAssign .value );
653
661
break ;
654
662
case For_kind :
@@ -707,7 +715,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, int optimize_)
707
715
}
708
716
709
717
static int
710
- astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , int optimize_ )
718
+ astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
711
719
{
712
720
switch (node_ -> kind ) {
713
721
case ExceptHandler_kind :
@@ -721,7 +729,7 @@ astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, int optimize_)
721
729
}
722
730
723
731
static int
724
- astfold_withitem (withitem_ty node_ , PyArena * ctx_ , int optimize_ )
732
+ astfold_withitem (withitem_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
725
733
{
726
734
CALL (astfold_expr , expr_ty , node_ -> context_expr );
727
735
CALL_OPT (astfold_expr , expr_ty , node_ -> optional_vars );
@@ -734,9 +742,9 @@ astfold_withitem(withitem_ty node_, PyArena *ctx_, int optimize_)
734
742
#undef CALL_INT_SEQ
735
743
736
744
int
737
- _PyAST_Optimize (mod_ty mod , PyArena * arena , int optimize )
745
+ _PyAST_Optimize (mod_ty mod , PyArena * arena , _PyASTOptimizeState * state )
738
746
{
739
- int ret = astfold_mod (mod , arena , optimize );
747
+ int ret = astfold_mod (mod , arena , state );
740
748
assert (ret || PyErr_Occurred ());
741
749
return ret ;
742
750
}
0 commit comments