blob: b41b7d4cc8195afe4a4c667074c4409d9e4e204e [file] [log] [blame]
Jeremy Hylton8e43cd72001-02-28 01:51:01 +00001"""Verify that warnings are issued for global statements following use"""
2
3from test_support import check_syntax
4
5import warnings
6
Jeremy Hylton150a6642001-02-28 22:50:15 +00007warnings.filterwarnings("error", module="<test code>")
Jeremy Hylton8e43cd72001-02-28 01:51:01 +00008
9def compile_and_catch_warning(text):
10 try:
11 compile(text, "<test code>", "exec")
Jeremy Hylton150a6642001-02-28 22:50:15 +000012 except SyntaxError, msg:
13 print "got SyntaxError as expected"
Jeremy Hylton8e43cd72001-02-28 01:51:01 +000014 else:
Jeremy Hylton150a6642001-02-28 22:50:15 +000015 print "expected SyntaxError"
Jeremy Hylton8e43cd72001-02-28 01:51:01 +000016
17prog_text_1 = """
18def wrong1():
19 a = 1
20 b = 2
21 global a
22 global b
23"""
24compile_and_catch_warning(prog_text_1)
25
26prog_text_2 = """
27def wrong2():
28 print x
29 global x
30"""
31compile_and_catch_warning(prog_text_2)
32
33prog_text_3 = """
34def wrong3():
35 print x
36 x = 2
37 global x
38"""
39compile_and_catch_warning(prog_text_3)
Jeremy Hylton2922ea82001-02-28 23:49:19 +000040
41prog_text_4 = """
42global x
43x = 2
44"""
45compile_and_catch_warning(prog_text_4)