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