PyXR

c:\python24\lib \ test \ test_global.py



0001 """Verify that warnings are issued for global statements following use."""
0002 
0003 from test.test_support import check_syntax
0004 
0005 import warnings
0006 
0007 warnings.filterwarnings("error", module="<test code>")
0008 
0009 def compile_and_check(text, should_fail=1):
0010     try:
0011         compile(text, "<test code>", "exec")
0012     except SyntaxError, msg:
0013         if should_fail:
0014             print "got SyntaxError as expected"
0015         else:
0016             print "raised unexpected SyntaxError:", text
0017     else:
0018         if should_fail:
0019             print "should have raised SyntaxError:", text
0020         else:
0021             print "as expected, no SyntaxError"
0022 
0023 prog_text_1 = """
0024 def wrong1():
0025     a = 1
0026     b = 2
0027     global a
0028     global b
0029 """
0030 compile_and_check(prog_text_1)
0031 
0032 prog_text_2 = """
0033 def wrong2():
0034     print x
0035     global x
0036 """
0037 compile_and_check(prog_text_2)
0038 
0039 prog_text_3 = """
0040 def wrong3():
0041     print x
0042     x = 2
0043     global x
0044 """
0045 compile_and_check(prog_text_3)
0046 
0047 prog_text_4 = """
0048 global x
0049 x = 2
0050 """
0051 compile_and_check(prog_text_4, 0)
0052 

Generated by PyXR 0.9.4
SourceForge.net Logo