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