1 """Verify that warnings are issued for global statements following use."""
2
3 from test.support import check_syntax_error
4 from test.support.warnings_helper import check_warnings
5 import unittest
6 import warnings
7
8
9 class ESC[4;38;5;81mGlobalTests(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
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 = """\
17 def 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 = """\
27 def 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 = """\
35 def 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 = """\
44 global x
45 x = 2
46 """
47 # this should work
48 compile(prog_text_4, "<test string>", "exec")
49
50
51 def setUpModule():
52 unittest.enterModuleContext(warnings.catch_warnings())
53 warnings.filterwarnings("error", module="<test string>")
54
55
56 if __name__ == "__main__":
57 unittest.main()