test_global.py 1.2 KB

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