2001-03-01 21:48:16 -04:00
|
|
|
"""Verify that warnings are issued for global statements following use."""
|
2001-02-27 21:51:01 -04:00
|
|
|
|
2006-10-28 10:10:17 -03:00
|
|
|
from test.test_support import run_unittest, check_syntax_error
|
|
|
|
import unittest
|
2001-02-27 21:51:01 -04:00
|
|
|
import warnings
|
2010-08-02 20:34:49 -03:00
|
|
|
|
2001-02-27 21:51:01 -04:00
|
|
|
|
2006-10-28 10:10:17 -03:00
|
|
|
class GlobalTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test1(self):
|
|
|
|
prog_text_1 = """\
|
2001-02-27 21:51:01 -04:00
|
|
|
def wrong1():
|
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
global a
|
|
|
|
global b
|
|
|
|
"""
|
2006-10-28 10:10:17 -03:00
|
|
|
check_syntax_error(self, prog_text_1)
|
2001-02-27 21:51:01 -04:00
|
|
|
|
2006-10-28 10:10:17 -03:00
|
|
|
def test2(self):
|
|
|
|
prog_text_2 = """\
|
2001-02-27 21:51:01 -04:00
|
|
|
def wrong2():
|
|
|
|
print x
|
|
|
|
global x
|
|
|
|
"""
|
2006-10-28 10:10:17 -03:00
|
|
|
check_syntax_error(self, prog_text_2)
|
2001-02-27 21:51:01 -04:00
|
|
|
|
2006-10-28 10:10:17 -03:00
|
|
|
def test3(self):
|
|
|
|
prog_text_3 = """\
|
2001-02-27 21:51:01 -04:00
|
|
|
def wrong3():
|
|
|
|
print x
|
|
|
|
x = 2
|
|
|
|
global x
|
|
|
|
"""
|
2006-10-28 10:10:17 -03:00
|
|
|
check_syntax_error(self, prog_text_3)
|
2001-02-28 19:49:19 -04:00
|
|
|
|
2006-10-28 10:10:17 -03:00
|
|
|
def test4(self):
|
|
|
|
prog_text_4 = """\
|
2001-02-28 19:49:19 -04:00
|
|
|
global x
|
|
|
|
x = 2
|
|
|
|
"""
|
2006-10-28 10:10:17 -03:00
|
|
|
# this should work
|
|
|
|
compile(prog_text_4, "<test string>", "exec")
|
|
|
|
|
|
|
|
|
|
|
|
def test_main():
|
2010-08-02 20:34:49 -03:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings("error", module="<test string>")
|
|
|
|
run_unittest(GlobalTests)
|
2006-10-28 10:10:17 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|