@@ -75,7 +75,7 @@ unary_not(PyObject *v)
7575}
7676
7777static int
78- fold_unaryop (expr_ty node , PyArena * arena )
78+ fold_unaryop (expr_ty node , PyArena * arena , int optimize )
7979{
8080 expr_ty arg = node -> v .UnaryOp .operand ;
8181
@@ -252,7 +252,7 @@ safe_mod(PyObject *v, PyObject *w)
252252}
253253
254254static int
255- fold_binop (expr_ty node , PyArena * arena )
255+ fold_binop (expr_ty node , PyArena * arena , int optimize )
256256{
257257 expr_ty lhs , rhs ;
258258 lhs = node -> v .BinOp .left ;
@@ -334,7 +334,7 @@ make_const_tuple(asdl_seq *elts)
334334}
335335
336336static int
337- fold_tuple (expr_ty node , PyArena * arena )
337+ fold_tuple (expr_ty node , PyArena * arena , int optimize )
338338{
339339 PyObject * newval ;
340340
@@ -346,7 +346,7 @@ fold_tuple(expr_ty node, PyArena *arena)
346346}
347347
348348static int
349- fold_subscr (expr_ty node , PyArena * arena )
349+ fold_subscr (expr_ty node , PyArena * arena , int optimize )
350350{
351351 PyObject * newval ;
352352 expr_ty arg , idx ;
@@ -374,7 +374,7 @@ fold_subscr(expr_ty node, PyArena *arena)
374374 in "for" loop and comprehensions.
375375*/
376376static int
377- fold_iter (expr_ty arg , PyArena * arena )
377+ fold_iter (expr_ty arg , PyArena * arena , int optimize )
378378{
379379 PyObject * newval ;
380380 if (arg -> kind == List_kind ) {
@@ -393,7 +393,7 @@ fold_iter(expr_ty arg, PyArena *arena)
393393}
394394
395395static int
396- fold_compare (expr_ty node , PyArena * arena )
396+ fold_compare (expr_ty node , PyArena * arena , int optimize )
397397{
398398 asdl_int_seq * ops ;
399399 asdl_seq * args ;
@@ -407,37 +407,37 @@ fold_compare(expr_ty node, PyArena *arena)
407407 i = asdl_seq_LEN (ops ) - 1 ;
408408 int op = asdl_seq_GET (ops , i );
409409 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 )) {
411411 return 0 ;
412412 }
413413 }
414414 return 1 ;
415415}
416416
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_ );
427427#define CALL (FUNC , TYPE , ARG ) \
428- if (!FUNC((ARG), ctx_)) \
428+ if (!FUNC((ARG), ctx_, optimize_ )) \
429429 return 0;
430430
431431#define CALL_OPT (FUNC , TYPE , ARG ) \
432- if ((ARG) != NULL && !FUNC((ARG), ctx_)) \
432+ if ((ARG) != NULL && !FUNC((ARG), ctx_, optimize_ )) \
433433 return 0;
434434
435435#define CALL_SEQ (FUNC , TYPE , ARG ) { \
436436 int i; \
437437 asdl_seq *seq = (ARG); /* avoid variable capture */ \
438438 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
439439 TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
440- if (elt != NULL && !FUNC(elt, ctx_)) \
440+ if (elt != NULL && !FUNC(elt, ctx_, optimize_ )) \
441441 return 0; \
442442 } \
443443}
@@ -447,13 +447,13 @@ static int astfold_excepthandler(excepthandler_ty node_, PyArena* ctx_);
447447 asdl_int_seq *seq = (ARG); /* avoid variable capture */ \
448448 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
449449 TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
450- if (!FUNC(elt, ctx_)) \
450+ if (!FUNC(elt, ctx_, optimize_ )) \
451451 return 0; \
452452 } \
453453}
454454
455455static int
456- astfold_mod (mod_ty node_ , PyArena * ctx_ )
456+ astfold_mod (mod_ty node_ , PyArena * ctx_ , int optimize_ )
457457{
458458 switch (node_ -> kind ) {
459459 case Module_kind :
@@ -475,7 +475,7 @@ astfold_mod(mod_ty node_, PyArena* ctx_)
475475}
476476
477477static int
478- astfold_expr (expr_ty node_ , PyArena * ctx_ )
478+ astfold_expr (expr_ty node_ , PyArena * ctx_ , int optimize_ )
479479{
480480 switch (node_ -> kind ) {
481481 case BoolOp_kind :
@@ -567,14 +567,19 @@ astfold_expr(expr_ty node_, PyArena* ctx_)
567567 CALL_SEQ (astfold_expr , expr_ty , node_ -> v .Tuple .elts );
568568 CALL (fold_tuple , expr_ty , node_ );
569569 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 ;
570575 default :
571576 break ;
572577 }
573578 return 1 ;
574579}
575580
576581static int
577- astfold_slice (slice_ty node_ , PyArena * ctx_ )
582+ astfold_slice (slice_ty node_ , PyArena * ctx_ , int optimize_ )
578583{
579584 switch (node_ -> kind ) {
580585 case Slice_kind :
@@ -595,14 +600,14 @@ astfold_slice(slice_ty node_, PyArena* ctx_)
595600}
596601
597602static int
598- astfold_keyword (keyword_ty node_ , PyArena * ctx_ )
603+ astfold_keyword (keyword_ty node_ , PyArena * ctx_ , int optimize_ )
599604{
600605 CALL (astfold_expr , expr_ty , node_ -> value );
601606 return 1 ;
602607}
603608
604609static int
605- astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ )
610+ astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , int optimize_ )
606611{
607612 CALL (astfold_expr , expr_ty , node_ -> target );
608613 CALL (astfold_expr , expr_ty , node_ -> iter );
@@ -613,7 +618,7 @@ astfold_comprehension(comprehension_ty node_, PyArena* ctx_)
613618}
614619
615620static int
616- astfold_arguments (arguments_ty node_ , PyArena * ctx_ )
621+ astfold_arguments (arguments_ty node_ , PyArena * ctx_ , int optimize_ )
617622{
618623 CALL_SEQ (astfold_arg , arg_ty , node_ -> args );
619624 CALL_OPT (astfold_arg , arg_ty , node_ -> vararg );
@@ -625,14 +630,14 @@ astfold_arguments(arguments_ty node_, PyArena* ctx_)
625630}
626631
627632static int
628- astfold_arg (arg_ty node_ , PyArena * ctx_ )
633+ astfold_arg (arg_ty node_ , PyArena * ctx_ , int optimize_ )
629634{
630635 CALL_OPT (astfold_expr , expr_ty , node_ -> annotation );
631636 return 1 ;
632637}
633638
634639static int
635- astfold_stmt (stmt_ty node_ , PyArena * ctx_ )
640+ astfold_stmt (stmt_ty node_ , PyArena * ctx_ , int optimize_ )
636641{
637642 switch (node_ -> kind ) {
638643 case FunctionDef_kind :
@@ -728,7 +733,7 @@ astfold_stmt(stmt_ty node_, PyArena* ctx_)
728733}
729734
730735static int
731- astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ )
736+ astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , int optimize_ )
732737{
733738 switch (node_ -> kind ) {
734739 case ExceptHandler_kind :
@@ -742,7 +747,7 @@ astfold_excepthandler(excepthandler_ty node_, PyArena* ctx_)
742747}
743748
744749static int
745- astfold_withitem (withitem_ty node_ , PyArena * ctx_ )
750+ astfold_withitem (withitem_ty node_ , PyArena * ctx_ , int optimize_ )
746751{
747752 CALL (astfold_expr , expr_ty , node_ -> context_expr );
748753 CALL_OPT (astfold_expr , expr_ty , node_ -> optional_vars );
@@ -755,9 +760,9 @@ astfold_withitem(withitem_ty node_, PyArena* ctx_)
755760#undef CALL_INT_SEQ
756761
757762int
758- _PyAST_Optimize (mod_ty mod , PyArena * arena )
763+ _PyAST_Optimize (mod_ty mod , PyArena * arena , int optimize )
759764{
760- int ret = astfold_mod (mod , arena );
765+ int ret = astfold_mod (mod , arena , optimize );
761766 assert (ret || PyErr_Occurred ());
762767 return ret ;
763768}
0 commit comments