1"""Verify that warnings are issued for global statements following use."""
2
3from test.support import check_syntax_error
4from test.support.warnings_helper import check_warnings
5import unittest
6import warnings
7
8
9class GlobalTests(unittest.TestCase):
10
11    def setUp(self):
12        self.enterContext(check_warnings())
13        warnings.filterwarnings("error", module="<test string>")
14
15    def test1(self):
16        prog_text_1 = """\
17def wrong1():
18    a = 1
19    b = 2
20    global a
21    global b
22"""
23        check_syntax_error(self, prog_text_1, lineno=4, offset=5)
24
25    def test2(self):
26        prog_text_2 = """\
27def wrong2():
28    print(x)
29    global x
30"""
31        check_syntax_error(self, prog_text_2, lineno=3, offset=5)
32
33    def test3(self):
34        prog_text_3 = """\
35def wrong3():
36    print(x)
37    x = 2
38    global x
39"""
40        check_syntax_error(self, prog_text_3, lineno=4, offset=5)
41
42    def test4(self):
43        prog_text_4 = """\
44global x
45x = 2
46"""
47        # this should work
48        compile(prog_text_4, "<test string>", "exec")
49
50
51def setUpModule():
52    unittest.enterModuleContext(warnings.catch_warnings())
53    warnings.filterwarnings("error", module="<test string>")
54
55
56if __name__ == "__main__":
57    unittest.main()
58