@@ -75,7 +75,7 @@ unary_not(PyObject *v)
75
75
}
76
76
77
77
static int
78
- fold_unaryop (expr_ty node , PyArena * arena )
78
+ fold_unaryop (expr_ty node , PyArena * arena , int optimize )
79
79
{
80
80
expr_ty arg = node -> v .UnaryOp .operand ;
81
81
@@ -252,7 +252,7 @@ safe_mod(PyObject *v, PyObject *w)
252
252
}
253
253
254
254
static int
255
- fold_binop (expr_ty node , PyArena * arena )
255
+ fold_binop (expr_ty node , PyArena * arena , int optimize )
256
256
{
257
257
expr_ty lhs , rhs ;
258
258
lhs = node -> v .BinOp .left ;
@@ -334,7 +334,7 @@ make_const_tuple(asdl_seq *elts)
334
334
}
335
335
336
336
static int
337
- fold_tuple (expr_ty node , PyArena * arena )
337
+ fold_tuple (expr_ty node , PyArena * arena , int optimize )
338
338
{
339
339
PyObject * newval ;
340
340
@@ -346,7 +346,7 @@ fold_tuple(expr_ty node, PyArena *arena)
346
346
}
347
347
348
348
static int
349
- fold_subscr (expr_ty node , PyArena * arena )
349
+ fold_subscr (expr_ty node , PyArena * arena , int optimize )
350
350
{
351
351
PyObject * newval ;
352
352
expr_ty arg , idx ;
@@ -374,7 +374,7 @@ fold_subscr(expr_ty node, PyArena *arena)
374
374
in "for" loop and comprehensions.
375
375
*/
376
376
static int
377
- fold_iter (expr_ty arg , PyArena * arena )
377
+ fold_iter (expr_ty arg , PyArena * arena , int optimize )
378
378
{
379
379
PyObject * newval ;
380
380
if (arg -> kind == List_kind ) {
@@ -393,7 +393,7 @@ fold_iter(expr_ty arg, PyArena *arena)
393
393
}
394
394
395
395
static int
396
- fold_compare (expr_ty node , PyArena * arena )
396
+ fold_compare (expr_ty node , PyArena * arena , int optimize )
397
397
{
398
398
asdl_int_seq * ops ;
399
399
asdl_seq * args ;
@@ -407,37 +407,37 @@ fold_compare(expr_ty node, PyArena *arena)
407
407
i = asdl_seq_LEN (ops ) - 1 ;
408
408
int op = asdl_seq_GET (ops , i );
409
409
if (op == In || op == NotIn ) {
410
- if (!fold_iter ((expr_ty )asdl_seq_GET (args , i ), arena )) {
410
+ if (!fold_iter ((expr_ty )asdl_seq_GET (args , i ), arena , optimize )) {
411
411
return 0 ;
412
412
}
413
413
}
414
414
return 1 ;
415
415
}
416
416
417
- static int astfold_mod (mod_ty node_ , PyArena * ctx_ );
418
- static int astfold_stmt (stmt_ty node_ , PyArena * ctx_ );
419
- static int astfold_expr (expr_ty node_ , PyArena * ctx_ );
420
- static int astfold_arguments (arguments_ty node_ , PyArena * ctx_ );
421
- static int astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ );
422
- static int astfold_keyword (keyword_ty node_ , PyArena * ctx_ );
423
- static int astfold_slice (slice_ty node_ , PyArena * ctx_ );
424
- static int astfold_arg (arg_ty node_ , PyArena * ctx_ );
425
- static int astfold_withitem (withitem_ty node_ , PyArena * ctx_ );
426
- static int astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ );
417
+ static int astfold_mod (mod_ty node_ , PyArena * ctx_ , int optimize_ );
418
+ static int astfold_stmt (stmt_ty node_ , PyArena * ctx_ , int optimize_ );
419
+ static int astfold_expr (expr_ty node_ , PyArena * ctx_ , int optimize_ );
420
+ static int astfold_arguments (arguments_ty node_ , PyArena * ctx_ , int optimize_ );
421
+ static int astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , int optimize_ );
422
+ static int astfold_keyword (keyword_ty node_ , PyArena * ctx_ , int optimize_ );
423
+ static int astfold_slice (slice_ty node_ , PyArena * ctx_ , int optimize_ );
424
+ static int astfold_arg (arg_ty node_ , PyArena * ctx_ , int optimize_ );
425
+ static int astfold_withitem (withitem_ty node_ , PyArena * ctx_ , int optimize_ );
426
+ static int astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , int optimize_ );
427
427
#define CALL (FUNC , TYPE , ARG ) \
428
- if (!FUNC((ARG), ctx_)) \
428
+ if (!FUNC((ARG), ctx_, optimize_ )) \
429
429
return 0;
430
430
431
431
#define CALL_OPT (FUNC , TYPE , ARG ) \
432
- if ((ARG) != NULL && !FUNC((ARG), ctx_)) \
432
+ if ((ARG) != NULL && !FUNC((ARG), ctx_, optimize_ )) \
433
433
return 0;
434
434
435
435
#define CALL_SEQ (FUNC , TYPE , ARG ) { \
436
436
int i; \
437
437
asdl_seq *seq = (ARG); /* avoid variable capture */ \
438
438
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
439
439
TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
440
- if (elt != NULL && !FUNC(elt, ctx_)) \
440
+ if (elt != NULL && !FUNC(elt, ctx_, optimize_ )) \
441
441
return 0; \
442
442
} \
443
443
}
@@ -447,13 +447,13 @@ static int astfold_excepthandler(excepthandler_ty node_, PyArena* ctx_);
447
447
asdl_int_seq *seq = (ARG); /* avoid variable capture */ \
448
448
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
449
449
TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
450
- if (!FUNC(elt, ctx_)) \
450
+ if (!FUNC(elt, ctx_, optimize_ )) \
451
451
return 0; \
452
452
} \
453
453
}
454
454
455
455
static int
456
- astfold_mod (mod_ty node_ , PyArena * ctx_ )
456
+ astfold_mod (mod_ty node_ , PyArena * ctx_ , int optimize_ )
457
457
{
458
458
switch (node_ -> kind ) {
459
459
case Module_kind :
@@ -475,7 +475,7 @@ astfold_mod(mod_ty node_, PyArena* ctx_)
475
475
}
476
476
477
477
static int
478
- astfold_expr (expr_ty node_ , PyArena * ctx_ )
478
+ astfold_expr (expr_ty node_ , PyArena * ctx_ , int optimize_ )
479
479
{
480
480
switch (node_ -> kind ) {
481
481
case BoolOp_kind :
@@ -567,14 +567,19 @@ astfold_expr(expr_ty node_, PyArena* ctx_)
567
567
CALL_SEQ (astfold_expr , expr_ty , node_ -> v .Tuple .elts );
568
568
CALL (fold_tuple , expr_ty , node_ );
569
569
break ;
570
+ case Name_kind :
571
+ if (_PyUnicode_EqualToASCIIString (node_ -> v .Name .id , "__debug__" )) {
572
+ return make_const (node_ , PyBool_FromLong (!optimize_ ), ctx_ );
573
+ }
574
+ break ;
570
575
default :
571
576
break ;
572
577
}
573
578
return 1 ;
574
579
}
575
580
576
581
static int
577
- astfold_slice (slice_ty node_ , PyArena * ctx_ )
582
+ astfold_slice (slice_ty node_ , PyArena * ctx_ , int optimize_ )
578
583
{
579
584
switch (node_ -> kind ) {
580
585
case Slice_kind :
@@ -595,14 +600,14 @@ astfold_slice(slice_ty node_, PyArena* ctx_)
595
600
}
596
601
597
602
static int
598
- astfold_keyword (keyword_ty node_ , PyArena * ctx_ )
603
+ astfold_keyword (keyword_ty node_ , PyArena * ctx_ , int optimize_ )
599
604
{
600
605
CALL (astfold_expr , expr_ty , node_ -> value );
601
606
return 1 ;
602
607
}
603
608
604
609
static int
605
- astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ )
610
+ astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , int optimize_ )
606
611
{
607
612
CALL (astfold_expr , expr_ty , node_ -> target );
608
613
CALL (astfold_expr , expr_ty , node_ -> iter );
@@ -613,7 +618,7 @@ astfold_comprehension(comprehension_ty node_, PyArena* ctx_)
613
618
}
614
619
615
620
static int
616
- astfold_arguments (arguments_ty node_ , PyArena * ctx_ )
621
+ astfold_arguments (arguments_ty node_ , PyArena * ctx_ , int optimize_ )
617
622
{
618
623
CALL_SEQ (astfold_arg , arg_ty , node_ -> args );
619
624
CALL_OPT (astfold_arg , arg_ty , node_ -> vararg );
@@ -625,14 +630,14 @@ astfold_arguments(arguments_ty node_, PyArena* ctx_)
625
630
}
626
631
627
632
static int
628
- astfold_arg (arg_ty node_ , PyArena * ctx_ )
633
+ astfold_arg (arg_ty node_ , PyArena * ctx_ , int optimize_ )
629
634
{
630
635
CALL_OPT (astfold_expr , expr_ty , node_ -> annotation );
631
636
return 1 ;
632
637
}
633
638
634
639
static int
635
- astfold_stmt (stmt_ty node_ , PyArena * ctx_ )
640
+ astfold_stmt (stmt_ty node_ , PyArena * ctx_ , int optimize_ )
636
641
{
637
642
switch (node_ -> kind ) {
638
643
case FunctionDef_kind :
@@ -728,7 +733,7 @@ astfold_stmt(stmt_ty node_, PyArena* ctx_)
728
733
}
729
734
730
735
static int
731
- astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ )
736
+ astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , int optimize_ )
732
737
{
733
738
switch (node_ -> kind ) {
734
739
case ExceptHandler_kind :
@@ -742,7 +747,7 @@ astfold_excepthandler(excepthandler_ty node_, PyArena* ctx_)
742
747
}
743
748
744
749
static int
745
- astfold_withitem (withitem_ty node_ , PyArena * ctx_ )
750
+ astfold_withitem (withitem_ty node_ , PyArena * ctx_ , int optimize_ )
746
751
{
747
752
CALL (astfold_expr , expr_ty , node_ -> context_expr );
748
753
CALL_OPT (astfold_expr , expr_ty , node_ -> optional_vars );
@@ -755,9 +760,9 @@ astfold_withitem(withitem_ty node_, PyArena* ctx_)
755
760
#undef CALL_INT_SEQ
756
761
757
762
int
758
- _PyAST_Optimize (mod_ty mod , PyArena * arena )
763
+ _PyAST_Optimize (mod_ty mod , PyArena * arena , int optimize )
759
764
{
760
- int ret = astfold_mod (mod , arena );
765
+ int ret = astfold_mod (mod , arena , optimize );
761
766
assert (ret || PyErr_Occurred ());
762
767
return ret ;
763
768
}
0 commit comments