blob: fb10533ae46b50bbcbe40505593c3edfcd4bb099 [file] [log] [blame]
Tim Petersfc35de42001-03-02 01:48:16 +00001"""Verify that warnings are issued for global statements following use."""
Jeremy Hylton8e43cd72001-02-28 01:51:01 +00002
3from test_support import check_syntax
4
5import warnings
6
Jeremy Hylton150a6642001-02-28 22:50:15 +00007warnings.filterwarnings("error", module="<test code>")
Jeremy Hylton8e43cd72001-02-28 01:51:01 +00008
Tim Petersfc35de42001-03-02 01:48:16 +00009def compile_and_check(text, should_fail=1):
Jeremy Hylton8e43cd72001-02-28 01:51:01 +000010 try:
11 compile(text, "<test code>", "exec")
Jeremy Hylton150a6642001-02-28 22:50:15 +000012 except SyntaxError, msg:
Tim Petersfc35de42001-03-02 01:48:16 +000013 if should_fail:
14 print "got SyntaxError as expected"
15 else:
16 print "raised unexpected SyntaxError:", text
Jeremy Hylton8e43cd72001-02-28 01:51:01 +000017 else:
Tim Petersfc35de42001-03-02 01:48:16 +000018 if should_fail:
19 print "should have raised SyntaxError:", text
20 else:
21 print "as expected, no SyntaxError"
Jeremy Hylton8e43cd72001-02-28 01:51:01 +000022
23prog_text_1 = """
24def wrong1():
25 a = 1
26 b = 2
27 global a
28 global b
29"""
Tim Petersfc35de42001-03-02 01:48:16 +000030compile_and_check(prog_text_1)
Jeremy Hylton8e43cd72001-02-28 01:51:01 +000031
32prog_text_2 = """
33def wrong2():
34 print x
35 global x
36"""
Tim Petersfc35de42001-03-02 01:48:16 +000037compile_and_check(prog_text_2)
Jeremy Hylton8e43cd72001-02-28 01:51:01 +000038
39prog_text_3 = """
40def wrong3():
41 print x
42 x = 2
43 global x
44"""
Tim Petersfc35de42001-03-02 01:48:16 +000045compile_and_check(prog_text_3)
Jeremy Hylton2922ea82001-02-28 23:49:19 +000046
47prog_text_4 = """
48global x
49x = 2
50"""
Tim Petersfc35de42001-03-02 01:48:16 +000051compile_and_check(prog_text_4, 0)