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