@@ -475,6 +475,40 @@ def test_constant_folding(self):
475475                    self .assertFalse (instr .opname .startswith ('BUILD_' ))
476476                self .check_lnotab (code )
477477
478+     def  test_folding_subscript (self ):
479+         tests  =  [
480+             # small ints 
481+             ('(1, )[0]' , True , False ),
482+             ('(255, )[0]' , True , False ),
483+             ('(1, (1, 2))[1][1]' , True , False ),
484+             ('(1, 2)[2-1]' , True , False ),
485+             ('(1, (1, 2))[1][2-1]' , True , False ),
486+             ('(1, (1, 2))[1:6][0][2-1]' , True , False ),
487+             # regular ints 
488+             ('(256, )[0]' , False , False ),
489+             ('(1, (1, 1000))[1][1]' , False , False ),
490+             ('(1, 1000)[2-1]' , False , False ),
491+             ('(1, (1, 1000))[1][2-1]' , False , False ),
492+             # errors 
493+             ('(1, )[1]' , True , True ),
494+             ('(1, )[-2]' , False , True ),
495+             ('"a"[1]' , True , True ),
496+             ('"a"[-2]' , False , True ),
497+             ('(1, (1, 2))[2:6][0][2-1]' , True , True ),
498+         ]
499+         for  expr , has_small_int , has_error  in  tests :
500+             with  self .subTest (expr = expr , has_small_int = has_small_int , has_error = has_error ):
501+                 code  =  compile (expr , '' , 'single' )
502+                 if  not  has_error :
503+                     self .assertNotInBytecode (code , 'BINARY_SUBSCR' )
504+                 else :
505+                     self .assertInBytecode (code , 'BINARY_SUBSCR' )
506+                 if  has_small_int :
507+                     self .assertInBytecode (code , 'LOAD_SMALL_INT' )
508+                 else :
509+                     self .assertNotInBytecode (code , 'LOAD_SMALL_INT' )
510+                 self .check_lnotab (code )
511+ 
478512    def  test_in_literal_list (self ):
479513        def  containtest ():
480514            return  x  in  [a , b ]
0 commit comments