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