Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | import re |
2 | import unittest | |
3 | import warnings | |
4 | ||
5 | from test import test_support | |
6 | ||
7 | class SyntaxTestCase(unittest.TestCase): | |
8 | ||
9 | def _check_error(self, code, errtext, | |
10 | filename="<testcase>", mode="exec"): | |
11 | """Check that compiling code raises SyntaxError with errtext. | |
12 | ||
13 | errtest is a regular expression that must be present in the | |
14 | test of the exception raised. | |
15 | """ | |
16 | try: | |
17 | compile(code, filename, mode) | |
18 | except SyntaxError, err: | |
19 | mo = re.search(errtext, str(err)) | |
20 | if mo is None: | |
21 | self.fail("SyntaxError did not contain '%r'" % (errtext,)) | |
22 | else: | |
23 | self.fail("compile() did not raise SyntaxError") | |
24 | ||
25 | def test_assign_call(self): | |
26 | self._check_error("f() = 1", "assign") | |
27 | ||
28 | def test_assign_del(self): | |
29 | self._check_error("del f()", "delete") | |
30 | ||
31 | def test_global_err_then_warn(self): | |
32 | # Bug tickler: The SyntaxError raised for one global statement | |
33 | # shouldn't be clobbered by a SyntaxWarning issued for a later one. | |
34 | source = re.sub('(?m)^ *:', '', """\ | |
35 | :def error(a): | |
36 | : global a # SyntaxError | |
37 | :def warning(): | |
38 | : b = 1 | |
39 | : global b # SyntaxWarning | |
40 | :""") | |
41 | warnings.filterwarnings(action='ignore', category=SyntaxWarning) | |
42 | self._check_error(source, "global") | |
43 | warnings.filters.pop(0) | |
44 | ||
45 | def test_main(): | |
46 | test_support.run_unittest(SyntaxTestCase) | |
47 | ||
48 | if __name__ == "__main__": | |
49 | test_main() |