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

3
from test.test_support import check_syntax
4 5 6

import warnings

7
warnings.filterwarnings("error", module="<test code>")
8

9
def compile_and_check(text, should_fail=1):
10 11
    try:
        compile(text, "<test code>", "exec")
12
    except SyntaxError, msg:
13 14 15 16
        if should_fail:
            print "got SyntaxError as expected"
        else:
            print "raised unexpected SyntaxError:", text
17
    else:
18 19 20 21
        if should_fail:
            print "should have raised SyntaxError:", text
        else:
            print "as expected, no SyntaxError"
22 23 24 25 26 27 28 29

prog_text_1 = """
def wrong1():
    a = 1
    b = 2
    global a
    global b
"""
30
compile_and_check(prog_text_1)
31 32 33 34 35 36

prog_text_2 = """
def wrong2():
    print x
    global x
"""
37
compile_and_check(prog_text_2)
38 39 40 41 42 43 44

prog_text_3 = """
def wrong3():
    print x
    x = 2
    global x
"""
45
compile_and_check(prog_text_3)
46 47 48 49 50

prog_text_4 = """
global x
x = 2
"""
51
compile_and_check(prog_text_4, 0)