| 1 |
"""Verify that warnings are issued for global statements following use.""" |
| 2 |
|
| 3 |
from test.test_support import run_unittest, check_syntax_error |
| 4 |
import unittest |
| 5 |
import warnings |
| 6 |
|
| 7 |
|
| 8 |
class GlobalTests(unittest.TestCase): |
| 9 |
|
| 10 |
def test1(self): |
| 11 |
prog_text_1 = """\ |
| 12 |
def wrong1(): |
| 13 |
a = 1 |
| 14 |
b = 2 |
| 15 |
global a |
| 16 |
global b |
| 17 |
""" |
| 18 |
check_syntax_error(self, prog_text_1) |
| 19 |
|
| 20 |
def test2(self): |
| 21 |
prog_text_2 = """\ |
| 22 |
def wrong2(): |
| 23 |
print x |
| 24 |
global x |
| 25 |
""" |
| 26 |
check_syntax_error(self, prog_text_2) |
| 27 |
|
| 28 |
def test3(self): |
| 29 |
prog_text_3 = """\ |
| 30 |
def wrong3(): |
| 31 |
print x |
| 32 |
x = 2 |
| 33 |
global x |
| 34 |
""" |
| 35 |
check_syntax_error(self, prog_text_3) |
| 36 |
|
| 37 |
def test4(self): |
| 38 |
prog_text_4 = """\ |
| 39 |
global x |
| 40 |
x = 2 |
| 41 |
""" |
| 42 |
|
| 43 |
compile(prog_text_4, "<test string>", "exec") |
| 44 |
|
| 45 |
|
| 46 |
def test_main(): |
| 47 |
with warnings.catch_warnings(): |
| 48 |
warnings.filterwarnings("error", module="<test string>") |
| 49 |
run_unittest(GlobalTests) |
| 50 |
|
| 51 |
if __name__ == "__main__": |
| 52 |
test_main() |