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 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test.support import run_unittest, check_syntax_error |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4 | import unittest |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 5 | |
6 | import warnings | ||||
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7 | warnings.filterwarnings("error", module="<test string>") |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 8 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9 | class GlobalTests(unittest.TestCase): |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | """ | ||||
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 19 | check_syntax_error(self, prog_text_1) |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 20 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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(): |
Neal Norwitz | 752abd0 | 2008-05-13 04:55:24 +0000 | [diff] [blame] | 24 | print(x) |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 25 | global x |
26 | """ | ||||
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 27 | check_syntax_error(self, prog_text_2) |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 28 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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(): |
Neal Norwitz | 752abd0 | 2008-05-13 04:55:24 +0000 | [diff] [blame] | 32 | print(x) |
Jeremy Hylton | 8e43cd7 | 2001-02-28 01:51:01 +0000 | [diff] [blame] | 33 | x = 2 |
34 | global x | ||||
35 | """ | ||||
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 36 | check_syntax_error(self, prog_text_3) |
Jeremy Hylton | 2922ea8 | 2001-02-28 23:49:19 +0000 | [diff] [blame] | 37 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | """ | ||||
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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() |