@@ -479,10 +479,13 @@ def test_constant_folding_small_int(self):
479479            ('(1 + 2, )[0]' , 3 ),
480480            ('(2 + 2 * 2, )[0]' , 6 ),
481481            ('(1, (1 + 2 + 3, ))[1][0]' , 6 ),
482+             ('1 + 2' , 3 ),
483+             ('2 + 2 * 2 // 2 - 2' , 2 ),
482484            ('(255, )[0]' , 255 ),
483485            ('(256, )[0]' , None ),
484486            ('(1000, )[0]' , None ),
485487            ('(1 - 2, )[0]' , None ),
488+             ('255 + 1' , None ),
486489        ]
487490        for  expr , oparg  in  tests :
488491            with  self .subTest (expr = expr , oparg = oparg ):
@@ -493,37 +496,85 @@ def test_constant_folding_small_int(self):
493496                    self .assertNotInBytecode (code , 'LOAD_SMALL_INT' )
494497                self .check_lnotab (code )
495498
496-     def  test_folding_subscript (self ):
499+     def  test_folding_binop (self ):
500+         add  =  get_binop_argval ('NB_ADD' )
501+         sub  =  get_binop_argval ('NB_SUBTRACT' )
502+         mul  =  get_binop_argval ('NB_MULTIPLY' )
503+         div  =  get_binop_argval ('NB_TRUE_DIVIDE' )
504+         floor  =  get_binop_argval ('NB_FLOOR_DIVIDE' )
505+         rem  =  get_binop_argval ('NB_REMAINDER' )
506+         pow  =  get_binop_argval ('NB_POWER' )
507+         lshift  =  get_binop_argval ('NB_LSHIFT' )
508+         rshift  =  get_binop_argval ('NB_RSHIFT' )
509+         or_  =  get_binop_argval ('NB_OR' )
510+         and_  =  get_binop_argval ('NB_AND' )
511+         xor  =  get_binop_argval ('NB_XOR' )
512+         subscr  =  get_binop_argval ('NB_SUBSCR' )
497513        tests  =  [
498-             ('(1, )[0]' , False ),
499-             ('(1, )[-1]' , False ),
500-             ('(1 + 2, )[0]' , False ),
501-             ('(1, (1, 2))[1][1]' , False ),
502-             ('(1, 2)[2-1]' , False ),
503-             ('(1, (1, 2))[1][2-1]' , False ),
504-             ('(1, (1, 2))[1:6][0][2-1]' , False ),
505-             ('"a"[0]' , False ),
506-             ('("a" + "b")[1]' , False ),
507-             ('("a" + "b", )[0][1]' , False ),
508-             ('("a" * 10)[9]' , False ),
509-             ('(1, )[1]' , True ),
510-             ('(1, )[-2]' , True ),
511-             ('"a"[1]' , True ),
512-             ('"a"[-2]' , True ),
513-             ('("a" + "b")[2]' , True ),
514-             ('("a" + "b", )[0][2]' , True ),
515-             ('("a" + "b", )[1][0]' , True ),
516-             ('("a" * 10)[10]' , True ),
517-             ('(1, (1, 2))[2:6][0][2-1]' , True ),
514+             ('1 + 2' , False , add ),
515+             ('1 + 2 + 3' , False , add ),
516+             ('1 + ""' , True , add ),
517+             ('1 - 2' , False , sub ),
518+             ('1 - 2 - 3' , False , sub ),
519+             ('1 - ""' , True , sub ),
520+             ('2 * 2' , False , mul ),
521+             ('2 * 2 * 2' , False , mul ),
522+             ('2 / 2' , False , div ),
523+             ('2 / 2 / 2' , False , div ),
524+             ('2 / ""' , True , div ),
525+             ('2 // 2' , False , floor ),
526+             ('2 // 2 // 2' , False , floor ),
527+             ('2 // ""' , True , floor ),
528+             ('2 % 2' , False , rem ),
529+             ('2 % 2 % 2' , False , rem ),
530+             ('2 % ()' , True , rem ),
531+             ('2 ** 2' , False , pow ),
532+             ('2 ** 2 ** 2' , False , pow ),
533+             ('2 ** ""' , True , pow ),
534+             ('2 << 2' , False , lshift ),
535+             ('2 << 2 << 2' , False , lshift ),
536+             ('2 << ""' , True , lshift ),
537+             ('2 >> 2' , False , rshift ),
538+             ('2 >> 2 >> 2' , False , rshift ),
539+             ('2 >> ""' , True , rshift ),
540+             ('2 | 2' , False , or_ ),
541+             ('2 | 2 | 2' , False , or_ ),
542+             ('2 | ""' , True , or_ ),
543+             ('2 & 2' , False , and_ ),
544+             ('2 & 2 & 2' , False , and_ ),
545+             ('2 & ""' , True , and_ ),
546+             ('2 ^ 2' , False , xor ),
547+             ('2 ^ 2 ^ 2' , False , xor ),
548+             ('2 ^ ""' , True , xor ),
549+             ('(1, )[0]' , False , subscr ),
550+             ('(1, )[-1]' , False , subscr ),
551+             ('(1 + 2, )[0]' , False , subscr ),
552+             ('(1, (1, 2))[1][1]' , False , subscr ),
553+             ('(1, 2)[2-1]' , False , subscr ),
554+             ('(1, (1, 2))[1][2-1]' , False , subscr ),
555+             ('(1, (1, 2))[1:6][0][2-1]' , False , subscr ),
556+             ('"a"[0]' , False , subscr ),
557+             ('("a" + "b")[1]' , False , subscr ),
558+             ('("a" + "b", )[0][1]' , False , subscr ),
559+             ('("a" * 10)[9]' , False , subscr ),
560+             ('(1, )[1]' , True , subscr ),
561+             ('(1, )[-2]' , True , subscr ),
562+             ('"a"[1]' , True , subscr ),
563+             ('"a"[-2]' , True , subscr ),
564+             ('("a" + "b")[2]' , True , subscr ),
565+             ('("a" + "b", )[0][2]' , True , subscr ),
566+             ('("a" + "b", )[1][0]' , True , subscr ),
567+             ('("a" * 10)[10]' , True , subscr ),
568+             ('(1, (1, 2))[2:6][0][2-1]' , True , subscr ),
569+ 
518570        ]
519-         subscr_argval  =  get_binop_argval ('NB_SUBSCR' )
520-         for  expr , has_error  in  tests :
571+         for  expr , has_error , nb_op  in  tests :
521572            with  self .subTest (expr = expr , has_error = has_error ):
522573                code  =  compile (expr , '' , 'single' )
523574                if  not  has_error :
524-                     self .assertNotInBytecode (code , 'BINARY_OP' , argval = subscr_argval )
575+                     self .assertNotInBytecode (code , 'BINARY_OP' , argval = nb_op )
525576                else :
526-                     self .assertInBytecode (code , 'BINARY_OP' , argval = subscr_argval )
577+                     self .assertInBytecode (code , 'BINARY_OP' , argval = nb_op )
527578                self .check_lnotab (code )
528579
529580    def  test_constant_folding_remove_nop_location (self ):
@@ -1165,9 +1216,24 @@ def f():
11651216        self .assertEqual (f (), frozenset (range (40 )))
11661217
11671218    def  test_multiple_foldings (self ):
1219+         # (1, (2 + 2 * 2 // 2 - 2, )[0], )  ==>  (1, 2) 
11681220        before  =  [
11691221            ('LOAD_SMALL_INT' , 1 , 0 ),
1222+             ('NOP' , None , 0 ),
1223+             ('LOAD_SMALL_INT' , 2 , 0 ),
1224+             ('NOP' , None , 0 ),
1225+             ('NOP' , None , 0 ),
1226+             ('LOAD_SMALL_INT' , 2 , 0 ),
1227+             ('BINARY_OP' , get_binop_argval ('NB_MULTIPLY' )),
1228+             ('LOAD_SMALL_INT' , 2 , 0 ),
1229+             ('NOP' , None , 0 ),
1230+             ('BINARY_OP' , get_binop_argval ('NB_FLOOR_DIVIDE' )),
1231+             ('NOP' , None , 0 ),
1232+             ('LOAD_SMALL_INT' , 2 , 0 ),
1233+             ('BINARY_OP' , get_binop_argval ('NB_ADD' )),
1234+             ('NOP' , None , 0 ),
11701235            ('LOAD_SMALL_INT' , 2 , 0 ),
1236+             ('BINARY_OP' , get_binop_argval ('NB_SUBTRACT' )),
11711237            ('BUILD_TUPLE' , 1 , 0 ),
11721238            ('LOAD_SMALL_INT' , 0 , 0 ),
11731239            ('BINARY_OP' , get_binop_argval ('NB_SUBSCR' ), 0 ),
0 commit comments