test_global.py 1003 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """Verify that warnings are issued for global statements following use."""
  2. from test.test_support import run_unittest, check_syntax_error
  3. import unittest
  4. import warnings
  5. class GlobalTests(unittest.TestCase):
  6. def test1(self):
  7. prog_text_1 = """\
  8. def wrong1():
  9. a = 1
  10. b = 2
  11. global a
  12. global b
  13. """
  14. check_syntax_error(self, prog_text_1)
  15. def test2(self):
  16. prog_text_2 = """\
  17. def wrong2():
  18. print x
  19. global x
  20. """
  21. check_syntax_error(self, prog_text_2)
  22. def test3(self):
  23. prog_text_3 = """\
  24. def wrong3():
  25. print x
  26. x = 2
  27. global x
  28. """
  29. check_syntax_error(self, prog_text_3)
  30. def test4(self):
  31. prog_text_4 = """\
  32. global x
  33. x = 2
  34. """
  35. # this should work
  36. compile(prog_text_4, "<test string>", "exec")
  37. def test_main():
  38. with warnings.catch_warnings():
  39. warnings.filterwarnings("error", module="<test string>")
  40. run_unittest(GlobalTests)
  41. if __name__ == "__main__":
  42. test_main()