Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame^] | 1 | """Verify that warnings are issued for global statements following use""" |
| 2 | |
| 3 | from test_support import check_syntax |
| 4 | |
| 5 | import warnings |
| 6 | |
| 7 | warnings.filterwarnings("error", category=SyntaxWarning, module=__name__) |
| 8 | |
| 9 | def compile_and_catch_warning(text): |
| 10 | try: |
| 11 | compile(text, "<test code>", "exec") |
| 12 | except SyntaxWarning, msg: |
| 13 | print "got SyntaxWarning as expected" |
| 14 | else: |
| 15 | print "expected SyntaxWarning" |
| 16 | |
| 17 | prog_text_1 = """ |
| 18 | def wrong1(): |
| 19 | a = 1 |
| 20 | b = 2 |
| 21 | global a |
| 22 | global b |
| 23 | """ |
| 24 | compile_and_catch_warning(prog_text_1) |
| 25 | |
| 26 | prog_text_2 = """ |
| 27 | def wrong2(): |
| 28 | print x |
| 29 | global x |
| 30 | """ |
| 31 | compile_and_catch_warning(prog_text_2) |
| 32 | |
| 33 | prog_text_3 = """ |
| 34 | def wrong3(): |
| 35 | print x |
| 36 | x = 2 |
| 37 | global x |
| 38 | """ |
| 39 | compile_and_catch_warning(prog_text_3) |
| 40 | |