|
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 syntax_error_bad_indentation(self): |
|
28 compile("def spam():\n print 1\n print 2", "?", "exec") |
|
29 |
|
30 def test_caret(self): |
|
31 err = self.get_exception_format(self.syntax_error_with_caret, |
|
32 SyntaxError) |
|
33 self.assert_(len(err) == 4) |
|
34 self.assert_(err[1].strip() == "return x!") |
|
35 self.assert_("^" in err[2]) # third line has caret |
|
36 self.assert_(err[1].find("!") == err[2].find("^")) # in the right place |
|
37 |
|
38 def test_nocaret(self): |
|
39 if is_jython: |
|
40 # jython adds a caret in this case (why shouldn't it?) |
|
41 return |
|
42 err = self.get_exception_format(self.syntax_error_without_caret, |
|
43 SyntaxError) |
|
44 self.assert_(len(err) == 3) |
|
45 self.assert_(err[1].strip() == "[x for x in x] = x") |
|
46 |
|
47 def test_bad_indentation(self): |
|
48 err = self.get_exception_format(self.syntax_error_bad_indentation, |
|
49 IndentationError) |
|
50 self.assert_(len(err) == 4) |
|
51 self.assert_(err[1].strip() == "print 2") |
|
52 self.assert_("^" in err[2]) |
|
53 self.assert_(err[1].find("2") == err[2].find("^")) |
|
54 |
|
55 def test_bug737473(self): |
|
56 import sys, os, tempfile, time |
|
57 |
|
58 savedpath = sys.path[:] |
|
59 testdir = tempfile.mkdtemp() |
|
60 try: |
|
61 sys.path.insert(0, testdir) |
|
62 testfile = os.path.join(testdir, 'test_bug737473.py') |
|
63 print >> open(testfile, 'w'), """ |
|
64 def test(): |
|
65 raise ValueError""" |
|
66 |
|
67 if 'test_bug737473' in sys.modules: |
|
68 del sys.modules['test_bug737473'] |
|
69 import test_bug737473 |
|
70 |
|
71 try: |
|
72 test_bug737473.test() |
|
73 except ValueError: |
|
74 # this loads source code to linecache |
|
75 traceback.extract_tb(sys.exc_traceback) |
|
76 |
|
77 # If this test runs too quickly, test_bug737473.py's mtime |
|
78 # attribute will remain unchanged even if the file is rewritten. |
|
79 # Consequently, the file would not reload. So, added a sleep() |
|
80 # delay to assure that a new, distinct timestamp is written. |
|
81 # Since WinME with FAT32 has multisecond resolution, more than |
|
82 # three seconds are needed for this test to pass reliably :-( |
|
83 time.sleep(4) |
|
84 |
|
85 print >> open(testfile, 'w'), """ |
|
86 def test(): |
|
87 raise NotImplementedError""" |
|
88 reload(test_bug737473) |
|
89 try: |
|
90 test_bug737473.test() |
|
91 except NotImplementedError: |
|
92 src = traceback.extract_tb(sys.exc_traceback)[-1][-1] |
|
93 self.failUnlessEqual(src, 'raise NotImplementedError') |
|
94 finally: |
|
95 sys.path[:] = savedpath |
|
96 for f in os.listdir(testdir): |
|
97 os.unlink(os.path.join(testdir, f)) |
|
98 os.rmdir(testdir) |
|
99 |
|
100 def test_members(self): |
|
101 # Covers Python/structmember.c::listmembers() |
|
102 try: |
|
103 1/0 |
|
104 except: |
|
105 import sys |
|
106 sys.exc_traceback.__members__ |
|
107 |
|
108 def test_base_exception(self): |
|
109 # Test that exceptions derived from BaseException are formatted right |
|
110 e = KeyboardInterrupt() |
|
111 lst = traceback.format_exception_only(e.__class__, e) |
|
112 self.assertEqual(lst, ['KeyboardInterrupt\n']) |
|
113 |
|
114 # String exceptions are deprecated, but legal. The quirky form with |
|
115 # separate "type" and "value" tends to break things, because |
|
116 # not isinstance(value, type) |
|
117 # and a string cannot be the first argument to issubclass. |
|
118 # |
|
119 # Note that sys.last_type and sys.last_value do not get set if an |
|
120 # exception is caught, so we sort of cheat and just emulate them. |
|
121 # |
|
122 # test_string_exception1 is equivalent to |
|
123 # |
|
124 # >>> raise "String Exception" |
|
125 # |
|
126 # test_string_exception2 is equivalent to |
|
127 # |
|
128 # >>> raise "String Exception", "String Value" |
|
129 # |
|
130 def test_string_exception1(self): |
|
131 str_type = "String Exception" |
|
132 err = traceback.format_exception_only(str_type, None) |
|
133 self.assertEqual(len(err), 1) |
|
134 self.assertEqual(err[0], str_type + '\n') |
|
135 |
|
136 def test_string_exception2(self): |
|
137 str_type = "String Exception" |
|
138 str_value = "String Value" |
|
139 err = traceback.format_exception_only(str_type, str_value) |
|
140 self.assertEqual(len(err), 1) |
|
141 self.assertEqual(err[0], str_type + ': ' + str_value + '\n') |
|
142 |
|
143 def test_format_exception_only_bad__str__(self): |
|
144 class X(Exception): |
|
145 def __str__(self): |
|
146 1/0 |
|
147 err = traceback.format_exception_only(X, X()) |
|
148 self.assertEqual(len(err), 1) |
|
149 str_value = '<unprintable %s object>' % X.__name__ |
|
150 self.assertEqual(err[0], X.__name__ + ': ' + str_value + '\n') |
|
151 |
|
152 def test_without_exception(self): |
|
153 err = traceback.format_exception_only(None, None) |
|
154 self.assertEqual(err, ['None\n']) |
|
155 |
|
156 |
|
157 def test_main(): |
|
158 run_unittest(TracebackCases) |
|
159 |
|
160 |
|
161 if __name__ == "__main__": |
|
162 test_main() |