Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | """Test cases for traceback module""" |
2 | ||
3 | import unittest | |
4 | from test.test_support import run_unittest, is_jython | |
5 | ||
6 | import traceback | |
7 | ||
8 | class TracebackCases(unittest.TestCase): | |
9 | # For now, a very minimal set of tests. I want to be sure that | |
10 | # formatting of SyntaxErrors works based on changes for 2.1. | |
11 | ||
12 | def get_exception_format(self, func, exc): | |
13 | try: | |
14 | func() | |
15 | except exc, value: | |
16 | return traceback.format_exception_only(exc, value) | |
17 | else: | |
18 | raise ValueError, "call did not raise exception" | |
19 | ||
20 | def syntax_error_with_caret(self): | |
21 | compile("def fact(x):\n\treturn x!\n", "?", "exec") | |
22 | ||
23 | def syntax_error_without_caret(self): | |
24 | # XXX why doesn't compile raise the same traceback? | |
25 | import test.badsyntax_nocaret | |
26 | ||
27 | def test_caret(self): | |
28 | err = self.get_exception_format(self.syntax_error_with_caret, | |
29 | SyntaxError) | |
30 | self.assert_(len(err) == 4) | |
31 | self.assert_("^" in err[2]) # third line has caret | |
32 | self.assert_(err[1].strip() == "return x!") | |
33 | ||
34 | def test_nocaret(self): | |
35 | if is_jython: | |
36 | # jython adds a caret in this case (why shouldn't it?) | |
37 | return | |
38 | err = self.get_exception_format(self.syntax_error_without_caret, | |
39 | SyntaxError) | |
40 | self.assert_(len(err) == 3) | |
41 | self.assert_(err[1].strip() == "[x for x in x] = x") | |
42 | ||
43 | def test_bug737473(self): | |
44 | import sys, os, tempfile, time | |
45 | ||
46 | savedpath = sys.path[:] | |
47 | testdir = tempfile.mkdtemp() | |
48 | try: | |
49 | sys.path.insert(0, testdir) | |
50 | testfile = os.path.join(testdir, 'test_bug737473.py') | |
51 | print >> open(testfile, 'w'), """ | |
52 | def test(): | |
53 | raise ValueError""" | |
54 | ||
55 | if 'test_bug737473' in sys.modules: | |
56 | del sys.modules['test_bug737473'] | |
57 | import test_bug737473 | |
58 | ||
59 | try: | |
60 | test_bug737473.test() | |
61 | except ValueError: | |
62 | # this loads source code to linecache | |
63 | traceback.extract_tb(sys.exc_traceback) | |
64 | ||
65 | # If this test runs too quickly, test_bug737473.py's mtime | |
66 | # attribute will remain unchanged even if the file is rewritten. | |
67 | # Consequently, the file would not reload. So, added a sleep() | |
68 | # delay to assure that a new, distinct timestamp is written. | |
69 | # Since WinME with FAT32 has multisecond resolution, more than | |
70 | # three seconds are needed for this test to pass reliably :-( | |
71 | time.sleep(4) | |
72 | ||
73 | print >> open(testfile, 'w'), """ | |
74 | def test(): | |
75 | raise NotImplementedError""" | |
76 | reload(test_bug737473) | |
77 | try: | |
78 | test_bug737473.test() | |
79 | except NotImplementedError: | |
80 | src = traceback.extract_tb(sys.exc_traceback)[-1][-1] | |
81 | self.failUnlessEqual(src, 'raise NotImplementedError') | |
82 | finally: | |
83 | sys.path[:] = savedpath | |
84 | for f in os.listdir(testdir): | |
85 | os.unlink(os.path.join(testdir, f)) | |
86 | os.rmdir(testdir) | |
87 | ||
88 | def test_main(): | |
89 | run_unittest(TracebackCases) | |
90 | ||
91 | ||
92 | if __name__ == "__main__": | |
93 | test_main() |