@@ -357,6 +357,34 @@ def test_ast_validation(self):
357357 tree = ast .parse (snippet )
358358 compile (tree , '<string>' , 'exec' )
359359
360+ def test_optimization_levels__debug__ (self ):
361+ cases = [(- 1 , '__debug__' ), (0 , '__debug__' ), (1 , False ), (2 , False )]
362+ for (optval , expected ) in cases :
363+ with self .subTest (optval = optval , expected = expected ):
364+ res = ast .parse ("__debug__" , optimize = optval )
365+ self .assertIsInstance (res .body [0 ], ast .Expr )
366+ if isinstance (expected , bool ):
367+ self .assertIsInstance (res .body [0 ].value , ast .Constant )
368+ self .assertEqual (res .body [0 ].value .value , expected )
369+ else :
370+ self .assertIsInstance (res .body [0 ].value , ast .Name )
371+ self .assertEqual (res .body [0 ].value .id , expected )
372+
373+ def test_optimization_levels_const_folding (self ):
374+ folded = ('Expr' , (1 , 0 , 1 , 5 ), ('Constant' , (1 , 0 , 1 , 5 ), 3 , None ))
375+ not_folded = ('Expr' , (1 , 0 , 1 , 5 ),
376+ ('BinOp' , (1 , 0 , 1 , 5 ),
377+ ('Constant' , (1 , 0 , 1 , 1 ), 1 , None ),
378+ ('Add' ,),
379+ ('Constant' , (1 , 4 , 1 , 5 ), 2 , None )))
380+
381+ cases = [(- 1 , not_folded ), (0 , not_folded ), (1 , folded ), (2 , folded )]
382+ for (optval , expected ) in cases :
383+ with self .subTest (optval = optval ):
384+ tree = ast .parse ("1 + 2" , optimize = optval )
385+ res = to_tuple (tree .body [0 ])
386+ self .assertEqual (res , expected )
387+
360388 def test_invalid_position_information (self ):
361389 invalid_linenos = [
362390 (10 , 1 ), (- 10 , - 11 ), (10 , - 11 ), (- 5 , - 2 ), (- 5 , 1 )
0 commit comments