Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | """Verify that warnings are issued for global statements following use.""" |
2 | ||
3 | from test.test_support import check_syntax | |
4 | ||
5 | import warnings | |
6 | ||
7 | warnings.filterwarnings("error", module="<test code>") | |
8 | ||
9 | def compile_and_check(text, should_fail=1): | |
10 | try: | |
11 | compile(text, "<test code>", "exec") | |
12 | except SyntaxError, msg: | |
13 | if should_fail: | |
14 | print "got SyntaxError as expected" | |
15 | else: | |
16 | print "raised unexpected SyntaxError:", text | |
17 | else: | |
18 | if should_fail: | |
19 | print "should have raised SyntaxError:", text | |
20 | else: | |
21 | print "as expected, no SyntaxError" | |
22 | ||
23 | prog_text_1 = """ | |
24 | def wrong1(): | |
25 | a = 1 | |
26 | b = 2 | |
27 | global a | |
28 | global b | |
29 | """ | |
30 | compile_and_check(prog_text_1) | |
31 | ||
32 | prog_text_2 = """ | |
33 | def wrong2(): | |
34 | print x | |
35 | global x | |
36 | """ | |
37 | compile_and_check(prog_text_2) | |
38 | ||
39 | prog_text_3 = """ | |
40 | def wrong3(): | |
41 | print x | |
42 | x = 2 | |
43 | global x | |
44 | """ | |
45 | compile_and_check(prog_text_3) | |
46 | ||
47 | prog_text_4 = """ | |
48 | global x | |
49 | x = 2 | |
50 | """ | |
51 | compile_and_check(prog_text_4, 0) |