Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8e43cd7

Browse files
committed
verify that warnings are issued for bogus uses of global
1 parent ff443a5 commit 8e43cd7

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

Lib/test/output/test_global

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test_global
2+
got SyntaxWarning as expected
3+
got SyntaxWarning as expected
4+
got SyntaxWarning as expected

Lib/test/test_global.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Verify that warnings are issued for global statements following use"""
2+
3+
from test_support import check_syntax
4+
5+
import warnings
6+
7+
warnings.filterwarnings("error", category=SyntaxWarning, module=__name__)
8+
9+
def compile_and_catch_warning(text):
10+
try:
11+
compile(text, "<test code>", "exec")
12+
except SyntaxWarning, msg:
13+
print "got SyntaxWarning as expected"
14+
else:
15+
print "expected SyntaxWarning"
16+
17+
prog_text_1 = """
18+
def wrong1():
19+
a = 1
20+
b = 2
21+
global a
22+
global b
23+
"""
24+
compile_and_catch_warning(prog_text_1)
25+
26+
prog_text_2 = """
27+
def wrong2():
28+
print x
29+
global x
30+
"""
31+
compile_and_catch_warning(prog_text_2)
32+
33+
prog_text_3 = """
34+
def wrong3():
35+
print x
36+
x = 2
37+
global x
38+
"""
39+
compile_and_catch_warning(prog_text_3)
40+

0 commit comments

Comments
 (0)