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 | |
Jeremy Hylton | 150a664 | 2001-02-28 22:50:15 +0000 | [diff] [blame] | 7 | warnings.filterwarnings("error", module="<test code>") |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 8 | |
| 9 | def compile_and_catch_warning(text): |
| 10 | try: |
| 11 | compile(text, "<test code>", "exec") |
Jeremy Hylton | 150a664 | 2001-02-28 22:50:15 +0000 | [diff] [blame] | 12 | except SyntaxError, msg: |
| 13 | print "got SyntaxError as expected" |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 14 | else: |
Jeremy Hylton | 150a664 | 2001-02-28 22:50:15 +0000 | [diff] [blame] | 15 | print "expected SyntaxError" |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 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) |
Jeremy Hylton | 2922ea8 | 2001-02-28 23:49:19 +0000 | [diff] [blame^] | 40 | |
| 41 | prog_text_4 = """ |
| 42 | global x |
| 43 | x = 2 |
| 44 | """ |
| 45 | compile_and_catch_warning(prog_text_4) |