1- """Verify that warnings are issued for global statements following use"""
1+ """Verify that warnings are issued for global statements following use. """
22
33from test_support import check_syntax
44
55import warnings
66
77warnings .filterwarnings ("error" , module = "<test code>" )
88
9- def compile_and_catch_warning (text ):
9+ def compile_and_check (text , should_fail = 1 ):
1010 try :
1111 compile (text , "<test code>" , "exec" )
1212 except SyntaxError , msg :
13- print "got SyntaxError as expected"
13+ if should_fail :
14+ print "got SyntaxError as expected"
15+ else :
16+ print "raised unexpected SyntaxError:" , text
1417 else :
15- print "expected SyntaxError"
18+ if should_fail :
19+ print "should have raised SyntaxError:" , text
20+ else :
21+ print "as expected, no SyntaxError"
1622
1723prog_text_1 = """
1824def wrong1():
@@ -21,25 +27,25 @@ def wrong1():
2127 global a
2228 global b
2329"""
24- compile_and_catch_warning (prog_text_1 )
30+ compile_and_check (prog_text_1 )
2531
2632prog_text_2 = """
2733def wrong2():
2834 print x
2935 global x
3036"""
31- compile_and_catch_warning (prog_text_2 )
37+ compile_and_check (prog_text_2 )
3238
3339prog_text_3 = """
3440def wrong3():
3541 print x
3642 x = 2
3743 global x
3844"""
39- compile_and_catch_warning (prog_text_3 )
45+ compile_and_check (prog_text_3 )
4046
4147prog_text_4 = """
4248global x
4349x = 2
4450"""
45- compile_and_catch_warning (prog_text_4 )
51+ compile_and_check (prog_text_4 , 0 )
0 commit comments