0001 import re 0002 import unittest 0003 import warnings 0004 0005 from test import test_support 0006 0007 class SyntaxTestCase(unittest.TestCase): 0008 0009 def _check_error(self, code, errtext, 0010 filename="<testcase>", mode="exec"): 0011 """Check that compiling code raises SyntaxError with errtext. 0012 0013 errtest is a regular expression that must be present in the 0014 test of the exception raised. 0015 """ 0016 try: 0017 compile(code, filename, mode) 0018 except SyntaxError, err: 0019 mo = re.search(errtext, str(err)) 0020 if mo is None: 0021 self.fail("SyntaxError did not contain '%r'" % (errtext,)) 0022 else: 0023 self.fail("compile() did not raise SyntaxError") 0024 0025 def test_assign_call(self): 0026 self._check_error("f() = 1", "assign") 0027 0028 def test_assign_del(self): 0029 self._check_error("del f()", "delete") 0030 0031 def test_global_err_then_warn(self): 0032 # Bug tickler: The SyntaxError raised for one global statement 0033 # shouldn't be clobbered by a SyntaxWarning issued for a later one. 0034 source = re.sub('(?m)^ *:', '', """\ 0035 :def error(a): 0036 : global a # SyntaxError 0037 :def warning(): 0038 : b = 1 0039 : global b # SyntaxWarning 0040 :""") 0041 warnings.filterwarnings(action='ignore', category=SyntaxWarning) 0042 self._check_error(source, "global") 0043 warnings.filters.pop(0) 0044 0045 def test_main(): 0046 test_support.run_unittest(SyntaxTestCase) 0047 0048 if __name__ == "__main__": 0049 test_main() 0050
Generated by PyXR 0.9.4