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