Tim Peters | fc35de4 | 2001-03-02 01:48:16 +0000 | [diff] [blame] | 1 | """Verify that warnings are issued for global statements following use.""" |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 2 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 3 | from test.test_support import run_unittest, check_syntax_error |
4 | import unittest | ||||
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 5 | |
6 | import warnings | ||||
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 7 | warnings.filterwarnings("error", module="<test string>") |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 8 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 9 | class GlobalTests(unittest.TestCase): |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 10 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 11 | def test1(self): |
12 | prog_text_1 = """\ | ||||
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 13 | def wrong1(): |
14 | a = 1 | ||||
15 | b = 2 | ||||
16 | global a | ||||
17 | global b | ||||
18 | """ | ||||
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 19 | check_syntax_error(self, prog_text_1) |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 20 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 21 | def test2(self): |
22 | prog_text_2 = """\ | ||||
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 23 | def wrong2(): |
24 | print x | ||||
25 | global x | ||||
26 | """ | ||||
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 27 | check_syntax_error(self, prog_text_2) |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 28 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 29 | def test3(self): |
30 | prog_text_3 = """\ | ||||
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 31 | def wrong3(): |
32 | print x | ||||
33 | x = 2 | ||||
34 | global x | ||||
35 | """ | ||||
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 36 | check_syntax_error(self, prog_text_3) |
Jeremy Hylton | 2922ea8 | 2001-02-28 23:49:19 +0000 | [diff] [blame] | 37 | |
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 38 | def test4(self): |
39 | prog_text_4 = """\ | ||||
Jeremy Hylton | 2922ea8 | 2001-02-28 23:49:19 +0000 | [diff] [blame] | 40 | global x |
41 | x = 2 | ||||
42 | """ | ||||
Georg Brandl | c6fdec6 | 2006-10-28 13:10:17 +0000 | [diff] [blame] | 43 | # this should work |
44 | compile(prog_text_4, "<test string>", "exec") | ||||
45 | |||||
46 | |||||
47 | def test_main(): | ||||
48 | run_unittest(GlobalTests) | ||||
49 | |||||
50 | if __name__ == "__main__": | ||||
51 | test_main() |