test_global.py 1.31 KB
Newer Older
1
"""Verify that warnings are issued for global statements following use."""
2

3
from test.support import run_unittest, check_syntax_error, check_warnings
4
import unittest
5
import warnings
6

7

8 9
class GlobalTests(unittest.TestCase):

10 11 12 13 14 15 16 17 18
    def setUp(self):
        self._warnings_manager = check_warnings()
        self._warnings_manager.__enter__()
        warnings.filterwarnings("error", module="<test string>")

    def tearDown(self):
        self._warnings_manager.__exit__(None, None, None)


19 20
    def test1(self):
        prog_text_1 = """\
21 22 23 24 25 26
def wrong1():
    a = 1
    b = 2
    global a
    global b
"""
27
        check_syntax_error(self, prog_text_1, lineno=4, offset=4)
28

29 30
    def test2(self):
        prog_text_2 = """\
31
def wrong2():
32
    print(x)
33 34
    global x
"""
35
        check_syntax_error(self, prog_text_2, lineno=3, offset=4)
36

37 38
    def test3(self):
        prog_text_3 = """\
39
def wrong3():
40
    print(x)
41 42 43
    x = 2
    global x
"""
44
        check_syntax_error(self, prog_text_3, lineno=4, offset=4)
45

46 47
    def test4(self):
        prog_text_4 = """\
48 49 50
global x
x = 2
"""
51 52 53 54 55
        # this should work
        compile(prog_text_4, "<test string>", "exec")


def test_main():
Florent Xicluna's avatar
Florent Xicluna committed
56 57 58
    with warnings.catch_warnings():
        warnings.filterwarnings("error", module="<test string>")
        run_unittest(GlobalTests)
59 60 61

if __name__ == "__main__":
    test_main()